-
별출력_220805데일리 codeup/다중 반복문 2022. 7. 5. 18:55
사각형 별 출력
정사각형 별표 출력하기
정수 n의 값을 입력받아 별표로 정사각형을 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cout<<"*"; } cout<<"\n"; } return 0; }
직사각형 별표 출력하기
정수 n과 m의 값을 입력받아 별표로 이루어진 직사각형을 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
#include <iostream> using namespace std; int main(){ int n,m; cin >> n >> m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++) cout<<"* "; cout<<"\n"; } return 0; }
정사각형 두 개 출력
정수 n이 주어졌을 때, 다음과 같은 형식으로 * 로 이루어진 n x n 크기의 정사각형을 두 개 출력합니다.
#include <iostream> using namespace std; int main(){ int n; cin >> n; for (int sq=0; sq<2; sq++){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++) cout<<"*"; cout<<"\n"; } cout<<"\n"; } return 0; }
직각 삼각형
별표 출력하기 2
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=n;i>0;i--){ for(int j=0;j<i;j++) cout<<"* "; cout<<"\n"; } return 0; }
별표 출력하기 7
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=n;i++){ for(int j=0;j<i;j++) cout<<"* "; cout<<"\n"; } return 0; }
별표 출력하기 5
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 2를 입력받는 경우
** ** *
n에 3을 입력받는 경우
*** *** *** ** ** *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=n;i>0;i--){ for(int j=0;j<i;j++){ for(int k=0;k<i;k++) cout<<"*"; cout<<' '; } cout<<"\n"; } return 0; }
직각 삼각형 출력
정수 n이 주어졌을 때, 아래 예를 참고하여 * 로 이루어진 직각삼각형을 출력하는 프로그램을 작성해보세요.
예) n = 3 일 때
* *** *****
예) n = 5 일 때
* *** ***** ******* *********
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=0;i<n;i++){ for(int j=0;j<2*i+1;j++){ cout<<"*"; } cout<<"\n"; } return 0; }
행에 대하여 대칭인 경우
별표 출력하기
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
예)
n에 2를 입력받는 경우
* * * *
n에 3을 입력받는 경우
* * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=2*n-1;i++){ if(i<=n) for(int j=1;j<=i;j++) cout<<"* "; else for(int k=1;k<=2*n-i;k++) cout<<"* "; cout<<endl; } return 0; }
별표 출력하기 4
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 2를 입력받는 경우
* * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1; i<=2*n-1; i++){ if(i<=n) for(int j=1; j<=n-i+1;j++) cout<<"* "; else for(int k=0; k<=i-n; k++) cout<<"* "; cout<<endl; } return 0; }
건너뛰고 별표 출력하기
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 4를 입력받는 경우
* ** *** **** *** ** *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) cout<<"*"; cout<<"\n\n"; } for(int k=n-1;k>0;k--){ for(int l=1;l<=k;l++) cout<<"*"; cout<<"\n\n"; } return 0; }
열에 대한 대칭인 별 출력
별표 출력하기 3
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 2를 입력받는 경우
* * * *
n에 3을 입력받는 경우
* * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=0;i<n;i++){ for(int j=0;j<i;j++) cout<<" "; for(int k=0;k<2*(n-i)-1;k++) cout<<"* "; cout<<endl; } return 0; }
별표 출력하기 9
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 3를 입력받는 경우
* * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=n-1;i>=0;i--){ for(int j=i;j>0;j--) cout<<" "; for(int k=1;k<=2*(n-i)-1;k++) cout<<"* "; cout<<endl; } return 0; }
두 개의 직각삼각형
정수 n이 주어졌을 때, 아래 예를 참고하여 * 로 이루어진 직각삼각형 대칭으로 2 개 출력하는 프로그램을 작성해보세요.
예) n = 3 일 때
****** ** ** * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=0;i<n;i++){ for(int j=0;j<n-i;j++) cout<<"*"; for(int k=0;k<2*i;k++) cout<<" "; for(int l=0;l<n-i;l++) cout<<"*"; cout<<endl; } return 0; }
행,열에 대해 대칭인 별 출력
별표 출력하기 6
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 2를 입력받는 경우
* * * * * * *
n에 3을 입력받는 경우
* * * * * * * * * * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=2*n-1;i++){ for(int j=1;j<n-abs(n-i);j++) cout<<" "; for(int k=1;k<=2*abs(n-i)+1;k++) cout<<"* "; cout<<endl; } return 0; }
별 그리기
가로 세로 2n-1 크기에 해당하는 격자에 다이아몬드 모양을 *로 그리는 코드를 작성해보세요.
규칙은 다음과 같습니다.
n = 2 * *** * n = 3 * *** ***** *** *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1; i<=2*n-1; i++){ for(int j=0; j<abs(n-i); j++) cout<<" "; for(int k=1; k<=2*(n-abs(n-i))-1;k++) cout<<"*"; cout<<endl; } return 0; }
특정 규칙에 따른 문자 출력
정수 n을 입력받아 문자를 특정 규칙에 따라 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 2를 입력받는 경우
@ @ @ @
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=2*n-1;i++){ if(i<n) for(int j=1;j<=(n-i);j++) cout<<" "; for(int k=1;k<=n-abs(n-i);k++) cout<<"@ "; cout<<endl; } return 0; }
체크 다이아몬드
가로 세로 2n-1 크기에 해당하는 격자에 체크 다이아몬드 모양을 그리는 코드를 작성해보세요.
규칙은 다음과 같습니다.
n = 3 일 때 * * * * * * * * * n = 4 일 때 * * * * * * * * * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=2*n-1;i++){ for(int j=abs(n-i);j>0;j--) cout<<" "; for(int k=1;k<=n-abs(n-i);k++) cout<<"* "; cout<<endl; } return 0; }
행에 따라 다른 별 출력
별표 출력하기 8
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요. 홀수번째 줄에는 '*'이 하나만 출력됩니다.
n에 3를 입력받는 경우 * * * * n에 4을 입력받는 경우 * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=n;i++){ if(i%2==1) cout<<"*"; else for(int j=1;j<=i;j++) cout<<"* "; cout<<endl; } return 0; }
별표 출력하기 10
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 3를 입력받는 경우
* * * * * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=2*n;i++){ if(i%2==1) for(int j=1;j<=i/2+1;j++) cout<<"* "; else for(int k=1; k<=n-i/2+1;k++) cout<<"* "; cout<<endl; } return 0; }
별표 출력하기 13
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 3를 입력받는 경우 * * * * * * * * * * * * n에 4을 입력받는 경우 * * * * * * * * * * * * * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1;i<=2*n;i++){ if(i%2==0) for(int j=1;j<i/2+1;j++) cout<<"* "; else for(int k=1; k<n-i/2+1;k++) cout<<"* "; cout<<endl; } return 0; }
모양과 관계 없는 별 출력
별표 출력하기
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 1를 입력받는 경우 * * * * * * * * n에 2을 입력받는 경우 * * * * * * * * * * * * * * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1; i<=2*n+1 ;i++){ if(i%2==1) for(int j=0;j<2*n+1;j++) cout<<"* "; else for(int k=0;k<=n;k++) cout<<"* "; cout<<endl; } return 0; }
별표 출력하기 12
정수 n의 값을 입력받아 별표를 출력하는 프로그램을 아래 예를 참고하여 작성해보세요.
n에 4를 입력받는 경우 * * * * * * * * n에 5를 입력받는 경우 * * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for(int i=1; i<=n; i++){ if(i==1) for(int j=0;j<n;j++) cout<<"* "; else{ cout<<" "; for(int k=0; k<(i-1)/2; k++) cout<<" "; for(int l=0; l<=n/2 - (i+1)/2; l++) cout<<"* "; } cout<<endl; } return 0; }
규칙을 찾아 출력하기
정수 n이 주어졌을 때, 다음과 같은 규칙을 가진 모양을 출력하는 프로그램을 작성해보세요.
n = 4 일 때 * * * * * * * * * * * * * n = 5 일 때 * * * * * * * * * * * * * * * * * * *
#include <iostream> using namespace std; int main(){ int n; cin >> n; for (int i=0;i<n;i++){ if(i==0) for(int j=1;j<n;j++) cout<<"* "; else{ for(int k=0;k<i;k++) cout<<"* "; for(int l=0;l<(n-i-1);l++) cout<<" "; } cout<<"* "<<endl; } return 0; }
'데일리 codeup > 다중 반복문' 카테고리의 다른 글
다중반복 숫자출력2_220707 (0) 2022.07.07 다중반복 숫자출력_220706 (0) 2022.07.06