【2051】数字方阵

Time Limit: 1 second
Memory Limit: 2 MB

问题描述

输入n(1<=n<=10)个整数,显示如下图形。例如n=5时,输入的数据为25,3,10,256,8。打印的方阵如下:

253102568

825310256

256825310

102568253

310256825

    
 

Input

输入只有两行,第一行一个整数n,第二行n个整数,用空格分隔。其中n个整数都在一般整数范围内

Output

输出有n行。

Sample Input

5

25 3 10 256 8

Sample Output

253102568

825310256

256825310

102568253

310256825

【题解】

可以用一个头指针f,尾指针t来控制输出,只要记录一行就可以,while f != t,就一直f++,输出a[f],f = n+1 时 f = 1,每输出一行 就让f-- t--,f t  < 1时 f = n  t = n;

具体的看代码实现 和注释。

【代码】

#include <cstdio>

int n,a[11]; //只要用一维数组来记录输入的数据就可以了

void input_data()
{
    scanf("%d",&n);
    for (int i = 1;i <= n;i++)
        scanf("%d",&a[i]);
}

void output_ans()
{
    int f = 1,t = n;
    for (int i = 1;i <= n;i++) //输出有n行
        {
            int tf = f, tt = t; //用tf和tt来控制输出
            while (tf != tt)
                {
                    printf("%d",a[tf]);  //输出当前所到的位置
                    tf++;
                    if (tf > n) //如果大于n就变成1
                        tf = 1;
                }
            printf("%d",a[tt]); //还有一个会没输出
            f--;
            if (f < 1) f = n;
            t--;
            if (t < 1) t = n; 
            printf("
");
        }
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    input_data();
    output_ans();
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7632458.html