코드 2개를 올려주시고요. 문제 번호도 적어주세요..
이렇게만 올려주시면 왜 틀렸는지 저도 모르겠어요.
https://www.acmicpc.net/proble...
문제는 이거네요..
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdlib>
using namespace std;
int n;
long long koong[67];
int T;
int main()
{
std::ios_base::sync_with_stdio(false);
cin >> T;
koong[0] = 1;
koong[1] = 1;
koong[2] = 2;
koong[3] = 4;
for (int i = 0;i < T;i++)
{
cin >> n;
for (int j = 4;j <= n;j++)
{
koong[j] = koong[j - 1] + koong[j - 2] + koong[j - 3] + koong[j - 4];
}
if (n < 2)
{
cout << 1 << endl;
}
else if (n == 2)
{
cout << 2 << endl;
}
else if (n == 3)
{
cout << 4 << endl;
}
else
{
cout << koong[n] << endl;
}
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdlib>
using namespace std;
int n;
long long koong[67];
int T;
int main()
{
std::ios_base::sync_with_stdio(false);
cin >> T;
koong[0] = 1;
koong[1] = 1;
koong[2] = 2;
koong[3] = 4;
for (int j = 4;j <= 67;j++)
{
koong[j] = koong[j - 1] + koong[j - 2] + koong[j - 3] + koong[j - 4];
}
for (int i = 0;i < T;i++)
{
cin >> n;
if (n < 2)
{
cout << 1 << endl;
}
else if (n == 2)
{
cout << 2 << endl;
}
else if (n == 3)
{
cout << 4 << endl;
}
else
{
cout << koong[n] << endl;
}
}
return 0;
}
사실 위의 코드도 운이 좋아서 맞은 것 뿐일 겁니다.
long long koong[67]; 를
long long koong[68]; 로 고쳐보세요.
왜 그런지는 정확히 모르겠지만
n에 67을 대입하는 순간에 n이던 T던지, 혹은, 다른 용도로 쓰는 전역(?) 변수가 변경이 되어서
정의되지 않은 동작을 수행했을 가능성이 상당히 큽니다.
댓글을 작성하려면 로그인해야 합니다.
starsun20 6년 전
꿍 피보나치를 처음에 67까지 다 구하고나서 T개의 테스트케이스에대해 답을 구하면 틀리다고나오고
각 테스트 케이스마다 피보나치 수열을 구해서 답을 내면 맞다고 나옵니다.
왜그런지 이유를 알 수 있을까요?코드는 동일합니다