题目:
A == B ? |
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 92 Accepted Submission(s): 48 |
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 |
Author
8600 && xhd
|
Source
校庆杯Warm Up
|
Recommend
linle
|
题目分析:
简单题。一开始看了,以为直接比较就行了。后来才知道。这两个数可能很大,并且可能存在"1.0000000 1"
这种情况。一开始用的是Java来做,很快就AC乐,因为对于这种跟大数沾边的题目,用Java做很快。但是后来还是用C/C++再A乐一遍。
代码如下:
Java版本
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
BigDecimal a = scanner.nextBigDecimal();
BigDecimal b = scanner.nextBigDecimal();
a = a.stripTrailingZeros();//用于处理一个数字后面的无效0
b = b.stripTrailingZeros();
if(a.compareTo(b) == 0){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
C/C++
/*
* a1.cpp
*
* Created on: 2015年3月26日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1000000;
/**
* 处理一个数字后面的无效0
*
*/
char* change(char str[]) {
int len = strlen(str);//获取这个字符串的长度
/**
* strchr(s,c)的含义:在s这个字符串中找到c字符第一次出现的位置
*/
if (strchr(str, '.') != NULL) {//如果这个数字存在小数点
while (str[--len] == '0');//那么把小数点后面的无效0全部去掉
if (str[len] == '.') {//如果这个数字小数点后面全部都是无效0
len--;//那么把这个小数点也去掉
}
str[len + 1] = '\0';//形成处理后的字符串
}
return str;//返回处理后的字符串
}
int main() {
char str1[maxn];
char str2[maxn];
while (scanf("%s %s", str1, str2) != EOF) {
if (strcmp(change(str1), change(str2)) == 0) {//如果这两个字符串相等
printf("YES\n");//那么就返回YES
} else {//否则
printf("NO\n");//返回NO
}
}
return 0;
}
以下附上一开始TLE的版本。个人感觉之所以是TLE,不是WA。很可能是因为我读入数据的时候是以%d的形式读进去,而数据中可能存在1.00000这样的数据,导致一直没有读取成功。
/*
* a.cpp
*
* Created on: 2015年3月26日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int a,b;
while(scanf("%d%d",&a,&b)!=EOF){
if(a == b){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}