#include <iostream>
#include <string>
using namespace std;
int main() {
int N;
cin>>N;
string ar;
int M=1;
int Q=4;
for(int s=0;s<N;s++){
ar.clear();
for(int t=0;t<Q;t++){
ar+=" ";
Q--;
}
for(int t=0;t<M;t++){
ar+="*";
M+=2;
}
cout<<ar<<endl;
}
M-=2;
Q+=1;
for(int s=1;s<N;s++){
ar.clear();
for(int t=0;t<Q;t++){
ar+=" ";
Q++;
}
for(int t=0;t<M;t++){
ar+="*";
M-=2;
}
cout<<ar<<endl;
}
return 0;
}
이렇게 함
메모리 초과임
줄여야 함
#include <iostream>
#include <string>
using namespace std;
int main() {
int N;
cin>>N;
string ar[199];
int M=1;
int Q=N-M;
int s,t;
for(s=0;s<N;s++){
for(t=0;t<Q;t++){
ar[s]+=" ";
Q--;
}
for(t=0;t<M;t++){
ar[s]+="*";
M+=2;
}
cout<<ar[s]<<endl;
}
for(s=N-2;s>=0;s--){
cout<<ar[s]<<endl;
}
return 0;
}
음 얘도 실패
아래는 모범 답안
#include <iostream>
using namespace std;
int main() {
int N=0;
cin>>N;
for(int i=1;i<=N;i++){
for(int j=N-i;j>0;j--)
cout<<" ";
for(int j=2*i-1;j>0;j--){
cout<<"*";
}
cout<<endl;
}
for(int i=1;i<N;i++){
for(int j=0;j<i;j++){
cout<<" ";
}
for(int j=2*(N-i)-1;j>0;j--){
cout<<"*";
}
cout<<endl;
}
return 0;
}'coding > baekjoon: cpp' 카테고리의 다른 글
| 정렬 시간복잡도 (0) | 2024.01.08 |
|---|---|
| [CPP] 백준 27866 문자와 문자열 (0) | 2024.01.08 |
| [CPP] 백준 3003 킹, 퀸, 룩, 비숍, 나이트, 폰 (0) | 2024.01.06 |
| [CPP] 백준 1546 평균 (0) | 2024.01.06 |
| [CPP] 백준 10811 바구니 뒤집기 (0) | 2024.01.06 |
