• Volume 1. Big Number(uva)


    如用到bign类参见大整数加减乘除模板

    424 - Integer Inquiry

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #define N 10050
    using namespace std;
    string s;
    int ans[N];
    int main()
    {
        int i, j;
        while (cin>>s, s[0] != '0')
        {
            for (i = s.length() - 1, j = 0; i >= 0; i--, j++)
                ans[j] += (s[i] - '0');
        }
        for (i = 0; i < N - 1; i++)
        {
            ans[i + 1] += ans[i] / 10;
            ans[i] %= 10;
        }
        i = N - 1;
        while (!ans[i] && i > 0)
            i--;
        while (i >= 0)
            cout<<ans[i--];
        cout<<endl;
        return 0;
    }
    View Code

    10106 - Product

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #define N 550
    using namespace std;
    int ans[N];
    string s1, s2;
    
    int main()
    {
        int i, j, ls1, ls2, len;//tmp表示进位
        while (cin>>s1>>s2)
        {
            ls1 = s1.length() - 1;
            ls2 = s2.length() - 1;
            len = ls1 + ls2;
            memset(ans, 0, sizeof(ans));
            for (i = ls1; i >= 0; i--)
                for (j = ls2; j >= 0; j--)
                {
                    ans[len - (i + j)] += (s1[i] - '0') * (s2[j] - '0');
                }
            len++;
             for (i = 0; i < len; i++)
             {
                 ans[i + 1] += ans[i] / 10;
                 ans[i] = ans[i] % 10;
             }
             while (!ans[len] && len > 0)//去除前导0
                len--;
             for (; len >= 0; len--)
                cout<<ans[len];
             cout<<endl;
        }
        return 0;
    }
    View Code

    465 - Overflow

    int main()
    {
        //ifstream cin("test.in");
        bign a, b, c;
        char ch;
        bign d = INT_MAX;
        string sa, sb;
        while (cin>>sa>>ch>>sb)
        {
    
            cout<<sa<<' '<<ch<<' '<<sb<<endl;
            a = sa;
            b = sb;
            if (d < a)
                cout<<"first number too big"<<endl;
            if (d < b)
                cout<<"second number too big"<<endl;
            if (ch == '+')
                c = a + b;
            else
                c = a * b;
            cout<<c<<endl;
            if (d < c)
                cout<<"result too big"<<endl;
        }
        return 0;
    }
    View Code

    748 - Exponentiation

    int main()
    {
        string sa, sb;
        bign a, b;
        int p, n, i;
        while (cin>>sa>>n)
        {
            p = 0;
            while (sa[p] != '.')
                p++;
            sa.erase(sa.begin() + p);
            a = sa;
            b = a ^ n;
            sb = b.to_str();
            p = (5 - p) * n;
            for (i = b.length(); i < p; i++)
                sb += '0';
            sb.insert(sb.begin() + p, '.');
            p = 0;
            while (sb[p] == '0')
                p++;
            for (i = sb.length() - 1; i >= p; i--)
                cout<<sb[i];
            cout<<endl;
        }
        return 0;
    }
    View Code

    10494 - If We Were a Child Again

    int main()
    {
        //string sa, sb;
        bign a, b, c;
        char ch;
        while (cin>>a>>ch>>b)
        {
            if (ch == '/')
                c = a / b;
            else
                c = a % b;
            cout<<c<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    云题库错题分析
    数据库相关子查询
    阻止事件冒泡
    21分钟 MySQL 入门教程(转载!!!)
    java接口
    java访问修饰符
    小游戏,快速击键
    个人对Java中多态的一些简单理解
    简述抽象和封装,对你学习Java有一些作用
    Bank,我只是来完成作业的
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3675483.html
Copyright © 2020-2023  润新知