자기개발/코딩테스트
C++ (코딩테스트) - 코코넛 그 두 번째 이야기(백준)
pi92
2023. 9. 22. 10:59
문제
https://www.acmicpc.net/problem/6417
6417번: 코코넛 그 두 번째 이야기
각 N에 따라 한 줄에, K가 존재할 경우 "N coconuts, max(K) people and 1 monkey" 를, 어떤 K도 코코넛을 규칙대로 나눌 수 없을 경우 "N coconuts, no solution"을 출력한다.
www.acmicpc.net
내가 쓴 코드
#include <iostream>
#include <cmath>
using namespace std;
int main(void) {
int N;
string command;
while (true)
{
cin >> N;
if(N == -1) break;
int maxK = 0;
for (int i = 2; i < sqrt(N)+1; i++)
{
int temp = N;
bool chk = true;
for (int j = 0; j < i; j++)
{
temp -= 1;
if (temp % i == 0)
{
temp = (temp * (i - 1)) / i;
}
else
{
chk = false;
break;
}
}
if (chk && temp % i == 0) maxK = i;
}
if (maxK < 2)
{
cout << N << " coconuts, no solution" << endl;
}
else
{
cout << N << " coconuts, "<< maxK <<" people and 1 monkey" << endl;
}
}
return 0;
}