开辟二维动态数组
Problem: Rotate matrix
Problem Description
there is a n*n matrix,your task is to rotate it k*90 degree clockwise.
Input
There are several testcases. For each case, the first line is n and k, and then is a n*n matrix.
1<=n<=1000 k>=0;
Output
A new matrix after rotated.
Sample Input
2 1
1 0
0 1
Sample Output
0 1
1 0
Source
NEUOJ-old
Hint
No Hint!
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,k,i,j;
int **Array;
while(~scanf("%d %d",&n,&k))
{
Array=(int **)malloc(n*sizeof(int *));
for(i=0; i<n; i++)
Array[i]=(int *)malloc(n*sizeof(int));
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&Array[i][j]);
}
}
if(k%4==0)
{
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d",Array[i][j]);
if(j!=n-1) printf(" ");
}
printf("\n");
}
}
else if(k%4==1)
{
for(i=0; i<n; i++)
{
for(j=n-1; j>=0; j--)
{
printf("%d",Array[j][i]);
if(j!=0) printf(" ");
}
printf("\n");
}
}
else if(k%4==2)
{
for(i=n-1; i>=0; i--)
{
for(j=n-1; j>=0; j--)
{
printf("%d",Array[i][j]);
if(j!=0) printf(" ");
}
printf("\n");
}
}
else if(k%4==3)
{
for(i=n-1; i>=0; i--)
{
for(j=0; j<n; j++)
{
printf("%d",Array[j][i]);
if(j!=n-1) printf(" ");
}
printf("\n");
}
}
}
return 0;
}
Problem: The Snakelike Matrix
Problem Description
There is a magical matrix called Snakelike Matrix.
As you can see,
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
is a 4*4 snakelike matrix.
Can you successfully get a snakelike matrix?
Input
Multiple test cases.Each test case contains an integer n (1<=n<=10) , means n*n matrix.
Output
Output the n*n snakelike matrix.
Sample Input
4
Sample Output
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
Source
NEUOJ-old
Hint
No Hint!
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n;
int **Array;
int i,j,t;
while(~scanf("%d",&n))
{
t=1;
Array=(int **)malloc(n*sizeof(int *));
for(i=0; i<n; i++)
Array[i]=(int *)malloc(n*sizeof(int));
for(i=0; i<n; i++)
{
if(i%2==0)
{
for(j=0; j<n; j++)
{
Array[i][j]=t++;
}
}
else
{
for(j=n-1; j>=0; j--)
{
Array[i][j]=t++;
}
}
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d",Array[i][j]);
if(j!=n-1) printf(" ");
}
printf("\n");
}
}
return 0;
}
Problem: The Screw Matrix
Problem Description
There is another magical matrix called Screw Matrix.
As you can see,
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
is a 4*4 screw matrix.
Can you successfully get a screw matrix?
Input
Multiple test cases.Each test case contains an integer n (1<=n<=10) , means n*n matrix.
Output
Output the n*n screw matrix.
Sample Input
4
Sample Output
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Source
NEUOJ-old
Hint
No Hint!
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;
int **Array;
int i,j;
int t,s,r,k;
while(~scanf("%d",&n))
{
Array=(int **)malloc(n*sizeof(int *));
for(i=0; i<n; i++)
Array[i]=(int *)malloc(n*sizeof(int));
i=0,j=-1,t=1;
s=1;
k=n;
while(s<=n*n)
{
for(r=0; r<k; r++)
{
j+=t;
Array[i][j]=s++;
}
for(r=k; r<2*k-1; r++)
{
i+=t;
Array[i][j]=s++;
}
k--;
t=-t;
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d",Array[i][j]);
if(j!=n-1) printf(" ");
}
printf("\n");
}
}
return 0;
}
c++开辟动态二维数组
int **p=new int *[n];
for(int i=0;i<n;i++)
{
p[i]=new int[n];
}