1060 Are They Equal (附测试点)

1060 Are They Equal (25 分)

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10​5​​with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

Input Specification:

Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and Bare the two float numbers to be compared. Each float number is non-negative, no greater than 10​100​​, and that its total digit number is less than 100.

Output Specification:

For each test case, print in a line YES if the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k(d[1]>0 unless the number is 0); or NO if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.

Note: Simple chopping is assumed without rounding.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

看似不难的一道题,思路很清晰,就是转换成规定的科学计数法之后比较

测试数据却是十分孤儿,最后符测试点

#include<bits/stdc++.h>
using namespace std;

string s1, s2, a1, a2;

string translate(int n, string s) {
	int size = s.size(), i = 0, count = 0;
	string temp;
	for (; i < size&&s[i] == '0'; i++);
	if (s[i] == '.') {
		i++;
		for (; s[i] == '0'; i++)count--;
		bool judge = false;							//防止0.0这样的数据,只有judge为true时,count才有效	
		for (; temp.size() < n&&i < size; i++) {
				temp.push_back(s[i]);
				judge = true;
		}
		while (temp.size() < n)temp.push_back('0');
		if(!judge)count=0;
	}
	else {
		for(int j=i;s[j]!='.'&&j<size;j++)count++;
		for (; s[i] != '.'&&temp.size()<n&&i<size; i++) 
			 temp.push_back(s[i]);
		if (temp.size() < n)i++;
		for (; i < size&&temp.size() < n; i++)
			temp.push_back(s[i]);
		while (temp.size() < n)temp.push_back('0');
	}
	temp += ("*10^" + to_string(count));
	return temp;
}

int main() {
	int n = 0;
	scanf("%d", &n);
	cin >> s1 >> s2;
	a1 = translate(n, s1);
	a2 = translate(n, s2);
	if (a1 == a2) cout << "YES 0." << a1;
	else cout << "NO 0." << a1 << " 0." << a2;
	return 0;
}

测试点:

 3 12300 12358.9
YES 0.123*10^5


1 12300 12358.9
YES 0.1*10^5


1 1.2300 1.23589
YES 0.1*10^1


5 1.2300 1.23589
NO 0.12300*10^1 0.12358*10^1


4 0.01234 0.012345
YES 0.1234*10^-1


5 0.01234 0.012345
NO 0.12340*10^-1 0.12345*10^-1


5 0.1234 0.12345
NO 0.12340*10^0 0.12345*10^0


0 0.11 0
YES 0.*10^0或者YES 0.0*10^0,都可以AC,测试点应该没有这个例子


1 0.1 0
NO 0.1*10^0 0.0*10^0


1 0.0 0.1
NO 0.0*10^0 0.1*10^0


1 0.0 0.000
YES 0.0*10^0


1 00.0 0.000
YES 0.0*10^0


4 00.0 0.000
YES 0.0000*10^0


5 00.0 0.000
YES 0.00000*10^0


1 05.0 5.000
YES 0.5*10^1


1 00.01 0.010
YES 0.1*10^-1

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值