코딩연습장
정올 - 숫자사각형4-2 #5932 [Java, C, C++, Python] 본문
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
반응형
'정올' 카테고리의 다른 글
정올 - 숫자 삼각형1 #5945 [Java, C, C++] (1) | 2024.05.29 |
---|---|
정올 - 숫자사각형4-3 #5933 [Java, C, C++, Python] (0) | 2024.05.28 |
정올 - 숫자사각형4-1 #5931 [Java, C, C++, Python] (0) | 2024.05.26 |
정올 - 숫자사각형2 #1856 [Java, C, C++, Python] (0) | 2024.05.25 |
정올 - 숫자사각형3 #1304 [Java, C, C++, Python] (1) | 2024.05.24 |