CodeForce 616A Comparing Two Long Integers 字符串比較

A. Comparing Two Long Integers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.

The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().

Input

The first line contains a non-negative integer a.

The second line contains a non-negative integer b.

The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.

Output

Print the symbol "<" if a < b and the symbol ">" if a > b. If the numbers are equal print the symbol "=".

Sample test(s)
input
9
10
output
<
input
11
10
output
>
input
00012345
12345
output
=
input
0123
9
output
>
input
0123
111
output
>

題意:比較兩個給出的數字的大小,然後給出相應的大小於符號,注意有些數據有前導零。

題解:由於數據的範圍過大,只能用字符串輸入來比較。這裏有兩種解法:一是字符串轉化爲整型數組,去除前導零,再逐個比較:二是加前導零是兩個字符串長度相等,然後再利用字典樹比較,直接得出結果。(加前導零的時候需要逆序,比較的時候需要正序)

解法一:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<string>

#define inf 9999999.9
#define maxn 1000010
using namespace std;

char s1[maxn],s2[maxn];
int a[maxn],b[maxn];
int main()
{
    while(~scanf("%s",s1))
    {
        int m,n;
        int fg=0;
       scanf("%s",s2);
       int l1=strlen(s1);
       int l2=strlen(s2);
       for(int i=0;i<=max(l1,l2);i++)
       {
           a[i]=s1[i]-'0';
           b[i]=s2[i]-'0';
       }
       for(int i=0;i<=l1;i++)
       {
           if(a[i]!=0)
           {
               m=i;
               break;
           }
       }
       for(int i=0;i<=l2;i++)
       {
           if(b[i]!=0)
           {
               n=i;
               break;
           }
       }
       /*for(int i=m;i<l1;i++)
        printf("%d",a[i]);
       for(int i=n;i<l2;i++)
        printf("%d",b[i]);*/
       if(l1-m>l2-n)
       {
           fg=1;
           printf(">\n");
       }

       else
        if(l1-m<l2-n)
        {
            fg=1;
            printf("<\n");
        }

       else
       {
          for(int i=m,j=n;i<l1;i++,j++)
          {
              if(a[i]>b[j])
              {
                  fg=1;
                 printf(">\n");
                 break;
              }
              else
                if(a[i]<b[j])
                {
                    fg=1;
                   printf("<\n");
                   break;
                }
          }
       }
       if(!fg)
        printf("=\n");

    }
    return 0;
}

解法二:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<string>

#define inf 9999999.9
#define maxn 1000010
using namespace std;

char s1[maxn],s2[maxn];
char s3[maxn],s4[maxn];
int a[170],b[170];
int main()
{
    while(~scanf("%s",s1))
    {
       scanf("%s",s2);
       int l1=strlen(s1);
       int l2=strlen(s2);
       int len1=0,len2=0;
       if(l1!=l2)
       {
           if(l1>l2)
           {
               for(int i=l1,j=l2;i>=0;i--,j--)
               {
                   s3[len1++]=s1[i];
                   if(j>=0)
                   s4[len2++]=s2[j];
                   else
                    s4[len2++]='0';
               }
           }
           if(l1<l2)
           {
               for(int i=l1,j=l2;j>=0;i--,j--)
               {
                   s4[len2++]=s2[j];
                   if(i>=0)
                   s3[len1++]=s1[i];
                   else
                    s3[len1++]='0';
               }
           }
           for(int i=max(l1,l2),j=0;i>=0;i--,j++)
           {
               s1[j]=s3[i];
               s2[j]=s4[i];
           }
       }
           if(strcmp(s1,s2)>0)
            printf(">\n");
           else
            if(strcmp(s1,s2)<0)
           printf("<\n");
           else
            if(strcmp(s1,s2)==0)
            printf("=\n");
    }
  return 0;
}

發佈了35 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章