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;
    }
  • 相关阅读:
    使用 libevent 和 libev 提高网络应用性能
    An existing connection was forcibly closed by the remote host
    各种浏览器的兼容css
    vs输出窗口,显示build的时间
    sass
    网站设置404错误页
    List of content management systems
    css footer not displaying at the bottom of the page
    强制刷新css
    sp_executesql invalid object name
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5401632.html
Copyright © 2011-2022 走看看