ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 2748번 풀이
    백준 2018. 8. 19. 17:33

    최초에 다음과 같이 구현했지만, 틀렸습니다가 떴습니다.



    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    using namespace std;
    int buffer[100];

    int main() {
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    buffer[0] = 0;
    buffer[1] = 1;
    if (n == 0 || n== 1) {
    cout << buffer[n];
    }else {
    for (int i = 2; i <= n; i++) {
    buffer[i] = buffer[i - 1] + buffer[i - 2];
    }
    cout << buffer[n];
    }
    return 0;
    }


    왜 안되는지 삽질하다가..

    설마 int가 오버플로우 나는 게 아니었나 싶어서 int를 롱롱으로 바꾸어서 다시 제출했습니다.


    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    using namespace std;
    long long buffer[100];

    int main() {
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    buffer[0] = 0;
    buffer[1] = 1;
    if (n == 0 || n== 1) {
    cout << buffer[n];
    }else {
    for (int i = 2; i <= n; i++) {
    buffer[i] = buffer[i - 1] + buffer[i - 2];
    }
    cout << buffer[n];
    }
    return 0;
    }



    그랬더니 정답 처리.

    '백준' 카테고리의 다른 글

    백준 2579번 C++ 풀이  (0) 2018.08.19
    백준 1463번 C++ 풀이  (0) 2018.08.19
    백준 1932번 C++ 풀이  (0) 2018.08.19
    백준 10866번 C++ 풀이  (0) 2018.08.19
    백준 1003번 C++ 풀이  (0) 2018.08.19
Designed by Tistory.