3967 - F. Be Good at Gaussing

 Give you many positive integer N (N<=23), for each N, just output N*(N+1)/2 integers in a single line, separated by space. (Don't ask me why.) For each N, the output line contains integers from 1 to N, and each just once. Again, do not ask me why, thank you. I'm so busy. But I can tell you a secret, the output has relationship with number triangle. As:(N=3)

                   1

                   2   6

                   3   4   5

See the sample for more information.

题目输入

a positive integer N (N<=23). 

题目输出

For each N, output N*(N+1)/2 integers in a single line, separated by a blank space. 

输入/输出样例

题目输入

3
4
2
6

题目输出

1 2 6 3 4 5
1 2 9 3 10 8 4 5 6 7
1 2 3
1 2 15 3 16 14 4 17 21 13 5 18 19 20 12 6 7 8 9 10 11

C语言解答

#include<stdio.h>
#include<string.h>
int main( int argc, char **argv )
{
    int n;
    while( scanf( "%d", &n ) != EOF )
    {
        int array[n + 1][n + 1];
        memset( array, 0, sizeof( array ) );
        int count = 1;
        int i = 0, j = 0, k = 1;
        array[i][j] = 1;
        printf( "%d", array[i][j] );
        while( count < ( ( 1 + n ) * n / 2 ) )
        {
            while( i + 1 < n && !array[i + 1][j] )
                array[++i][j] = ++count;
            while( j + 1 < n && !array[i][j + 1] )
                array[i][++j] = ++count;
            while( i - 1 >= 0 && j - 1>= 0 && !array[i - 1][j - 1] )
                array[--i][--j] = ++count;
        }
        int u, v, ans = 1;
        for( u = 1; u < n; ++u )
        {
            for( v = 0; v <= u; ++v )
                printf( " %d", array[u][v] );
        }
        printf( "\n" );
    }
    return 0;

}

C++解答

#include<iostream>
using namespace std;
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<ctime>
const int maxN = 24;
int tri[maxN][maxN];
int main()
{
	int N, value, row, col, dir, i, j;
	while (cin >> N)
	{
		memset(tri, 0, sizeof(tri));
		row = col = 1;
		for (value = 1; value <= N; ++value)
			tri[row++][col] = value;
		row = N, col = 2;
		for (value = N + 1; value <= 2 * N - 1; ++value)
			tri[row][col++] = value;
		row = N - 1, col = N - 1;
		for (value = 2 * N; value <= 3 * N - 3; ++value)
			tri[row--][col--] = value;
		row = 2, col = 2;
		value = 3 * N - 2;
		dir = 1;
		while (value <= N*(N + 1) / 2)
		{
			if (1 == dir)/*down one line*/
			{
				if ((0 == tri[row + 1][col]))
					tri[++row][col] = value;
				else
				{
					dir = 2;
					tri[row][++col] = value;
				}
			}
			else if (2 == dir)/*right one line*/
			{
				if ((0 == tri[row][col + 1]))
					tri[row][++col] = value;
				else
				{
					dir = 3;
					tri[--row][--col] = value;
				}
			}
			else
			{
				if ((0 == tri[row - 1][col - 1]))
					tri[--row][--col] = value;
				else
				{
					dir = 1;
					tri[++row][col] = value;
				}
			}
			++value;
		}
		for (row = 1; row <= N; ++row)
		{
			for (col = 1; col <= row; ++col)
			{
				cout << tri[row][col];
				if (!(row == N&&col == row))
					cout << " ";
			}
		}
		cout << endl;
	}
	return 0;
}

时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题