2000. A-B(Big Integer)

yunhuang

有点麻烦,试了好几次才AC,还是python大法好~

include

include

include

define MAXN 1024

using namespace std;

int main()
{
string num1,num2;
while((cin>>num1>>num2))
{
int ans[MAXN]={0},cnt=0;
if(num1.length()<=num2.length())//判断符号
{
bool flag = true;
if(num1.length()==num2.length())
{
int i;
for(i=0;inum2[i])
flag = false;
}
if(flag==true)
{
cout<<’-‘;
swap(num1,num2);
}
}
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
while(num2.length()<num1.length()) num2.push_back(‘0’);//对齐位数,方便计算
for(int i =0;i1&&ans[cnt-1]==0)cnt--;//去除前导零 for(int i = cnt-1;i>=0;i–)
cout<<ans[i];
cout<<endl;
}
return 0;
}

VarusFromHrbust

Java 大数类
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()) {
        BigInteger b1 = sc.nextBigInteger();
        BigInteger b2 = sc.nextBigInteger();
        System.out.println(b1.subtract(b2));
    }

}

}

10175101229

include

using namespace std;

void swap(string a,string b)
{
string *temp;
temp=a;
a=b;
b=temp;
}

int main()
{
string str1,str2,a(“”);
int len1,len2,temp,flag=0;
while(cin>>str1>>str2)
{
len1=str1.size(),len2=str2.size();
if(len1=0; i--) { str1[i]=(char)(58+str1[i]-flag-str2[i]); if(str1[i]>=(‘9’+1))
{
flag=0;
str1[i]=(char)(str1[i]-10);
}

        else
        {
            flag=1;
        }
    }
    int pos=str1.find_first_not_of('0');
    if(pos==-1)
        str1.erase(0,len1-1);
    else
        str1.erase(0,pos);
    str1=a+str1;
    cout<<str1<<endl;
    a= {""};
}

}

10175101229

大数减法,不是很难,用string很方便,我写得比较杂,很多地方可以改进的

13627999316

python实现比较简单:唯一注意的就是测试用例是多组输入:

while True:
    try:
        a,b = map(int,input().split())
        print(a-b)
    except:
        break
你当前正在回复 博客/题目
存在问题!