• uva 10494


    uva 10494 - If We Were a Child Again

    If We Were a Child Again

    Input: standard input
    Output: standard output

    Time Limit: 7 seconds

     

    “Oooooooooooooooh!

    If I could do the easy mathematics like my school days!!

    I can guarantee, that I’d not make any mistake this time!!”

    Says a smart university student!!

    But his teacher even smarter – “Ok! I’d assign you such projects in your software lab. Don’t be so sad.”

    “Really!!” - the students feels happy. And he feels so happy that he cannot see the smile in his teacher’s face.

     

    The Problem

    The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.

    But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people. You agreed to write only the (integer) division and mod (% in C/C++) operations for him.

     

    Input

    Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).

     
    Output

    A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.

     
     
    Sample Input

    110 / 100

    99 % 10

    2147483647 / 2147483647

    2147483646 % 2147483647

     
     
    Sample Output

    1

    9

    1

    2147483646

    高精度对低精度的除法和取余,模拟下小学时候的除法过程就行了。取余则调用已经实现的+-*/就行了。

    中间结果要用long long才能过,坑爹的是忽略替换for(int i = 0, g = 0……)中的int结果WA了好多次。

    套用模版时如果需要用到long long就全局替换掉int.注意替换后一些关键词也被替换了此时注意检查语法错误就行了

    /*
    1.高精度加法
    2.高精度减法 
    3.高精度乘法 
    4.高精度除以低精度 
    5.高精度对低精度的取余 
    
    必要时可以将全局的long long替换成long long.除了main函数的返回值long long
    用到除法和取余的时候可能需要把全局的long long替换成long long 
    */
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <climits>
    #include <cassert>
    using namespace std;
    
    #define maxn 30000
    
    struct bign
    {
        long long len, s[maxn];
    
        bign()
        {
            memset(s, 0, sizeof(s));
            len = 1;
        }
        
        bign(long long num)
        {
            *this = num;
        }
        
        bign(const char* num) 
        {
            *this = num;
        }
        
        bign operator = (long long num) 
        {
            char s[maxn];
            sprintf(s, "%d", num);
            *this = s;
            return *this;
        }
        
        bign operator = (const char* num) 
        {
            len = strlen(num);
            for (long long i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
            return *this;
        }
        
        string str() const 
        {
            string res = "";
            for (long long i = 0; i < len; i++) res = (char)(s[i] + '0') + res;
            if (res == "") res = "0";
            return res;
        }
        
        /*去除前导0*/ 
        void clean() 
        {
            while(len > 1 && !s[len-1]) len--;
        }
        
        /*高精度的加法*/
        bign operator + (const bign& b) const
        {
            bign c;
            c.len = 0;
            for (long long i = 0, g = 0; g || i < max(len, b.len); i++) 
            {
                long long x = g;
                if (i < len) x += s[i];
                if (i < b.len) x += b.s[i];
                c.s[c.len++] = x % 10;
                g = x / 10;
            }
            return c;
        }
        
        /*高精度的减法*/ 
        bign operator - (const bign& b) 
        {
            bign c; c.len = 0;
            for (long long i = 0, g = 0; i < len; i++) 
            {
                long long x = s[i] - g;
                if (i < b.len) x -= b.s[i];
                if (x >= 0) 
                    g = 0;
                else 
                {
                    g = 1;
                    x += 10;
                }
                c.s[c.len++] = x;
            }
            c.clean();
            return c;
        }
        
        /*高精度的乘法*/ 
        bign operator * (const bign& b) 
        {
            bign c; c.len = len + b.len;
            for (long long i = 0; i < len; i++)
            for (long long j = 0; j < b.len; j++)
            c.s[i+j] += s[i] * b.s[j];
            for (long long i = 0; i < c.len-1; i++)
            {
                c.s[i+1] += c.s[i] / 10;
                c.s[i] %= 10;
            }
            c.clean();
            return c;
        } 
        
        /*高精度除以低精度*/    /*用到除法和取余的时候可能需要把全局的int替换成long long*/
        bign operator / (long long b) const
        {
            assert(b > 0);
            bign c;c.len = len;
            for (long long i = len-1, g = 0; i >= 0; --i)
            {
                long long x = 10*g+s[i];    //这里可能会超过int 故用long long 
                
                c.s[i] = x/b;                //这里可能会超过int
                
                g = x-c.s[i]*b;                //这里可能会超过int 
            }
            c.clean();
            return c;
        }
        
        /*高精度对低精度取余*/    /*用到除法和取余的时候可能需要把全局的int替换成long long*/
        bign operator % (long long b)
        {
            assert(b > 0);
            bign d = b;
            bign c = *this-*this/b*d;
            return c;
        }
        
        bool operator < (const bign& b) const
        {
            if (len != b.len) return len < b.len;
            for (long long i = len-1; i >= 0; i--)
            if (s[i] != b.s[i]) return s[i] < b.s[i];
            return false;
        }
        
        bool operator > (const bign& b) const
        {
            return b < *this;
        }
        
        bool operator <= (const bign& b) 
        {
            return !(b > *this);
        }
        
        bool operator >= (const bign& b) 
        {
            return !(b < *this);
        }
        
        bool operator == (const bign& b) 
        {
            return !(b < *this) && !(*this < b);
        }
        
        bool operator != (const bign& b) 
        {
            return (b < *this) || (*this < b);
        }
        
        bign operator += (const bign& b) 
        {
            *this = *this + b;
            return *this;
        }
    };
    
    istream& operator >> (istream &in, bign& x) 
    {
        string s;
        in >> s;
        x = s.c_str();
        return in;
    }
    
    ostream& operator << (ostream &out, const bign& x) 
    {
        out << x.str();
        return out;
    }
    
    
    int main()
    {
        bign a;
        char b;
        long long c;
        while (cin >> a >> b >> c)
        {
            if (b == '/')
                cout << a/c << endl;
            else
                cout << a%c << endl;
        }
    }
    View Code
  • 相关阅读:
    Pycharm 2016 注册码
    在mac上搭建python环境
    Carthage 的使用
    生成唯一的随机字符串
    utf-8 转码--网址转码
    让自己的项目支持 Carthage
    系统自带的语音合成
    个性化 UIAlertController
    cocoaPods 创建自己的依赖库
    appleDoc 使用
  • 原文地址:https://www.cnblogs.com/chenyg32/p/3188113.html
Copyright © 2020-2023  润新知