-
다중반복 숫자출력_220706데일리 codeup/다중 반복문 2022. 7. 6. 13:24
행 단위로 반복하여 출력
정수 n의 값이 주어지면 행 단위로 반복하여 출력하는 프로그램을 작성해보세요.
1 ≤ n ≤ 9
#include<iostream> using namespace std; int main(){ int n; cin >> n; for (int i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<j; cout<<endl; } return 0; }
열 단위로 반복하여 출력
정수 n의 값이 주어지면 열 단위로 반복하여 출력하는 프로그램을 작성해보세요.
#include<iostream> using namespace std; int main(){ int n; cin >> n; for (int i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<i; cout<<endl; } return 0; }
순서쌍 만들기
정수 n을 입력받아 (n,n) 부터 (1,1)까지의 순서쌍을 만드는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 1을 입력 받는 경우 (1,1) n에 2를 입력 받는 경우 (2,2) (2,1) (1,2) (1,1)
#include<iostream> using namespace std; int main(){ int n; cin >> n; for (int i=n;i>0;i--){ for(int j=n;j>0;j--) cout<<'('<<i<<','<<j<<')'<<' '; cout<<endl; } return 0; }
열에 따라 감소하는 정사각형
정수 n이 주어지면 다음과 같은 정사각형 모양을 각 행마다 1씩 감소하며 출력하는 프로그램을 작성해보세요.
예) n = 4
4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1#include<iostream> using namespace std; int main(){ int n; cin >> n; for (int i=n;i>0;i--){ for(int j=n;j>0;j--) cout<<j<<' '; cout<<endl; } return 0; }
행과 열
행과 열의 수를 a, b로 입력받아 출력하는 프로그램을 작성해보세요.
입력
4 5
출력
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20
#include<iostream> using namespace std; int main(){ int a,b; cin >> a >> b; for (int i=1;i<=a;i++){ for(int j=1;j<=b;j++) cout<<i*j<<' '; cout<<endl; } return 0; }
홀수로 이루어진 정사각형
정수 n의 값을 입력받아 두자리 홀수로 이루어진 n * n 정사각형을 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n이 3인 경우
11 13 15 13 15 17 15 17 19
n이 4인 경우
11 13 15 17 13 15 17 19 15 17 19 21 17 19 21 23
#include<iostream> using namespace std; int main(){ int n; cin >> n; for (int i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<7+2*(i+j)<<' '; cout<<endl; } return 0; }
좌우반전시킨 정사각형
정수 n이 주어지면 다음과 같은 정사각형 모양을 좌우반전시켜 출력하는 프로그램을 작성해보세요.
입력
4
출력
4 3 2 1 8 6 4 2 12 9 6 3 16 12 8 4
#include<iostream> using namespace std; int main(){ int n; cin >> n; for (int i=1;i<=n;i++){ for(int j=n;j>0;j--) cout<<i*j<<' '; cout<<endl; } return 0; }
정사각형 출력
자연수 n이 주어집니다. 1부터 n * n까지 다음 규칙에 따라 출력하는 프로그램을 작성해보세요.
입력
4
출력
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream> using namespace std; int main(){ int n; cin >> n; for (int i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<n*(i-1)+j<<' '; cout<<endl; } return 0; }
1-9 정사각형
정수 n의 값이 주어지면 일의 자리 숫자로 이루어진 n*n모양 사각형을 출력하는 프로그램을 작성해보세요. 9다음에는 1이 나와야 합니다.
입력
4
출력
1234 5678 9123 4567
#include<iostream> using namespace std; int main(){ int n,cnt = 0,num = 1; cin >> n; while(cnt<n*n){ if(num==10) num=1; cout<<num; cnt++; num++; if(cnt%n==0) cout<<endl; } return 0; }
모양대로 숫자 출력하기 2
정수 n의 값을 입력받아 다음과 같이 n x n 크기의 숫자를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요. 모든 숫자는 10 미만의 짝수로 이루어져 있습니다.
n에 2를 입력받는 경우 2 4 6 8 n에 3을 입력받는 경우 2 4 6 8 2 4 6 8 2
#include<iostream> using namespace std; int main(){ int n,cnt = 2; cin >> n; for (int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(cnt == 10) cnt = 2; cout<<cnt<<' '; cnt +=2; } cout<<endl; } return 0; }
9-1 정사각형
정수 n이 주어졌을 때, n x n 모양의 정사각형을 출력하는데, 9부터 1까지 1씩 감소하며 출력하는 프로그램을 작성해보세요. 단, 1 다음에는 다시 9에서 시작하여 1씩 줄어드는 것을 반복합니다.
입력
4
출력
9876 5432 1987 6543
#include<iostream> using namespace std; int main(){ int n,cnt = 9; cin >> n; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(cnt == 0) cnt = 9; cout<<cnt; cnt--; } cout<<endl; } return 0; }
좌우로 반복하며 출력
정수 n의 값이 주어지면 n값에 따라 일의 자리 숫자를 좌우로 반복하며 출력하는 프로그램을 작성해보세요.
#include<iostream> using namespace std; int main(){ int n,cnt = 9; cin >> n; for(int i=0;i<n;i++){ if(i%2==0) for(int j=1;j<=n;j++) cout<<j; else for(int k=n;k>0;k--) cout<<k; cout<<endl; } return 0; }
상하로 반복하며 출력
정수 n의 값이 주어지면 n값에 따라 일의 자리 숫자를 상하로 반복하며 출력하는 프로그램을 작성해보세요.
입력
4
출력
1414 2323 3232 4141
#include<iostream> using namespace std; int main(){ int n,cnt = 9; cin >> n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(j%2==1) cout<<i; else cout<<n-i+1; } cout<<endl; } return 0; }
좌우 지그재그 출력
자연수 n이 주어집니다. 1부터 n * n까지 다음 규칙에 따라 출력하는 프로그램을 작성해보세요.
입력
3
출력
1 2 3 6 5 4 7 8 9
#include<iostream> using namespace std; int main(){ int n,cnt = 9; cin >> n; for(int i=1;i<=n;i++){ if(i%2==1) for(int j=1;j<=n;j++) cout<<n*(i-1)+j<<' '; else for(int k=n;k>0;k--) cout<<n*(i-1)+k<<' '; cout<<endl; } return 0; }
열에 따라 다르게 출력
자연수 n이 주어집니다. 1부터 다음 규칙에 따라 출력하는 프로그램을 작성해보세요. 규칙은 홀수 번째 줄에는 1씩 증가하고, 짝수 번째 줄에는 2씩 증가하는 것입니다.
입력
4
출력
1 2 3 4 6 8 10 12 13 14 15 16 18 20 22 24
#include<iostream> using namespace std; int main(){ int n,cnt = 0; cin >> n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i%2==1) cnt++; else cnt+=2; cout<<cnt<<' '; } cout<<endl; } return 0; }
'데일리 codeup > 다중 반복문' 카테고리의 다른 글
다중반복 숫자출력2_220707 (0) 2022.07.07 별출력_220805 (0) 2022.07.05