当前位置:网站首页>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;
} 
边栏推荐
- [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
- 说一个软件创业项目,有谁愿意投资的吗?
- 数据分片介绍
- Qt Charts使用(重写QChartView,实现一些自定义功能)
- Required String parameter ‘XXX‘ is not present
- Speaking of a software entrepreneurship project, is there anyone willing to invest?
- NCS Chengdu Xindian interview experience
- JS operation
- JS的操作
猜你喜欢

leetcode134. gas station

How to realize sliding operation component in fast application

ncs成都新电面试经验

LeetCode 715. Range module

Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error

数字三角形模型 AcWing 1027. 方格取数

【踩坑】nacos注册一直连接localhost:8848,no available server

Rapid integration of authentication services - harmonyos platform

NCS Chengdu Xindian interview experience

Greenplum 6.x version change record common manual
随机推荐
How to add a mask of a target in a picture
使用AGC重签名服务前后渠道号信息异常分析
Three series of BOM elements
联想混合云Lenovo xCloud:4大产品线+IT服务门户
Interpolation lookup (two methods)
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
如何在图片的目标中添加目标的mask
数据分片介绍
FPGA knowledge accumulation [6]
LeetCode 736. LISP syntax parsing
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
[Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
Greenplum6.x搭建_环境配置
opencv之图像分割
GoLand set goproxy
Rapid integration of authentication services - harmonyos platform
LeetCode 715. Range 模块
Newly found yii2 excel processing plug-in
实现自定义内存分配器