CS/알고리즘_문제풀이(자바)

array 3

Jedy_Kim 2021. 8. 26. 13:13
728x90

문제

N이 주어질 때, 다음과 같은 프로그램을 작성해보자.  

입력

첫째 줄에 자연수 N이 주어진다.(1<=N<=100)

 

출력

예시를 참고하여 작성하자.

 

예제 입력

3

예제 출력

1 2 4
3 5

예제 입력

5

예제 출력

1 2 4 7 11
3 5 8 12 
6 9 13 
10 14
15 

 

#코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
import java.io.*;
 
public class Main{
    public static void main(String[] args) throws Exception{
 
      // Please Enter Your Code Here
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st = new StringTokenizer(br.readLine());
      
      int N = Integer.parseInt(st.nextToken()); 
      int[][] arr = new int[N][];
      
      int idx = 0;
      int startVal = 1;
      for(int i = N; i > 0; i--) {
        arr[idx] = new int[i];
        arr[idx][0= startVal; 
        idx++;
        startVal += (idx+1);
      }
      
      startVal = 1;
      for(int i = 0; i < arr.length; i++) {
        int increaseVal = startVal;
        System.out.print(arr[i][0+ " ");
        for(int j=1; j < arr[i].length; j++) {
          arr[i][j] = arr[i][j-1+ increaseVal;
          System.out.print(arr[i][j] + " ");
          increaseVal++;
        }
        startVal++;
        System.out.println("");
      } 
 
    }
}
cs

 

반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

검증수  (0) 2021.08.27
car  (0) 2021.08.27
array 2  (0) 2021.08.26
array 1  (0) 2021.08.26
숫자 피라미드  (0) 2021.08.25