当前位置:网站首页>Output a spiral matrix C language
Output a spiral matrix C language
2022-07-07 08:52:00 【Please Sit Down】
Clockwise :
int main(){
int a[10][10],i,j,k=0,m,n;
printf("Enter n(n<10):");
scanf("%d",&n);
if(n%2==0) m=m/2;
else m=m/2+1;
for(i=0;i<m;i++){
for(j=i;j<n-i;j++){
k++; a[i][j]=k;
}
for(j=i+1;j<n-i;j++){
k++; a[j][n-i-1]=k;
}
for(j=n-i-2;j>=i;j--){
k++; a[n-i-1][j]=k;
}
for(j=n-i-2;j>=i+1;j--){
k++; a[j][i]=k;
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%5d",a[i][j]);
}
printf("\n");
}
return 0;
}

Anti-clockwise :
int main(){
int a[10][10],i,j,k=0,m,n;
printf("Enter n(n<10):");
scanf("%d",&n);
if(n%2==0) m=m/2;
else m=m/2+1;
for(i=0;i<m;i++){
for(j=i;j<n-i;j++){
k++; a[j][i]=k;
}
for(j=i+1;j<n-i;j++){
k++; a[n-i-1][j]=k;
}
for(j=n-i-2;j>=i;j--){
k++; a[j][n-i-1]=k;
}
for(j=n-i-2;j>=i+1;j--){
k++; a[i][j]=k;
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%5d",a[i][j]);
}
printf("\n");
}
return 0;
} 
边栏推荐
- Redis summary
- [wechat applet: cache operation]
- Basic data types and string types are converted to each other
- 求有符号数的原码、反码和补码【C语言】
- 调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
- selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
- [Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
- leetcode135. Distribute candy
- Problems encountered in the use of go micro
- [Yugong series] February 2022 U3D full stack class 005 unity engine view
猜你喜欢

NCS Chengdu New Electric interview Experience

Pointer advanced, string function

Platformization, a fulcrum of strong chain complementing chain

最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼

Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical
![[Yugong series] February 2022 U3D full stack class 005 unity engine view](/img/19/dc1b2f74b9ec5f5808de3049793862.jpg)
[Yugong series] February 2022 U3D full stack class 005 unity engine view
![[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University](/img/e2/519a5267cd5425a83434d2da65ebe6.jpg)
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University

Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and

Why choose cloud native database

JS operation
随机推荐
MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
Teach you how to select PCB board by hand (II)
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
说一个软件创业项目,有谁愿意投资的吗?
National SMS center number inquiry
Greenplum 6.x version change record common manual
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
模拟卷Leetcode【普通】1609. 奇偶树
You should use Google related products with caution
Novice entry SCM must understand those things
[Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
LeetCode 736. Lisp 语法解析
测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
Platformization, a fulcrum of strong chain complementing chain
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
opencv 将16位图像数据转为8位、8转16
Database storage - table partition
Sign and authenticate API interface or H5 interface
Pointer advanced, string function