오늘의 문제는 “10886번 0 = not cute / 1 = cute”.
숫자를 홀수 번 받아서 1이 더 많은지 0이 더 많은지를 판별하는 간단한 문제이다.
간단한 문제라 별 생각없이 풀었는데….
틀렸다.
#include <iostream>
using namespace std;
int main()
{
int n, x;
int cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) //n번 숫자를 입력받아서
{
cin >> n;
if (n == 0) //0을 받으면 cnt를 줄이고, 1을 받으면 cnt를 올린다
{
cnt--;
}
else
{
cnt++;
}
}
if (0 < cnt) //cnt의 값에 따라 출력한다
{
cout << "Junhee is cute!\n";
}
else
{
cout << "Junhee is not cute!\n";
}
return 0;
}알고보니 if문 안의 조건식을 틀렸다…
엄밀히 말하면 if문 안의 조건식 전에 숫자를 n으로 입력받은 것이 틀렸다.
숫자를 받기 위한 다른 변수 x까지 선언했는데, 무의식 중에 n으로 입력받은 것이다.
#include <iostream>
using namespace std;
int main()
{
int n, x;
int cnt = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x;
if (x == 0)
{
cnt--;
}
else
{
cnt++;
}
}
if (0 < cnt)
{
cout << "Junhee is cute!\n";
}
else
{
cout << "Junhee is not cute!\n";
}
return 0;
}n을 x로 고쳐서 x로 입력받으니 맞았다.
간단한 문제일수록 간단하게 틀리기 쉬운 것 같다.