CSU 1777: 大还是小?【模拟/后导0】

293419 roniking 1777
Accepted
2032 0 C++ 2000 2018-04-03 19:21:25

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

Hint

Source

湖南省第十一届大学生计算机程序设计竞赛

 【代码】:

 【分析】:

[整数部分判断]:用string.find函数找到小数点的位置,在这之前的字符串转换为数值比较大小,若不相等则可以直接判断谁大谁小,否则还要继续判断小数位。

[小数部分判断]:截取小数点后的部分,若位数不同用0填补成相同位数。比如0.03和0.0300变成0300=0300或者0.0050和0.07变成0050<0700,这样位数相同可以直接用string比较字典序。

字典序解释:

设想一本英语字典里的单词,何者在前何者在后?
显然的做法是先按照第一个字母、以 a、b、c……z 的顺序排列;如果第一个字母一样,那么比较第二个、第三个乃至后面的字母。如果比到最后两个单词不一样长(比如,sigh 和 sight),那么把短者排在前。
通过这种方法,我们可以给本来不相关的单词强行规定出一个顺序。“单词”可以看作是“字母”的字符串,而把这一点推而广之就可以认为是给对应位置元素所属集合分别相同的各个有序多元组规定顺序:下面用形式化的语言说明。
而数字也有字典序.
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<sstream>
#include<queue>
#include<cmath>
#include<list>
#include<vector>
#include<string>

using namespace std;

#define long long ll
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int N = 100005;
int n, m, tot;
int a[50][50];
int x, y;

int main()
{

    string a, b;
    int ai = 0, bi = 0, c = 1, ad, bd;
    while(cin >> a >> b)
    {
        ai = 0, bi = 0, ad = 0, bd = 0;
        int x = a.find('.'); //1
        int y = b.find('.'); //1
        //cout<<x<<endl;
        for(int i=0; i<x; i++)
        {
            ai = ai * 10 + a[i] - '0';
        }
        for(int i=0; i<y; i++)
        {
            bi = bi * 10 + b[i] - '0';
        }
        printf("Case %d: ",c++);
        //cout<<"ai = "<<ai<<" "<<"bi = "<<bi<<endl;
        int ca = 0, cb = 0, pa, pb;
        if(ai > bi)
        {
            printf("Bigger
");
        }
        else if(ai < bi)
        {
            puts("Smaller");
        }
        else
        //12.0050 12.007
        //0500 700
        {
            //0.2      0.2000
            string sa = a.substr(x+1,a.size());
            string sb = b.substr(y+1,b.size());
            int an = sa.size();//1
            int bn = sb.size();//4
            if(an > bn)
            {
                for(int i=0; i<an-bn;i++)
                {
                    sb += '0';
                }
            }
            if(an < bn)
            {
                for(int i=0; i<bn-an;i++)
                {
                    sa += '0';
                }
            }
            //cout<<sa<<" "<<sb<<endl;
            if(sa > sb)
            {
                puts("Bigger");
            }
            else if(sa < sb)
            {
                puts("Smaller");
            }
            else
            {
                puts("Same");
            }
        }
    }

    return 0;
}
/*
0.0005000
0.0007
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<sstream>
#include<queue>
#include<cmath>
#include<list>
#include<vector>
#include<string>
using namespace std;
#define long long ll
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int N = 100005;
int n, m, tot;
int a[50][50];
int x, y;

int main()
{

    string a, b;
    int ai = 0, bi = 0, c = 1, ad, bd;
    while(cin >> a >> b)
    {
        ai = 0, bi = 0, ad = 0, bd = 0;
        int x = a.find('.'); int y = b.find('.');
        for(int i=0; i<x; i++)
            ai = ai * 10 + a[i] - '0';
        for(int i=0; i<y; i++)
            bi = bi * 10 + b[i] - '0';
        printf("Case %d: ",c++);

        int ca = 0, cb = 0, pa, pb;

        if(ai > bi)
            printf("Bigger
");
        else if(ai < bi)
            puts("Smaller");
        else
        {
            string sa = a.substr(x+1,a.size());
            string sb = b.substr(y+1,b.size());
            int an = sa.size();
            int bn = sb.size();
            if(an > bn)
                for(int i=0; i<an-bn;i++)
                    sb += '0';
            if(an < bn)
                for(int i=0; i<bn-an;i++)
                    sa += '0';
            if(sa > sb)
                puts("Bigger");
            else if(sa < sb)
                puts("Smaller");
            else
                puts("Same");
        }
    }

    return 0;
}
压行版(滑稽脸
原文地址:https://www.cnblogs.com/Roni-i/p/8710564.html