Description
输入两个实数,判断第一个数大,第二个数大还是一样大。每个数的格式为:
[整数部分].[小数部分]
简单起见,整数部分和小数部分都保证非空,且整数部分不会有前导 0。不过,小数部分的最 后可以有 0,因此 0.0 和 0.000 是一样大的。
Input
输入包含不超过 20 组数据。每组数据包含一行,有两个实数(格式如前所述)。每个实数都 包含不超过 100 个字符。
Output
对于每组数据,如果第一个数大,输出”Bigger”。如果第一个数小,输出”Smaller”。如果两个 数相同,输出”Same”。
Sample Input
1.0 2.0
0.00001 0.00000
0.0 0.000
Sample Output
Case 1: Smaller
Case 2: Bigger
Case 3: Same
字符长度最多100,double的精度肯定不够了
我是先当做字符串输入,这样就判断大小就很方便,幸亏“.”的ascii码比数字的小
然后就是比较麻烦的后置0,我先判断长度,在字符的公共部分先判断大小,如果还是相等,就再找长的那个数字,如果后面出现了不是0的数,那肯定这个更大,如果都是0就相等
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#define INF 0x3f3f3f3f
#define MAXN 10000005
#define Mod 1000000007
using namespace std;
int main()
{
string a,b;
int cnt=1;
while(cin>>a>>b)
{
int la=a.length(),lb=b.length();
if(la==lb)
{
if(a==b)
printf("Case %d: Same\n",cnt++);
else if(a<b)
printf("Case %d: Smaller\n",cnt++);
else
printf("Case %d: Bigger\n",cnt++);
}
else
{
if(la<lb)
{
int flag=0;
for(int i=0; i<la; ++i)
{
if(a[i]<b[i])
{
flag=1;
break;
}
else if(a[i]>b[i])
{
flag=2;
break;
}
}
if(flag==1)
printf("Case %d: Smaller\n",cnt++);
else if(flag==2)
printf("Case %d: Bigger\n",cnt++);
else
{
for(int i=la;i<lb;++i)
{
if(b[i]!='0')
{
flag=1;
break;
}
}
if(flag==0)
printf("Case %d: Same\n",cnt++);
else
printf("Case %d: Smaller\n",cnt++);
}
}
else if(la>lb)
{
int flag=0;
for(int i=0; i<lb; ++i)
{
if(a[i]<b[i])
{
flag=1;
break;
}
else if(a[i]>b[i])
{
flag=2;
break;
}
}
if(flag==1)
printf("Case %d: Smaller\n",cnt++);
else if(flag==2)
printf("Case %d: Bigger\n",cnt++);
else
{
for(int i=lb;i<la;++i)
{
if(a[i]!='0')
{
flag=1;
break;
}
}
if(flag==0)
printf("Case %d: Same\n",cnt++);
else
printf("Case %d: Bigger\n",cnt++);
}
}
}
}
return 0;
}