【2012】建立二维矩阵

 


Time Limit: 3 second
Memory Limit: 2 MB

一维数组有6个整数,数组元素分别为 7 4 8 9 1 5 要求建立一个如下数组(矩阵):

7   4   8   9   1   5
4   8   9   1   5   7
8   9   1   5   7   4 
9   1   5   7   4   8 
1   5   7   4   8   9  
5   7   4   8   9   1 

Input

没有输入文件

Output

输出文件有6行。

7   4   8   9   1   5
4   8   9   1   5   7
8   9   1   5   7   4 
9   1   5   7   4   8 
1   5   7   4   8   9  
5   7   4   8   9   1 

【题解】

再把原序列往后复制1遍。即a[7..12]也存着a[1..6]的原序列。

【代码】

 

#include <cstdio>

const int MAXN = 6;

int a[MAXN+5];

void output_ans()
{
    a[1] = 7;a[2] = 4; a[3] = 8; a[4] =9; a[5] = 1; a[6] =5;
    for (int i = 7; i <= 12;i++) //把原来的序列复制一遍。
        a[i] = a[i-6];
    for (int i = 1;i <= 6;i++)
        {
            for (int j = i; j <= i+4;j++)
                printf("%d   ",a[j]);
            printf("%d\n",a[i+5]);
        }
}

int main()
{
    output_ans();
    return 0;
}


posted @ 2017-10-06 19:23  AWCXV  阅读(221)  评论(0编辑  收藏  举报