C语言二维数组元素怎么“顺时针旋转”,如图
展开全部
#include"stdio.h"
int main()
{int i,j,n,t,a[20][20];
int c;
scanf("%d",&n);
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{a[i][j]=n*i+j+1;
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("After:\n");
for(i=0;i<n-1;i++)
for(j=i;j<n-1-i;j++)
{t=a[i][j];
a[i][j]=a[n-1-j][i];
a[n-1-j][i]=a[n-1-i][n-1-j];
a[n-1-i][n-1-j]=a[j][n-1-i];
a[j][n-1-i]=t;
}
for(i1=0;i1<n;i1++)
{for(j1=0;j1<n;j1++)
printf("%3d",a[i1][j1]);
printf("\n");
}
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//关键是理解表达式b[i][j] = a[2 - j][i];
//我用的c++,只有输入和输出和c不同,逻辑一样
#include<iostream>
using namespace std;
int main() {
int a[3][3] = { {1,2,3} ,{4,5,6}, {7,8,9} };
cout << "转化之前的数组为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
int b[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//逆时针b[i][j] = a[j][2 - i];
b[i][j] = a[2 - j][i];
}
}
cout << "转化之后的数组为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
}
//我用的c++,只有输入和输出和c不同,逻辑一样
#include<iostream>
using namespace std;
int main() {
int a[3][3] = { {1,2,3} ,{4,5,6}, {7,8,9} };
cout << "转化之前的数组为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
int b[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//逆时针b[i][j] = a[j][2 - i];
b[i][j] = a[2 - j][i];
}
}
cout << "转化之后的数组为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
}
追问
十分感谢大佬
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询