csu1777——大還是小?(字符串處理?)

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章