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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值