码迷,mamicode.com
首页 > 其他好文 > 详细

螺旋队列问题

时间:2015-07-27 16:26:58      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享技术分享技术分享

#include<stdio.h>
#include<stdlib.h>
using namespace std;


#define max(a,b)  ((a)<(b)?(b):(a))
#define abs(a) ((a)>0 ? (a):(-a))
int foo(int x, int y)
{
int t = max(abs(x), abs(y));
int u = t + t;
int v = u - 1;
v = v*v + u;
if (x == -t)
v += u + t - y;
else if (y == -t)
v += 3* u + x - t;
else if (y == t)
v += t - x;
else
v += y - t;
return v;
}
int main()
{
int x, y;
for (y = -4; y <= 4; y++)
{
for (x = -4; x <= 4; x++)
printf("%5d", foo(x, y));
printf("\n");
}
while (scanf_s("%d%d", &x, &y) == 2)
printf("%d\n", foo(x, y)); 
system("pause");
return 0;
}

技术分享

#include<iostream>
using namespace std;
int a[10][10];
void Fun(int n)
{
int m = 1; j, i;
for (i = 0; i < n / 2; i++)
{
for (j = 0; j < n - i; j++)
{
if (a[i][j] == 0)
a[i][j] = m++;
}
for (j = i + 1; j < n - i; j++)
{
if (a[j][n - 1 - i] == 0)
a[j][n - 1 - i] = m++;
}
for (j = n - i - 1; j > i; j--)
{
if (a[n - i - 1][j] == 0)
a[n - i - 1][j] = m++;
}
for (j = n - i - 1; j > i; j--)
{
if (a[j][i] == 0)
a[j][i] = m++;
}
}
if (n % 2 == 1)
a[n / 2][n / 2] = m;
}
main(void)
{
int n, i, j;
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
a[i][j] = 0;
}
Fun(n);
for (i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

螺旋队列问题

标签:

原文地址:http://blog.csdn.net/wangfengfan1/article/details/47072233

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!