1. 程式人生 > >比較大數的大小(10**6) c++ python

比較大數的大小(10**6) c++ python

                                                              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 sys
a = raw_input()
b = raw_input()
a = '0' * (10**6 - len(a)) + a
b = '0' * (10**6 - len(b)) + b
if 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 stdin
aa = 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/python

str = "this is string example....wow!!!";

print str.rjust(50, '0');

以上例項輸出結果如下:

000000000000000000thisisstring example....wow!!!