动态创建数组
动态创建数组,实现矩阵转置
1 #include<cstdio>
2
3 #include<iostream>
4
5 using namespace std;
6
7 int main()
8
9 {
10
11 int *a,n;
12
13 while(scanf("%d",&n)&&n)
14
15 {
16
17 a=new int[n*n+1];
18
19 for(int i=1;i<=n*n;i++)
20
21 scanf("%d",&a[i]);
22
23 for(int i=1;i<=n;i++)
24
25 {
26
27 for(int j=i;j<=n;j++)
28
29 {
30
31 int t=a[(i-1)*n+j];
32
33 a[(i-1)*n+j]=a[(j-1)*n+i];
34
35 a[(j-1)*n+i]=t;
36
37 }
38
39 }
40
41
42
43 for(int i=1;i<=n;i++)
44
45 {
46
47 for(int j=1;j<=n;j++)
48
49 printf("%d ",a[(i-1)*n+j]);
50
51 printf("\n");
52
53 }
54
55 delete[] a;
56
57 }
58
59 return 0;
60
61
62 }
2
3 #include<iostream>
4
5 using namespace std;
6
7 int main()
8
9 {
10
11 int *a,n;
12
13 while(scanf("%d",&n)&&n)
14
15 {
16
17 a=new int[n*n+1];
18
19 for(int i=1;i<=n*n;i++)
20
21 scanf("%d",&a[i]);
22
23 for(int i=1;i<=n;i++)
24
25 {
26
27 for(int j=i;j<=n;j++)
28
29 {
30
31 int t=a[(i-1)*n+j];
32
33 a[(i-1)*n+j]=a[(j-1)*n+i];
34
35 a[(j-1)*n+i]=t;
36
37 }
38
39 }
40
41
42
43 for(int i=1;i<=n;i++)
44
45 {
46
47 for(int j=1;j<=n;j++)
48
49 printf("%d ",a[(i-1)*n+j]);
50
51 printf("\n");
52
53 }
54
55 delete[] a;
56
57 }
58
59 return 0;
60
61
62 }
Keep it simple, stupid.