大数比较大小
比较大小和排序可以说是编程比较常见的问题,并且对于比较小的数来比较大小(数值不超过数据类型的范围),用编程是很容易实现的,但是当数非常大的时候,比如超过了1000位,我们应该如何处理呢? 给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如123456789123456789要大于-123456
自己写的代码(有点长) #include<stdio.h> 最优代码(使用c++写的) 01.
#include<iostream>
02.
#include<string>
03.
using ?namespace ?std;
04.
?
05.
int?main()
06.
{
07.
string a,b;
08.
while (cin>>a>>b)
09.
{
10.
if (a== "0" &&b== )
11.
return ?0;
12.
(a==b)
13.
cout<< "a==b" <<endl;
14.
else?(a[0]== '-' &&b[0]== 15.
16.
(a.substr(1,string::npos)>b.substr(1,string::npos)||a.length()>b.length())
17.
"a<b" 18.
else?"a>b" <<endl;
19.
}
20.
(a>&&b> ||a< &&b< &&a.length()>b.length()||a>b)
21.
22.
(a<23.
24.
?
25.
26.
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |