• CodeVS 3115 高精度练习之减法


    题目大意:

    http://codevs.cn/problem/3115/

    题解:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    int ans[501] = {0};
    
    int sub(string a, string b)// a is the big number, b is smaller.
    {
        int m = a.length()-1;
        int n = b.length()-1;
        int k = 0;
    
        for(int i = m; i >= 0; i--)
        {
            if(n >= 0)
            {
                if(a[i] >= b[n])
                    ans[k++] = int(a[i]-b[n]);
    
                else
                {
                    ans[k++] = int(a[i]+10-b[n]);
    
                    int tmp = i-1;
                    while(a[tmp] == '0')
                    {
                        a[tmp] = '9';
                        tmp--;
                    }
                    a[tmp] = char(a[tmp]-1);
                }
    
                n--;
            }
    
            else
            {
                ans[k++] = int(a[i] - '0');
            }
        }
    
        return k;
    
    }
    
    int main()
    {
    
        string a,b;
        cin >> a >> b;
        int k;
    
        if(a.length() < b.length() || (a.length() == b.length() && a < b))
        {
            k = sub(b,a)-1;
            cout << "-";
        }
    
        else if(a.length() > b.length() || (a.length() == b.length() && a > b))
        {
            k = sub(a,b)-1;
        }
    
        else
        {
            cout << "0";
        }
    
        while(ans[k] == 0)
            k--;
        for(int i = k; i >= 0; i--)
        {
            cout << ans[i];
        }
    
    
        return 0;
    }
  • 相关阅读:
    9.16第一次上课关于HTML的小结
    9.22课堂记录
    9.19课堂记录
    html文档基本结构
    HTMl学习1
    HTML第一次学习笔记
    CSS3和CSS2.1的区别
    HTML5新元素
    第一课:HTML
    块级元素和行内元素
  • 原文地址:https://www.cnblogs.com/zyqBlog/p/7603568.html
Copyright © 2020-2023  润新知