알고리즘
C++ 피라미드
Chars4785
2019. 1. 7. 13:12
문제
N과 시작 숫자 S가 주어지면 숫자 피라미드를 만드는 프로그램을 작성하시오. 예를 들어, N이 5이고 S가 3 이라면, 그 숫자 피라미드는
3
456
21987
3456789
987654321
입력
입력의 첫 번째 줄에 N과 시작 숫자 S가 주어진다. ( 1≤N≤100, 1 ≤S≤ 9)
5 3
출력
첫 번째 줄부터 숫자 피라미드를 출력한다.
#include <iostream>
using namespace std;
int temp[9];
int main() {
int total,startNum;
int array[9]={1,2,3,4,5,6,7,8,9};
int reverse =-1;
int position =0;
int index =0;
cin>> total >> startNum;
position = startNum -1;
for(int i=0;i<total;i++)
{
for(int j=0;j<total-i-1;j++)
cout<<" ";
for(int j=0;j<2*i+1 ;j++)
{
temp[j]=array[position++];
if(position ==9)
position=0;
}
if(reverse > 0)
{
for(int j=0;j<2*i+1;j++)
cout<<temp[j];
}else{
for(int j=2*i;j>=0;j--)
cout<<temp[j];
}
reverse *= -1;
cout<< endl;
}
return 0;
}
-1을 계속 곱을 하면서 0 보다 크고 작음을 통해서 reverse 를 표현 했다.