題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2054
A == B ?
Time Limit: 1000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 141430????Accepted Submission(s): 22790
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
這個就不用翻譯了吧大家都一應該看得懂,一開始看到這個題目時我就覺的這個好像好簡單啊然后我就沒有多想其它的了所以我一開始的代碼就是
#include<stdio.h>
int main()
{
? ? int A,B;
? ? while(scanf("%d%d",&A,&B)!=EOF)
? ? {
? ? ? ? if(A==B) printf("YES\n");
? ? ? ? else printf("NO\n");
? ? }
? ? return 0;
}
果然我還是太年輕了我就得到了超時了边篮,然后我才發(fā)現(xiàn)輸入比較的數(shù)應該是很大的不可以這樣比較;
然后經(jīng)過大佬的提示
1.如果兩個數(shù)是浮點型比較的話,一般不會用等號,因為這樣是不準確的,一般都是作差,只要差小于某個精度就認為是相等的,
2.只要考慮小數(shù)點后零以及20==20.的情況
3.用字符數(shù)組
代碼如下:
#include<stdio.h>
#include<string.h>
char a[1000000];
char b[1000000];
void delete0 (char a[])
{
int i,len=strlen(a);
? ? int flag = 0;
? ? char ch = '.';
? ? for(i=0;i<len;i++)
? ? ? ? if(a[i]==ch)
? ? ? ? {
? ? ? ? ? ? flag = 1;
? ? ? ? ? ? break;
? ? ? ? }
? ? if(flag)
? ? {
? ? ? ? while(a[len-1]=='0')
? ? ? ? {
? ? ? ? ? ? a[len-1] = '\0';
? ? ? ? ? ? len--;
? ? ? ? }
? ? ? ? if(a[len-1]=='.')
? ? ? ? ? ? a[len-1] = '\0';//(這里要注意把后面0去完后,最后一位是否為小數(shù)點,是則改成結(jié)束符)
? ? }
}
int main()
{
while(~scanf("%s%s",a,b))
{
delete0 (a);
delete0 (b);
if(strcmp(a,b)==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
總結(jié)一下:
1.scanf("%s%s",a,b) 在數(shù)組面前不用&符號
2.strcmp(a,b)==0 比較a,b字符串數(shù)組是否相同逛漫,相同返回值為0
3.len=strlen(a) strlen函數(shù)可以測出a的實際長度因為它遇到'\0'就會結(jié)束
4.??char a[1000000]; 數(shù)組定義的大一點 a[len-1] 由于數(shù)組是從0開始索引的
附大佬的java版的代碼簡直就是作弊啊
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
BigDecimal A = in.nextBigDecimal();
BigDecimal B = in.nextBigDecimal();
if(A.compareTo(B)==0) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
}