- 论坛徽章:
- 0
|
本帖最后由 erlangs 于 2011-01-26 15:41 编辑
- void round(int n)
- {
- int **matrix;
- matrix = (int **)malloc(sizeof(int *)*n);
- int in;
- for (in=0; in<n; in++)
- matrix[in] = (int *)malloc(sizeof(int)*n);
- matrix[n/2][n/2] = 1;
- int n2,np, i, x = 0, y = 1, x_step, y_step;
- for(;n>=3;n -= 2,x++,y += 2)
- {
- n2=n*n;
- x_step = 0;
- y_step = 1;
- for(i = (n-2)*(n-2)+1; i <= n2; i++)
- {
- printf("n = %d, x = %d,y= %d value = %d\n",n,x,y,i);
- matrix[x][y] = i;
- np = n-1;
- if(i == (n2 - np*3)) // 第一个拐点
- {
- x_step = 1;
- y_step = 0;
- }
- else if(i == (n2 - np*2))
- {
- x_step = 0;
- y_step = -1;
- }
- else if(i == (n2 - np))
- {
- x_step = -1;
- y_step = 0;
- }
- else if(i == n2)
- {
- continue;
- }
- x += x_step;
- y += y_step;
- }
- }
- }
复制代码 |
|