比较大数的大小(10**6) c++ python

来源:互联网 发布:ubuntu系统能做什么 编辑:程序博客网 时间:2024/06/06 12:23
                                                              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/printf instead 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 "=".



Examples

Input
9
10
Output
<
Input
11
10
Output

>

Input

00012345
12345

Output

=

Input

0123
9

Output

>

Input

0123
111

Output

>


1.第一种方法是在字符串数组前面加上前导0 直到题目所给定的最大长度!


# _*_ coding:utf-8 _*_import sysa = raw_input()b = raw_input()a = '0' * (10**6 - len(a)) + ab = '0' * (10**6 - len(b)) + bif a > b :    print ">"elif a < b :    print "<"else :    print "="-------------------------------------------------------------------------------------------第二种写法;a, b = [raw_input().rjust(10**6, '0') for i in range(2)]if a == b:    print("=")elif a < b:    print("<")else:    print(">")

-----------------------------------------------------------------------------------------
第三种写法<pre name="code" class="python"># -*-coding:utf-8-*-a,b=raw_input(),raw_input();len1=max(len(a),len(b))a,b=a.zfill(len1),b.zfill(len1)print ['<','=','>'][cmp(a,b)+1]
----------------------------------------------------------------------------------------
c++代码 //by  wyc#include<bits/stdc++.h>using namespace std;string a,b;int main(){    cin>>a>>b;    reverse(a.begin(),a.end());    reverse(b.begin(),b.end());    int lena = a.length();    int lenb = b.length();    a.append(1e6-lena, '0');    b.append(1e6-lenb, '0');    reverse(a.begin(), a.end());    reverse(b.begin(), b.end());    if(a == b)        cout<<"="<<endl;    else if( a > b)        cout<<">"<<endl;    else        cout<<"<"<<endl;}



--------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;int main(){int i,j,k;char a[1000009],b[1000009];scanf("%s%s",&a,&b);i=strlen(a);j=strlen(b);reverse(a,a+i);reverse(b,b+j);while(i<j) a[i++]='0';while(i>j) b[j++]='0';k=i-1;while(k>=0 && a[k]==b[k]) k--;if(k<0) printf("=\n");else if(a[k]<b[k]) printf("<\n");else printf(">\n");return 0;}





 
2.第二种方法是把输入的数的前导0给去掉之后进行比较
from sys import stdinaa = stdin.readline().strip()b = stdin.readline().strip()def par(a): l = len(a) for i in xrange(l):  if a[i]!='0':   return a[i:] return '0'aa = par(aa)b = par(b)def ch(a,b): if len(a) > len(b):  return '>' if len(a) < len(b):  return '<' if a==b:  return '=' if a>b:  return '>' return '<'print ch(aa,b)


----------------------------------------------------------------------------------------------------------------------------



#include<bits/stdc++.h>using namespace std;char a[1000080];char b[1000080];int main() {gets(a);gets(b);int lena = strlen(a);int lenb = strlen(b);int i = 0;while (a[i] == '0') {i++;}int j = 0;while (b[j] == '0') {j++;}int lena2 = lena - i;int lenb2 = lenb - j;if(lena2>lenb2){cout<<">"<<endl;exit(0);}if(lena2<lenb2){cout<<"<"<<endl;exit(0);}while(a[i]!=0){if(a[i]>b[j]){cout<<">"<<endl;exit(0);}if(a[i]<b[j]){cout<<"<"<<endl;exit(0);}i++,j++;}cout<<'='<<endl;return 0;







rjust参数

  • width -- 指定填充指定字符后中字符串的总长度.
  • fillchar -- 填充的字符,默认为空格。

返回值

返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串

实例

以下实例展示了rjust()函数的使用方法:

#!/usr/bin/pythonstr = "this is string example....wow!!!";print str.rjust(50, '0');

以上实例输出结果如下:

000000000000000000this is string example....wow!!!


1 0