69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#define OnAxle(n, b) (4 * n * n + 1 + n * (b))
int main() {
int n, m, x, y, ans, m_val;
printf("please input an integer n: ");
scanf("%d", &n);
m = n >> 1;
for(x = -m; x <= m; ++x) {
for(y = -m; y <= m; ++y) {
m_val = abs(x) > abs(y) ? abs(x) : abs(y);
if(m_val == abs(y)) {
ans = y > 0 ? OnAxle(m_val, 3) + x : OnAxle(m_val, -1) - x;
} else {
ans = x < 0 ? OnAxle(m_val, 1) + y : OnAxle(m_val, -3) - y;
}
printf("%4d ", ans);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#define M 50 //控制方向
void main()
{
int i,j,n,count;
int dada[M][M] = {0};
printf("Input n(0~%d):",M);
scanf("%d",&n);
count = n*n;
for(i=0;i<(n+1)/2;i++)
{
for (j=i;j<n-i;j++)
{
dada[i][j] = count--;
}
for (j=i+1;j<n-i;j++)
{
dada[j][n-i-1] = count--;
}
for (j=n-i-2;j>=i;j--)
{
dada[n-i-1][j] = count--;
}
for (j=n-i-2;j>i;j--)
{
dada[j][i] = count--;
}
}
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("%5d",dada[i][j]);
}
printf("\n\n");
}
}
Input n(0~50):10
100 99 98 97 96 95 94 93 92 91
65 64 63 62 61 60 59 58 57 90
66 37 36 35 34 33 32 31 56 89
67 38 17 16 15 14 13 30 55 88
68 39 18 5 4 3 12 29 54 87
69 40 19 6 1 2 11 28 53 86
70 41 20 7 8 9 10 27 52 85
71 42 21 22 23 24 25 26 51 84
72 43 44 45 46 47 48 49 50 83
73 74 75 76 77 78 79 80 81 82
#include <cmath> //abs
#include <iomanip> //setw
using namespace std;
int luoXuanQueue(int x,int y)
{
int n = abs(x) > abs(y) ? abs(x) : abs(y);
int layer = 2*n - 1;
if( (x+y)>0 )
{
return (layer*layer)+layer+(y-x)+1;
}
else
{
layer++;
return (layer*layer)+layer+(x-y)+1;
}
}
int main()
{
for(int i = -2; i < 3; i++)
{
for(int j = -2; j < 3; j++)
{
cout << setw(3) << luoXuanQueue(j,i);
}
cout << endl;
}
return 0;
}