#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n, k, i;
int pos=0, cnt =0, kp=0;
cin >> n >> k; // 7, 3
int a[n+1]={0,}; // 0~7번 방 만들기, 배열 0으로 초기화
while(1){
pos+=1;
if(pos > n) pos = pos-n; // 배열을 넘으면 다시 되돌아 가기
if(a[pos]==0) ++kp;
if(k == kp){
a[pos] = 1 ; // k번째라면 제외 시키기
cnt += 1;
cout << pos ;
kp=0; //초기화
}
if(cnt == n) break; // 7명 다 체크했음
}
}
답이 3627514 라고 나오긴 하는데
백준은 <3, 6, 2, 7, 5, 1, 4> 요구하고 있다.
따라서 번거롭긴 하지만 괄호와 마지막일 때 flag를 조건에 넣어주자.
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n, k, i;
int pos=0, cnt =0, kp=0;
int flag = 0;
cin >> n >> k; // 7, 3
//int a[20] = {0,} // 다른 컴파일러에서 에러날 때 배열 사이즈를 정해주면 된다.
int a[n+1]={0,}; // 0 ~ 7번 방 만들기, 배열 0으로 초기화
cout << "<";
while(1){
pos+=1;
if(pos > n) pos = pos-n; // 배열을 넘으면 다시 되돌아 가기
if(a[pos]==0) ++kp;
if(k == kp){
a[pos] = 1 ; // k번째라면 제외 시키기
cnt += 1;
if(cnt == n) flag =1; //7명 다 체크했음
cout << pos ;
kp=0; //초기화
if (flag == 1) break;
else cout << ", ";
}
}
cout << ">";
}
'CPP 문제풀이 > 백준' 카테고리의 다른 글
백준 10808번 알파벳 개수 (0) | 2020.08.26 |
---|---|
백준 2609번 : 최대공약수와 최소공배수 (0) | 2020.08.24 |
백준 11650번 : 좌표 정렬하기 (0) | 2020.08.23 |
백준 2750번, 2751번 : 수 정렬하기 (0) | 2020.08.20 |
15552번(빠른 A+B) (0) | 2019.09.14 |