3967 - F. Be Good at Gaussing

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

 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;
}

Java解答


import java.util.Scanner;

class data {
	int dig;
	public data(int a) {
		dig = a;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			int n = in.nextInt();
			method(n);
		}
	}

	private static void method(int n) {
		// TODO Auto-generated method stub
		int sum = n * (n + 1) / 2;
		data[][] arr = new data[n][n];
		int count = 1;
		for (int i = 0; i < arr.length; i++) {
			arr[i][0] = new data(count);
			count++;
		}
		for (int i = 1; i < arr[0].length; i++) {
			arr[n - 1][i] = new data(count);
			count++;
		}
		int L = arr[0].length - 1;
		int H = arr.length - 1;
		While: while (count <= sum) {
			// 1.向左上循环
			for (; arr[--H][--L] == null;) {
				arr[H][L] = new data(count);
				count++;
				if (count > sum)
					break While;
			}
			L++;
			H++;
			// 2.单向下循环
			for (; arr[++H][L] == null;) {
				arr[H][L] = new data(count);
				count++;
				if (count > sum)
					break While;
			}
			H--;
			// 3.单向右循环
			for (; arr[H][++L] == null;) {
				arr[H][L] = new data(count);
				count++;
				if (count > sum)
					break While;
			}
			L--;

		}
		// 遍历;
		for (int i = 0; i < arr.length; i++) {
			for (int k = 0; k < arr[0].length; k++) {
				if (arr[i][k] != null){
					if(i==arr.length-1&&k==arr[0].length-1)
						System.out.println(arr[i][k].dig);
					else
						System.out.print(arr[i][k].dig + " ");
				}
					
				
			}
		}
		

	}
}