Poj Solution 3982

http://poj.org/problem?id=3982

#include <stdio.h>   
#include <string.h>   
#include <stdlib.h>   
  
#define MAX_LEN 1024   
  
void BigIntegerAdd(const char a[], const char b[], char res[])   
{   
    int i, j, k;   
    int x, y, z;   
    int up, l;   
  
    char *c;   
  
    if(strlen(a) > strlen(b))   
    {   
        l = strlen(a) + 2;   
    }   
    else  
    {   
        l = strlen(b) + 2;   
    }   
  
    c = (char *)malloc(l * sizeof(char));   
    i = strlen(a) - 1;   
    j = strlen(b) - 1;   
    k = up = 0;   
  
    while(i >= 0 || j >= 0)   
    {   
        x = i < 0 ? '0' : a[i];   
        y = j < 0 ? '0' : b[j];   
        z = x - '0' + y - '0';   
        if(up)   
        {   
            ++z;   
        }   
        if(z > 9)   
        {   
            up = 1;   
            z -= 10;   
        }   
        else  
        {   
            up = 0;   
        }   
        c[k++] = z + '0';   
        --i;   
        --j;   
    }   
  
    if(up)   
    {   
        c[k++] = '1';   
    }   
    i = 0;   
    c[k] = '';   
  
    for(k -= 1; k >= 0; --k)   
    {   
        res[i++] = c[k];   
    }   
    res[i] = '';   
  
    free(c);   
}   
  
int main(int argc, char **argv)   
{   
    char a[4][MAX_LEN];   
    int i, n = 99;   
    while(EOF != scanf("%s %s %s", a[0], a[1], a[2]))   
    {   
        for(i = 3; i <= 99; ++i)   
        {   
            BigIntegerAdd(a[0], a[1], a[3]);   
            BigIntegerAdd(a[2], a[3], a[3]);   
            strcpy(a[0], a[1]);   
            strcpy(a[1], a[2]);   
            strcpy(a[2], a[3]);   
        }   
        printf("%sn", a[3]);   
    }   
    return 0;   
}  

											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *