View Code of Problem 3497

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;
string sp(int n)
{
	char t_s[10000];
	sprintf(t_s, "%d", n);
	string s = t_s;
	return s;
}
string sum(string s1, string s2)
{
	int i, t1, t2,t;
	string s;
	int len1 = s1.size();
	int len2 = s2.size();
	int len = max(len1, len2);
	if (len1 < len2)
	{
		for (i = 0; i < len2 - len1; i++)
		{
			s1.insert(s1.begin(), '0');
		}
	}
	else if (len1 > len2)
	{
		for (i = 0; i < len1 - len2; i++)
		{
			s2.insert(s2.begin(), '0');
		}
	}
	for (i = len - 1; i > 0; i--)
	{
		t1 = s1[i] - '0';
		t2 = s2[i] - '0';
		t = t1 + t2;
		if (t < 10)
			s.insert(s.begin(), t + '0');
		else
		{
			s1[i - 1]=((s1[i - 1] - '0')+1) + '0';
			t = t % 10;
			s.insert(s.begin(), t + '0');
		}
	}
	t1 = s1[i] - '0';
	t2 = s2[i] - '0';
	t = t1 + t2;
	s = sp(t) + s;
	return s;
}
int main()
{
	int n, m, t,flag;
	int i, j, k;
	string a[100];
	while (cin >> a[0] >> a[1] >> a[2])
	{
		for (i = 3; i < 100; i++)
			a[i] = sum(a[i - 1], sum(a[i - 2], a[i - 3]));
		/*for (i = 3; i < 100; i++)
			cout << a[i] << endl;*/
		cout << a[99] << endl;
	}
	
	return 0;
}

Double click to view unformatted code.


Back to problem 3497