PAT A 1060 Are They Equal (25)(25 分)

15 篇文章 0 订阅
4 篇文章 0 订阅

题目介绍:

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

算法思路:

我个人感觉这道题的难度还是很大的,主要的难点在于需要考虑的情况太多了(一部分原因也是有点贪了,想把合寻找数量级和找到前N为有效数组的操作进行合并)。自己分析之后我打算把函数分开。

第一步,确定数量级。具体思路为:

          1、删除输入数字之前的0,例如像00014这样的数组

          2、删除完之后会发现一个问题,就是0,就会把所有数字都删除完毕,此时我们把字符串赋值为0,数量级赋值为0

          3、如果删除后第一位为小数点时,我们把小数点删掉,继续删除字符串开始的0,每删一位,数量级减一

         4、此时,我们剩下需要处理的就是大于一的数字。 我们先查看是否存在小数点,使用find(".")进行,如果返回为-1,(string::npos),则输入为整数,直接获取剩余的位数为他的数量级,否则的话,local即为他的数量级;然后为了与其他分支统一,我们在这里也把他的小数点删除掉。

第二步,获得前N位有效数组,此时,我们已经删除掉无效位,所以先把不足N为的字符串补足N位,然后截取前N为进行比较,如果相同则相等

第三步: 格式化输出

代码展示:


#include <string>
#include <iostream>
using namespace std;
int N;
void getK(string  &num,int &n){
	while(num[0] == '0')  num.erase(num.begin());
	if(num.length() == 0){ //处理0
		num = "0";
		n = 0;
		return;
	}else if(num[0] == '.'){  //处理小于1的数
		num.erase(num.begin());
		n=0;
		while(num[0]=='0'){
			n--;
			num.erase(num.begin());
		}
		if(num.length() == 0){
			n=0;
			num = "0";
		}
		return;
	}else{    //处理大于1的数
		int local = num.find(".");
		if(local == string ::npos){  //如果这个数字没有小数部分
			n = num.length();
			return ;
		}else{   //处理混合型的数组
		  
			n = local;
			num.erase(num.begin()+local);
			return;
		}
	}
}

void getN(string &num){
	while(num.length()<N){
		num ="0" + num;
	}
}
bool compare(string &num1,string &num2){
	getN(num1);
	getN(num2);
	num1= num1.substr(0,N);
	num2 = num2.substr(0,N);
	if( num1 == num2 ){
		cout<<"YES ";
		return true;
	}else{
		cout<<"NO ";
		return false;
	}
}

int main(){
	
	string num1,num2;
	cin>>N>>num1>>num2;
	int e1,e2;
	getK(num1,e1);
	getK(num2,e2);
	if(compare(num1,num2)){
		cout<< "0."<<num2<<"*10^"<<e2 <<endl;
	}else{
		cout<< "0."<<num1<<"*10^"<<e1<<" ";
		cout<< "0."<<num2<<"*10^"<<e2<<endl;
	}


	return 0;
}

注意:

写代码一定要把思路理清楚,不要上来就动手,前期考虑不足的话,很容易在后期修改的过程中思路越来越乱。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值