처음에 long, long long 으로 타입을 지정하고 틀렸다. 수가 굉장히 크기 때문에 string타입으로 지정하고, string타입으로 사칙 연산을 수행해야 한다.
reverse 함수와 to_string 함수 활용에 대해 연습할 수 있었다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void input_data(int* n, vector<string>& dp)
{
cin >> *n;
dp.push_back("0");
dp.push_back("1");
return;
}
string string_sum(string A, string B)
{
//cout << "string_sum(" << A << ", " << B << ")\n";
int i;
string answer = "";
int temp, ten_temp = 0;
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
//cout << "reverse(A) : " << A << " reverse(B) : " << B << "\n";
if (A.length() > B.length())
{
for (i = B.length(); i < A.length(); i++)
{
B += "0";
}
}
else if (A.length() == B.length())
{
;
}
else
{
for (i = A.length(); i < B.length(); i++)
{
A += "0";
}
}
//cout << "editted(A) : " << A << " editted(B) : " << B << "\n";
for (i = 0; i < A.length(); i++)
{
temp = (A[i] - '0') + (B[i] - '0') + ten_temp;
//cout << "temp : " << temp << "\n";
if (temp >= 10)
{
ten_temp = 1;
answer += to_string((temp % 10));
}
else
{
ten_temp = 0;
answer += to_string(temp);
}
}
if (ten_temp == 1)
{
answer += to_string(ten_temp);
}
reverse(answer.begin(), answer.end());
//cout << answer << "\n";
return answer;
}
void find_answer(int n, vector<string>& dp)
{
int i;
for (i = 2; i <= n; i++)
{
dp.push_back(string_sum(dp[i-2], dp[i-1]));
}
cout << dp[n] << "\n";
return;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
vector<string> dp;
input_data(&n, dp);
find_answer(n, dp);
return 0;
}