`
joe_zxy
  • 浏览: 43352 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

POJ ACM习题【No.3173】

    博客分类:
  • ACM
阅读更多
Parkside's Triangle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3839 Accepted: 2301

Description

Bessie taught the cows to make Parkside's Triangle. It is generated from two numbers: the size and the seed. The size N (1 <= N <= 20) determines how many rows are in the triangle and the seed S (1 <= S <= 9) determines the first number in the triangle. Here are two examples:

 N=5, S=3                  N=6, S=1  



3 4 6 9 4                1 2 4 7 2 7    

  5 7 1 5                  3 5 8 3 8    

    8 2 6                    6 9 4 9    

      3 7                      1 5 1   

        8                        6 2  

                                   3
The first line of any triangle has no blanks at its front.

Analyze the above examples, discover the rule, and write a program that will generate Parkside's Triangle given any size N (1 <= N <= 20) and any seed S (1 <= S <= 9).

Input

Line 1: Two space-separated integers: N and S

Output

Lines 1..N: Parkside's Triangle as above; no trailing blanks on any line.

Sample Input

5 3

Sample Output

3 4 6 9 4
  5 7 1 5
    8 2 6
      3 7
        8

 

 

水题++

 

 

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		
		String[] str = cin.nextLine().split(" ");
		int N = Integer.valueOf(str[0]).intValue();
		int S = Integer.valueOf(str[1]).intValue();
			
		int[][] Tri = new int[N][N];
			
		for(int j = 0; j < N; j++)
		{
			for(int i = 0; i <= j; i++)
			{
				Tri[i][j] = S;
				
				if(S == 9)
					S = 1;
				else
					S++;
			}
		}
		print(Tri);
	}
	
	
	private static void print(int[][] tri)
	{
		for(int i = 0; i < tri.length; i++)
		{
			for( int j = 0; j < tri.length - 1; j++)
			{
				if(tri[i][j] < 1 || tri[i][j] > 9)
					System.out.print(" ");
				else
					System.out.print(tri[i][j]);
				System.out.print(" ");
				
			}
			System.out.println(tri[i][tri.length -1]);
		}
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics