HDU 2054 A==B?
A == B ?
Problem Description
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
Input
each test case contains two numbers A and B.
Output
for each case, if A is equal to B, you should print "YES", or print "NO".
Sample Input
1 2
2 2
3 3
4 3
Sample Output
NO
YES
YES
NO
代码:
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn=100005; 6 7 int main() 8 { 9 //freopen("in.txt","r",stdin); 10 int i; 11 int flag1,flag2; 12 bool key1,key2,key; 13 char a[maxn],b[maxn]; 14 while(scanf("%s%s",a,b)!=EOF) 15 { 16 key=true; 17 key2=key1=false; 18 int len1=strlen(a); 19 int len2=strlen(b); 20 for(i=0;i<len1;i++) 21 if(a[i]=='.') 22 { 23 flag1=i; 24 key1=true; 25 break; 26 } 27 for(i=0;i<len2;i++) 28 if(b[i]=='.') 29 { 30 flag2=i; 31 key2=true; 32 break; 33 } 34 //key1,key2标记两个是否有小数点 35 if(!key1&&!key2) 36 if(strcmp(a,b)!=0) 37 key=false; 38 //没有小数点直接比较 39 else if(key1&&!key2) 40 { 41 for(i=0;i<flag1;i++) 42 if(a[i]!=b[i]) 43 { 44 key=false; 45 break; 46 } 47 if(key) 48 for(i=flag1+1;i<len1;i++) 49 if(a[i]!='0') 50 { 51 key=false; 52 break; 53 } 54 } 55 else if(key2&&!key1) 56 { 57 for(i=0;i<flag2;i++) 58 if(a[i]!=b[i]) 59 { 60 key=false; 61 break; 62 } 63 if(key) 64 for(i=flag2+1;i<len2;i++) 65 if(b[i]!='0') 66 { 67 key=false; 68 break; 69 } 70 } 71 /*两个字符串如果一个有一个没有,先比较整数部位,如果相同,再判断有小数点的 72 字符串小数点后面是否都是0*/ 73 else 74 { 75 if(len1<len2) 76 { 77 for(i=len1;i<len2;i++) 78 a[i]='0'; 79 a[i]='\0'; 80 } 81 else if(len2<len1) 82 { 83 for(i=len2;i<len1;i++) 84 b[i]='0'; 85 b[i]='\0'; 86 } 87 else; 88 if(strcmp(a,b)!=0) 89 key=false; 90 } 91 //如果都有小数点,再较短的字符串后面补零再比较. 92 if(key) 93 printf("YES\n"); 94 else 95 printf("NO\n"); 96 } 97 return 0; 98 }