Algorithm/Cases-Study

SWEA-1225 암호생성기

Xi_kor 2021. 4. 4. 10:10

큐를 이용해서 풀었다! 1부터 5까지 감소하는게 한 사이클이므로 cnt라는 변수를 통해 5이상이 되면 다시 1로 바꿔주었다. 앞에 있는 수를 cnt만큼 빼주고 다시 push하는 과정을 했다.

#include <iostream>
#include <queue>
using namespace std;

int main(){
	for(int T=1; T<=10; T++){
		int tc;
		cin>>tc;
		queue <int> q;
		for(int i=0; i<8; i++){
			int a;
			cin>>a;
			q.push(a); 
		}
		int cnt=1;
		while(1){
			if(cnt>5) cnt=1;
			int x=q.front();
			q.pop();
			x=x-cnt;
			if(x<=0){
				x=0;
			}
			q.push(x);
			if(x<=0) break;
			cnt++;
		}
		
		cout<<"#"<<tc<<" ";
		while(!q.empty()){
			cout<<q.front()<<" ";
			q.pop();
		}
		cout<<'\n';
	}
	return 0;
}