zoukankan      html  css  js  c++  java
  • 【CODEVS3115】高精度练习之减法

    Description

    给出两个正整数A和B,计算A-B的值。保证A和B的位数不超过500位。

    Input

    读入两个用空格隔开的正整数

    Output

    输出A-B的值

    Sample Input

    3 12

    Sample Output

    -9

    Hint

    两个正整数的位数不超过500位

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    char Sa[510],Sb[510];
    int a[510],b[510];
    int main()
    {
        cin>>Sa>>Sb;
        int aLen=strlen(Sa),bLen=strlen(Sb);
        int l=max(aLen,bLen);
        if (bLen>aLen || (aLen==bLen && !strcmp(Sa,Sb)))
        {
            swap(Sa,Sb);
            swap(aLen,bLen);
            cout<<"-";
        }
        for (int i=1;i<=aLen;i++) a[i]=Sa[aLen-i]-'0';
        for (int i=1;i<=bLen;i++) b[i]=Sb[bLen-i]-'0';
        for (int i=1;i<=l;i++)
        {
            a[i]=a[i]-b[i];
            if (a[i]<0)
            {
                a[i]+=10;
                a[i+1]--; 
            }                                                              
        }
        while (!a[l]) l--;
        for (int i=l;i>=1;i--) cout<<a[i];
        return 0;
    }
  • 相关阅读:
    linux 常用命令
    git 常见命令
    合并两个有序链表---python
    Code Contract for .NET
    Kruskal最小生成树算法
    逻辑-哲学
    停机问题
    逆向工程
    .net framework
    python 类库
  • 原文地址:https://www.cnblogs.com/liumengyue/p/5191698.html
Copyright © 2011-2022 走看看