Last active
October 13, 2015 08:28
-
-
Save freetstar/4167827 to your computer and use it in GitHub Desktop.
将1到100以螺旋的方式输出,具体结果见代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* | |
* Filename: 111.c | |
* | |
* Description: | |
* | |
* Version: 1.0 | |
* Created: 2012年11月29日 15时08分53秒 | |
* Revision: none | |
* Compiler: gcc | |
* | |
* Author: freetstar (http://www.freetstar.com), [email protected] | |
* Organization: Tianjin Normal University | |
* | |
* ===================================================================================== | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
int main(int argc, const char *argv[]) | |
{ | |
int num; | |
//初始化数组信息 | |
int a[10][10]; | |
//循环的计数 | |
int i = 0; | |
//total | |
int topright= 10; | |
int downleft = 28; | |
for(num=0;num<5;num++) | |
{ | |
for(i=num; i<=9-num;i++) | |
{ | |
if(i==num) | |
{ | |
//前列 | |
for(int j=num+1;j<=9-num;j++) | |
a[j][num]=downleft+9-j-num; | |
} | |
if(i==9-num) | |
{ | |
//后列 | |
for(int j=num+1;j<=9-num;j++) | |
a[j][9-num]=topright+j-num; | |
} | |
//首行 | |
a[num][i]=topright-9+num+i; | |
// 末行 | |
a[9-num][i]=downleft-i+num; | |
} | |
downleft = downleft+2*(8-num*2)+2*(6-num*2)+2; | |
topright = topright+4*(8-num*2)+2; | |
} | |
//输出结果 | |
for (i = 0; i < 10; i++) { | |
for(int j=0;j<10;j++){ | |
printf("%-3d ",a[i][j]); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment