Title Link: Http://codeforces.com/problemset/problem/616/A
Title meaning: As the name implies, compare the size of two strings with a length of not more than 1e6
Simulation can be. Available in two versions, array version & pointer version.
(1) Array version (the short string is 0 from the high, until it is the same length as the long string)
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5 using namespacestd;6 7 Const intMAXN = 1e6 +5;8 CharA[MAXN], B[MAXN];9 intREV_A[MAXN], REV_B[MAXN];Ten One intcmpintlen) A { - intf =0;//0:a=b; 1:a>b; 2:a<b - //compared to the time from the high, the storage is from the low start to save the for(inti = len-1; I >=0&&!f; i--) { - if(Rev_a[i] >Rev_b[i]) { -f =1; - } + Else if(Rev_a[i] <Rev_b[i]) { -f =2; + } A } at returnF; - } - - intMain () - { - #ifndef Online_judge inFreopen ("In.txt","R", stdin); - #endif //Online_judge to + while(SCANF ("%s%s", A, b)! =EOF) { - intLa =strlen (a); the intLB =strlen (b); * $ for(inti =0; I < LA; i++) {Panax Notoginsengrev_a[la-i-1] = a[i]-'0'; - } the + for(inti =0; i < lb; i++) { Arev_b[lb-i-1] = b[i]-'0'; the } + - intFlag =0; $ //ensure that the comparison string is equal in length, 0 $ if(La < lb) {//La < lb - for(inti =0; i < Lb-la; i++) { -Rev_a[la+i] =0; the } -Flag =cmp (lb);Wuyi } the Else{//La >= lb - for(inti =0; i < la-lb; i++) { WuRev_b[lb+i] =0; - } AboutFlag =cmp (LA); $ } - if(Flag = =1) puts (">"); - Else if(Flag = =2) puts ("<"); - ElsePuts"="); A } + return 0; the}
(2) The pointer version (after filtering the prefix 0, then the size of the bit)
1 /*2 pointer version3 */4#include <iostream>5#include <cstdio>6#include <cstdlib>7#include <cstring>8 using namespacestd;9 Ten Const intMAXN = 1e6 +5; One CharA[MAXN], B[MAXN]; A - intcmpChar*S1,Char*S2) - { the //Filter Prefix 0 - while(*s1 = ='0') { -s1++; - } + while(*s2 = ='0') { -s2++; + } A at intL1 =strlen (S1); - intL2 =strlen (S2); - if(L1 >L2) { - return '>'; - } - Else if(L1 <L2) { in return '<'; - } to //A , B is equal in length (L1 = L2) + for(inti =0; i < L1; i++) { - if(*s1 < *S2) {//the value pointed to by the pointer the return '<'; * } $ Else if(*s1 > *S2) {Panax Notoginseng return '>'; - } thes1++;//The pointer moves right one +s2++; A } the return '='; + } - $ $ intMain () - { - #ifndef Online_judge theFreopen ("In.txt","R", stdin); - #endif //Online_judgeWuyi the while(SCANF ("%s%s", A, b)! =EOF) { -printf"%c\n", CMP (A, b)); Wu } - return 0; About}
Codeforces Educational codeforces Round 5 A. Comparing A Long integers