반응형
250x250
Notice
Recent Posts
«   2025/05   »
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
관리 메뉴

코딩연습장

정올 - 숫자사각형4-2 #5932 [Java, C, C++, Python] 본문

정올

정올 - 숫자사각형4-2 #5932 [Java, C, C++, Python]

감귤짱 2024. 5. 27. 09:00
728x90
반응형

숫자사각형4-2 #5932 

 

[ 문제 ]

정사각형의 한 변의 길이 n을 입력받은 후 다음과 같은 정사각형 형태로 출력하는 프로그램을 작성하시오.

 

 

[ 입력 ]

정사각형 한 변의 길이 n을 입력받는다. (1 ≤ n ≤ 100)

 

[ 출력 ]

형식에 맞춰 출력한다. 숫자 사이는 공백으로 구분하여 출력한다.

 

[ 예제 ]

입력 2 4
출력 1 2
2 1
1 2 3 4
4 3 2 1
1 2 3 4
4 3 2 1

 

[ 출처 ]

JUNGOL

 

728x90
반응형
 

 

Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = 0;
        for(int i=0;i<n;i++) {
            for(int j=0;j<n;j++) {
                if(i%2==0) {
                    a++;
                    System.out.print(a + " ");
                }else {
                    System.out.print(a + " ");
                    a--;
                }
            }
            System.out.println();
        }
    }
}

 

C

#include <stdio.h>

int main(){
    int n;
    scanf("%d", &n);
    int a = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i%2==0){
                a++;
                printf("%d ", a);
            }else{
                printf("%d ", a);
                a--;
            }
        }
        printf("\n");
    }
    return 0;
}

 

C++

#include <iostream>

using namespace std;

int main(){
    int n;
    cin >> n;
    int a = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i%2==0){
                a++;
                cout << a << " ";
            }else{
                cout << a << " ";
                a--;
            }
        }
        cout << endl;
    }
    return 0;
}

 

Python

n = int(input())
a = 0
for i in range(n):
    for j in range(n):
        if i%2==0:
            a += 1
            print(a, end=" ")
        else:
            print(a, end=" ")
            a -= 1
    print()

 

 

 

https://jungol.co.kr/problem/5932

 

문제 - JUNGOL

history 최근 본 문제

jungol.co.kr

 

728x90
반응형