I missed only the line "When any number in the triangle is exceeds or equals 10^60"
I calculate only less than 10^60 so I get WA
Code: Select all
code removed after acc
Moderator: Board moderators
Code: Select all
code removed after acc
Code: Select all
1 204 20706 ...
Code: Select all
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string calc_sum(string str1, string str2) {
string s1 = str1, s2 = str2;
if (str1.length() > str2.length()) {
for (int i = str2.length(); i < str1.length(); i++)
s2 = '0' + s2;
}
else {
for (int i = str1.length(); i < str2.length(); i++)
s1 = '0' + s1;
}
int carry=0, digit=0;
string sum = "";
for (int i=s1.length()-1; i>=0; i--) {
int a = s1[i]-'0';
int b = s2[i]-'0';
digit = (a+b+carry)%10;
carry = (a+b+carry)/10;
sum += (char)(digit+'0');
}
if (carry>0) sum += (char)(carry+'0');
reverse(sum.begin(), sum.end());
return sum;
}
main() {
bool end_cond = false;
int n = 1;
vector<string> v1, v2;
while (!end_cond) {
v2 = v1;
v1.clear();
v1.push_back("1");
cout << "1";
for (int i = 1; i < n-1; i++) {
string s = calc_sum(v2[i-1], v2[i]);
v1.push_back(s);
cout << " " << s;
if (s.length() > 60) end_cond = true;
}
if (n > 1) {
v1.push_back("1");
cout << " 1";
}
cout << endl;
n++;
}
cout << endl;
return 0;
}
Code: Select all
// cout << endl;
return 0;