C语言开辟动态二维数组

开辟二维动态数组
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];
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值