当前位置:网站首页>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;
}
边栏推荐
- idea里使用module项目的一个bug
- Selenium automation integration, eight years of testing experience, soft test engineer, an article to teach you
- 【踩坑】nacos注册一直连接localhost:8848,no available server
- opencv之图像分割
- RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
- 模拟卷Leetcode【普通】1705. 吃苹果的最大数目
- National SMS center number inquiry
- NCS Chengdu Xindian interview experience
- Redis summary
- LeetCode 715. Range 模块
猜你喜欢
Greenplum6.x重新初始化
Introduction to data fragmentation
Greenplum6.x-版本变化记录-常用手册
Greenplum 6.x reinitialization
联想混合云Lenovo xCloud:4大产品线+IT服务门户
xray的简单使用
[MySQL] detailed explanation of trigger content of database advanced
How to realize sliding operation component in fast application
NCS Chengdu New Electric interview Experience
How to integrate app linking services in harmonyos applications
随机推荐
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Find the original code, inverse code and complement of signed numbers [C language]
JS的操作
Greenplum 6.x build_ Environment configuration
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
xray的简单使用
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
Greenplum6.x-版本变化记录-常用手册
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
LeetCode 736. Lisp 语法解析
详解华为应用市场2022年逐步减少32位包体上架应用和策略
数据分析方法论与前人经验总结2【笔记干货】
Count sort (diagram)
opencv之图像分割
Leetcode 1984. Minimum difference in student scores
About using CDN based on Kangle and EP panel
Go write a program that runs within a certain period of time
ncs成都新電面試經驗