算法笔记 P209 例题:【PAT A1060】Are They Equal

算法笔记练习 题解合集

本题链接

题目

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 B are 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

思路

我们要干啥?

我们要把所有千奇百怪的输入都统一成这种标准形式:0.d[1]...d[N]*10^k
实际上就是要从输入中提取到关键的字符串d[1]...d[N],还有指数k。所以先定义一个结构Standard来存放一个数字的标准形式,其中d为关键的字符串,exp为指数。


代码怎么写?

假设输入数据是名为input的字符串,正式开始处理数据:

  1. 精度的问题先放一放(步骤 5 会处理),我们先提取出正确的dexp。注意input可能会有无效的前导零(比如0000123.456),所以首先把前导零都去掉;
  2. 分类讨论,先按照input中存不存在小数点来分类;
  3. 如果不存在小数点,那么d即为此时的inputexp即为此时input的长度,进入步骤 5;
  4. 如果存在小数点,那么小数点前面的整数部分是零吗?(或者说原数字小于 1 吗?)
    a. 如果原数字大于等于 1,exp即为小数点在input中的下标,d即为去掉小数点后的input
    b. 如果原数字小于 1,由于之前我们把前导零都去掉了,input现在应该是.xxx的形式。
    此时exp即为小数点后面连续的0的个数的相反数(例如.00123exp是 -2,.123exp是 0),d即为去掉小数点再去掉前导零之后的input。但是如果小数点后面全是 0,例如.00000,那么exp应该等于0d应该是"0"
  5. 还要对d进行精度的调整,假设精度为sig
    a. 若d.size() > sig,就把d后面砍掉,只取前sig位的字符;
    b. 若d.size() < sig,在d后面不断加 0 直到其长度等于sig
  6. 用这样的方法可以获取到两个数字的标准形式,就可以愉快地进行比较,输出了。

代码

#include <iostream>
#include <string>
using namespace std;
typedef struct {
	string d;
	int exp;
} Standard;
Standard getStandard(string input, int sig) {
	Standard ret;
	string digits;
	while (!input.empty() && input[0] == '0')
		input.erase(input.begin());
	auto dotPos = input.find('.');
	if (dotPos != string::npos) {
		if (dotPos == 0) {
			int cnt = 0;
			while (dotPos+cnt+1 < input.size() && input[dotPos+cnt+1] == '0')
				++cnt;
			if (dotPos+cnt+1 == input.size()) 
				ret.exp = 0;
			else
				ret.exp = -cnt;
		} else 
			ret.exp = dotPos;
		input.erase(input.begin() + dotPos);
	} else {
		ret.exp = input.size(); 
	}
	while (!input.empty() && input[0] == '0')
		input.erase(input.begin());
	ret.d = input;
	if (ret.d.size() < sig)
		ret.d.append(sig - ret.d.size(), '0');
	else if (ret.d.size() > sig)
		ret.d.erase(ret.d.begin() + sig, ret.d.end()); 
	return ret;
} 
void printStandard(Standard a) {
	cout << "0." << a.d << "*10^" << a.exp; 
} 
int main() {
	int sig;
	string str1, str2;
	cin >> sig >> str1 >> str2;
	Standard a = getStandard(str1, sig), b = getStandard(str2, sig);
	if (a.d == b.d && a.exp == b.exp) {
		cout << "YES ";
		printStandard(a); 
	} else {
		cout << "NO ";
		printStandard(a); 
		putchar(' '); 
		printStandard(b); 
	} 
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值