当前位置:网站首页>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;
}
边栏推荐
- LeetCode 715. Range 模块
- 【MySQL】数据库进阶之触发器内容详解
- About using CDN based on Kangle and EP panel
- [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- 测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
- Other 7 features of TCP [sliding window mechanism ▲]
- [step on the pit] Nacos registration has been connected to localhost:8848, no available server
- 如何在HarmonyOS应用中集成App Linking服务
- 对API接口或H5接口做签名认证
- How to realize sliding operation component in fast application
猜你喜欢
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
Greenplum 6.x reinitialization
Greenplum6.x常用语句
Greenplum 6.x common statements
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
LeetCode 736. Lisp 语法解析
Novice entry SCM must understand those things
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
数字三角形模型 AcWing 1027. 方格取数
Image segmentation in opencv
随机推荐
Required String parameter ‘XXX‘ is not present
About using CDN based on Kangle and EP panel
A bug using module project in idea
NCS Chengdu New Electric interview Experience
Tips for using jeditabletable
Platformization, a fulcrum of strong chain complementing chain
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
Category of IP address
为什么要选择云原生数据库
PPT模板、素材下载网站(纯干货,建议收藏)
如何在图片的目标中添加目标的mask
Opencv converts 16 bit image data to 8 bits and 8 to 16
指针进阶,字符串函数
Greenplum6.x常用语句
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
LeetCode 715. Range module
使用AGC重签名服务前后渠道号信息异常分析
Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
阿里p8手把手教你,自动化测试应该如何实现多线程?赶紧码住
Three usage scenarios of annotation @configurationproperties