P2142高精度减法 40求调

题目总版

nyq0812 @ 2024-11-15 20:44:43

rt

#include<iostream>
using namespace std;
int main(){
    int a[10086],b[10086],c[10086];
    bool n;
    string s1,s2;
    cin>>s1>>s2;
    if(s1.size()<s2.size()||s1.size()==s2.size()&&s1<s2)
    {
        swap(s1,s2);
        n=true;

        //cout<<'-';
    }
    int len1=s1.size();
    int len2=s2.size();
    for(int i=0;i<len1;i++)
    {
        a[i]=s1[len1-i-1]-'0';
    }
    for(int i=0;i<len2;i++)
    {
        b[i]=s2[len2-i-1]-'0';
    }
    for(int i=0;i<len1;i++)
    {
        if(a[i]-b[i]<0)
        {
            a[i]+=10;
            a[i+1]--;
        }
        c[i]=a[i]-b[i];
    }
    while(c[len1-1]==0&&len1>1)
    {
        len1--;
    }
    if(n==true)
            cout<<'-';
    for(int i=len1-1;i>=0;i--)
    {
        cout<<c[i];

    }   
}

by longyitongxue @ 2024-11-15 20:53:34

数组没有清零


by longyitongxue @ 2024-11-15 20:54:10

int a[10086]={},b[10086]={},c[10086]={};

by longyitongxue @ 2024-11-15 20:54:22

加个关注


|