hdu 2054 A == B ?

A == B ?

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 64227    Accepted Submission(s): 10057


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
题目大意:看a是不是等于b 思路:分非常多种情况。有符号无符号,或者0.1 和 .1是相等的,还要注意没实用的0 我把前边后边无用的0都去掉,再分情况推断符号的问题,小数点的问题,代码 好长,有个非常短的代码,也过了,可是01和1,+1和1,等非常多情况測试都不正确,可是 竟然对了!

,,,不太理解。

附上大神超短代码。 2014,11,9 这题真烦人!

#include<stdio.h>
#include<string.h>
char a[150000],b[150000],c[150000],d[150000];
int main(){
	int i,j,k,l,lena,lenb;
	while(scanf("%s%s",a,b)!=EOF){
		lena=strlen(a);lenb=strlen(b);
		for(i=0;i<lena;i++){
			if(a[i]=='.'){
				for(j=lena-1;j>=0;j--){
					if(a[j]!='0'){
						a[j+1]='';
						break;
					}
				}
				if(a[j]=='.')
					a[j]='';
				break;
			}		
		}//把小数后边无用的0去掉 
		for(i=0;i<lenb;i++){
			if(b[i]=='.'){
				for(j=lenb-1;j>=0;j--){
					if(b[j]!='0'){
						b[j+1]='';
						break;
					}
				}
				if(b[j]=='.')
					b[j]='';
				break;
			}		
		}//把小数后边无用的0去掉
		if(strcmp(a,b)==0) printf("YES
");
		else if((a[0]=='+'&&b[0]=='-')||(a[0]=='-'&&b[0]=='+'))//符号不一样的情况 
			printf("NO
");
		else if((a[0]=='+'&&b[0]=='+')||(a[0]=='-'&&b[0]=='-')){//符号一样的情况 
			for(i=1;i<lena;i++){
				if(a[i]=='0')
					a[i]='.';
				if(a[i]!='0')
					break;
			}
			k=l=0;
			for(i=1;i<lena;i++){
				if(a[i]!='.') 
					c[k++]=a[i];	
			}//把前边的无用的0去掉
			for(i=1;i<lenb;i++){
				if(b[i]=='0')
					b[i]='.';
				if(b[i]!='0')
					break;
			}
			for(i=1;i<lenb;i++){
				if(b[i]!='.')
					d[l++]=b[i];
			}//把前边无用的0去掉 
			if(strcmp(c,d)==0) printf("YES
");
		}
		else if((a[0]=='-'&&(b[0]!='+'&&b[0]!='-'))||(b[0]=='-'&&(a[0]!='+'&&a[0]!='-')))//一个是负号一个没符号的情况 
			printf("NO
");
		else{//一个是正号一个没符号,和都没有符号的情况 
			k=l=0;
			for(i=0;i<lena;i++){
				if(a[i]=='+')
					a[i]='.';
				else if(a[i]=='0')
					a[i]='.';
				else
					break;
			}
			for(i=0;i<lena;i++)
				if(a[i]!='.')
					c[k++]=a[i];
			for(i=0;i<lenb;i++){
				if(b[i]=='+')
					b[i]='.';
				else if(b[i]=='0')
					b[i]='.';
				else 
					break;
			}
			for(i=0;i<lenb;i++)
				if(b[i]!='.')
					d[l++]=b[i];
			if(strcmp(c,d)==0)
				printf("YES
");
			else printf("NO
");	
		}
	}
	return 0;
}

//大神代码:
#include <stdio.h>
#include <string.h>
char a[150000],b[150000];
void f(char *s){
	int lens;
	lens=strlen(s);
	if(strchr(s,'.')!=NULL)	{
		while(s[--lens]=='0') ;
		if(s[lens]=='.') lens--;
		s[lens+1]='';
	}
}
int main(){
	while(scanf("%s%s",a,b)==2){
		f(a);
		f(b);
		if(strcmp(a,b)==0) printf("YES
");
		else printf("NO
");
	}
	return 0;
} 



 


原文地址:https://www.cnblogs.com/mthoutai/p/6908003.html