poj 3982(水题,大数加法和大数乘法)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct Node{
    int data[1005];
    int n;
    Node(){
        n = 1;
        memset(data,0,sizeof data);
    }
};
Node nPlus(Node n1,Node n2){
    Node ans;
    int i,j,t,len,len2;
    len = min(n1.n,n2.n);
    len2 = max(n1.n,n2.n);
    for(i=0;i<len;i++){
        ans.data[i] += n1.data[i]+n2.data[i];
        if(ans.data[i]>=10){
            ans.data[i+1] = ans.data[i]/10;
            ans.data[i] %= 10;
        }
    }
    if(n1.n>len){
        for(i=len;i<len2;i++){
            ans.data[i] += n1.data[i];
            if(ans.data[i]>=10){
                ans.data[i+1] = ans.data[i]/10;
                ans.data[i] %= 10;
            }
        }
    }
    else if(n2.n>len){
        for(i=len;i<len2;i++){
            ans.data[i] += n2.data[i];
            if(ans.data[i]>=10){
                ans.data[i+1] = ans.data[i]/10;
                ans.data[i] %= 10;
            }
        }
    }
    ans.n = len2;
    if(ans.data[ans.n]>0){
        ans.n++;
    }
    return ans;
}
Node mul(Node n1,int m){
    int i,j;
    if(m==0){
        Node s;
        return s;
    }
    for(i=0;i<n1.n;i++){
        n1.data[i] *= m;
    }
    for(i=0;i<n1.n;i++){
        if(n1.data[i]>=10){
            n1.data[i+1] += n1.data[i]/10;
            n1.data[i] %= 10;
        }
    }
    while(n1.data[i]>0){
        if(n1.data[i]>=10){
            n1.data[i+1] += n1.data[i]/10;
            n1.data[i] %= 10;
        }
        i++;
    }
    n1.n = i;
    return n1;
}
int main(){
    int i,j;
    Node node[4],ans[4];
    memset(ans,0,sizeof ans);
    memset(node,0,sizeof node);
    node[0].data[0] = 1;
    node[1].data[0] = 0;
    node[2].data[0] = 0;
    node[0].n = node[1].n = node[2].n = 1;
    for(i=4;i<=100;i++){
        node[3] = nPlus(nPlus(node[2],node[1]),node[0]);
        node[0] = node[1];
        node[1] = node[2];
        node[2] = node[3];
    }
    ans[0] = node[3];
    memset(node,0,sizeof node);
    node[0].data[0] = 0;
    node[1].data[0] = 1;
    node[2].data[0] = 0;
    node[0].n = node[1].n = node[2].n = 1;
    for(i=4;i<=100;i++){
        node[3] = nPlus(nPlus(node[2],node[1]),node[0]);
        node[0] = node[1];
        node[1] = node[2];
        node[2] = node[3];
    }
    ans[1] = node[3];
    memset(node,0,sizeof node);
    node[0].data[0] = 0;
    node[1].data[0] = 0;
    node[2].data[0] = 1;
    node[0].n = node[1].n = node[2].n = 1;
    for(i=4;i<=100;i++){
        node[3] = nPlus(nPlus(node[2],node[1]),node[0]);
        node[0] = node[1];
        node[1] = node[2];
        node[2] = node[3];
    }
    ans[2] = node[3];
    int a0,a1,a2;
    while(scanf("%d%d%d",&a0,&a1,&a2)!=EOF){
        Node tmp = mul(ans[0],a0);
        ans[3] = tmp;
        tmp = mul(ans[1],a1);
        ans[3] = nPlus(ans[3],tmp);
        tmp = mul(ans[2],a2);
        ans[3] = nPlus(ans[3],tmp);
        for(i=ans[3].n-1;i>-1;i--){
            cout<<ans[3].data[i];
        }
        cout<<endl;
    }
    return 0;
}

 

poj 3982(水题,大数加法和大数乘法)

上一篇:JZOJ 4250.路径


下一篇:Redis学习--渐进式rehash实现原理