zoukankan      html  css  js  c++  java
  • NEFU 2016省赛演练一 F题 (高精度加法)

    Function1

    Problem:F

    Time Limit:1000ms

    Memory Limit:65535K

    Description

    You know that huicpc0838 has been reviewing his textbooks and doing related exercises for the coming PG exams these days. One day, when he was abused by the sixth chapter of the textbook  Computer Organization Principles, he came up with an idea. He wrote down something in his draft and happily went lunch (at 11:00 am).
    Here is what he wrote:
    
    int function(int a,int b){
    int c=(a&b),d=(a^b);
    return c==0? d:function1(c<<1,d);
    }
    This function will terminated finally without doubt.
    I will test it this code tonight after I back to dorm.
    
    Do you want to find what's in his mind? Given a,b, can you output the results?
    

    Input

    The input will end with EOF, consisting of several blocks. Every block is two lines, every line represents a non-negative integer, whose length is no more that 3000.

    Output

    Output function1(a,b)%2011.

    Sample Input

    0
    1
    31415926535897932384626
    2718281828
    

    Sample Output

    1
    507
    
    题解1:将公式转换一下发现就是求A+B对2011取余,高精度加法模板题。
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int i,j,k=1,n,m,t,x;
        long long flag;
        char c[3005],d[3005],ans[3005];
        while(cin>>c>>d)
        {
            n=strlen(c);
            m=strlen(d);
            if (n>m)
            {
                x=n-m;
                for (i=n-1;i>=x;i--)
                d[i]=d[i-x];
                for (i=0;i<x;i++)
                d[i]='0';
            }
            else
            {
                x=m-n;
                n=m;
                for (i=m-1;i>=x;i--)
                c[i]=c[i-x];
                for (i=0;i<x;i++)
                c[i]='0';
            }
            int jinwei=0;
            for (i=n-1;i>=0;i--)
            {
                ans[i]=((c[i]-'0')+(d[i]-'0')+jinwei)%10+'0';
                if (c[i]+d[i]-'0'-'0'+jinwei>=10)
                jinwei=1;
                else
                jinwei=0;
            }
            ans[n]='';
            if(jinwei)
            flag=1;
            else
            flag=0;
            for(i=0;i<n;i++)
            flag=(flag*10+ans[i]-'0'+2011)%2011;
            cout<<flag<<endl;
        }
        return 0;
    }

     题解2:先取余再加,最优解。

    #include <iostream>
    #include <string.h>
    using namespace std;
    #define mod 2011
    typedef long long ll;
    int main()
    {
        int i;
        ll a,b;
        char c[3005],d[3005];
        while(cin>>c>>d)
        {
            int len1=strlen(c);
            int len2=strlen(d);
            a=c[0]-'0';
            b=d[0]-'0';
            for(i=1;i<len1;i++)
            a=(a*10+c[i]-'0')%mod;
            for(i=1;i<len2;i++)
            b=(b*10+d[i]-'0')%mod;
            cout<<(a+b)%mod<<endl;
        }
        return 0;
    }
  • 相关阅读:
    DirectX:在graph自己主动连线中增加自己定义filter(graph中遍历filter)
    C3P0数据库连接池使用
    POJ
    【jQuery】复选框的全选、反选,推断哪些复选框被选中
    BestCoder Round #75 King&#39;s Cake 模拟&amp;&amp;优化 || gcd
    《Javascript_Dom 编程艺术》(第2版)读书笔记
    POJ 2947-Widget Factory(高斯消元解同余方程式)
    MFC 小知识总结四
    迭代器和iter()函数
    hdu1595find the longest of the shortest 最短路
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5401632.html
Copyright © 2011-2022 走看看