Problem 26 高精度加法

//============================================================================
// Name        : pe25.cpp
// Author      : paul08colin
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000;
int main()
{
    int f1[1010];
    int f2[1010];
    int f3[1010];
    memset(f1,0,sizeof(f1));
    memset(f2,0,sizeof(f2));
    memset(f3,0,sizeof(f3));
    f1[0] = 1;
    f2[0] = 1;
    int ans = 2;
    while(1)
    {
    	int carry = 0;
    	int i;
    	for(i = 0; i <= MAXN; i++)
    	{
    		f3[i] = f1[i] + f2[i] + carry;
    		carry = (f3[i])/10;
    		f3[i] %= 10;
    	}
    	ans++;
    	for(i = 1005; i >=999; i--)
    	{
    		if(f3[i] != 0)
    		{
			   goto here;
    		}
    	}

       	for(i = 0; i <= MAXN; i++)
        	{
        		f1[i] = f2[i] + f3[i] + carry;
        		carry = (f1[i])/10;
        		f1[i] %= 10;
        	}
       	ans++;
       	for(i = 1005; i >=999; i--)
        	{
        		if(f1[i] != 0)
        		{
    			   goto here;
        		}
        	}
       	memcpy(f2,f1,sizeof(f1));
       	memcpy(f1,f3,sizeof(f3));


    }
    here:;
    printf("%d\n",ans);
	return 0;
}


你可能感兴趣的:(Problem 26 高精度加法)