PAT A1060 Are They Equal(含样例2、3、4、6数据)

本文介绍了一种用于判断两个浮点数在特定精度下是否等价的算法,通过字符串处理去除前导零和小数点,统一化为整数形式进行比较,适用于精确数学计算场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PAT A1060 Are They Equal

tag: [string]

在这里插入图片描述

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
  • 生词
wordmeaning
with simple chopping简单的切割
non-negetive非负
assumev.假设
without rounding没有四舍五入
  • 思路:

用string保存浮点数,先把多余的前导零去掉,去掉后剩余部分,根据小数点位置分为3种情况:

1)【原】0000.12345 -> .12345 或 .0012345
2)【原】000012.345 -> 12.345
3)【原】000012345 -> 12345 //相当于小数点在最末尾

分别处理,首先计算出指数e:
1)中e为.后面第一个有效数字(非0)的位置减1(如.123e=1-1=0, .00123e=3-1=2),可以通过不断从前面截断字符串的方式实现;2) 和 3)e就是小数点的位置(如12.345e=2

切割出有效位(获得子串),分为2种情况:
1)有效位数够 -> 直接截取前N(去掉.
2)有效位不足 -> 末尾补0(用循环不断s += '0')

最后,比较,格式化输出即可;

  • TIPS:

注意不要落掉 0000 0000.00这种情况,如果全0 -> 经过上面对string的处理后,s为空,对应的字串就是精度要求个数的0,对应的指数e为0

  • code1:
#include <string> 
#include <iostream>
using namespace std;

string deal(string s, int &e, int N){
	while(s.length() > 0 && s[0] == '0'){
		s.erase(s.begin());
	}
	if(s[0] == '.'){
		s.erase(s.begin());
		while(s.length() > 0 && s[0] == '0'){
			s.erase(s.begin());
			e--;
		}			
	}
	else{
		if(s.find(".") != string::npos){
			e = s.find(".");
		}
		else{
			int n = 0;
			for(string::iterator it = s.begin(); it != s.end(); it++){
				n++;
			}
			e = n;
		}
	}
	if(s.length() == 0){
		e = 0;
		string sub1;
		for(int i = 0; i < N; ++i){
			sub1 += "0";
		}
		return sub1;
	}
	else{
		if(s.find(".") != string::npos){
			s.erase(s.begin() + s.find("."));
		}
		string sub = s.substr(0, N);
		return sub;
	}
}

void Output(string s1, string s2, int e1, int e2){
	if(s1 == s2 && e1 == e2){
		cout << "YES 0." << s1 << "*10^"
				<< e1;
	}
	else{
		cout << "NO 0." << s1 << "*10^"
				<< e1 << " " << "0." << s2
				<< "*10^" << e2; 
	}
}

int main(){
	int N;
	string s1, s2;
	int e1 = 0, e2 = 0;
	cin >> N >> s1 >> s2; 
	string sub1 = deal(s1, e1, N);
	string sub2 = deal(s2, e2, N);
	Output(sub1, sub2, e1, e2);
	return 0;
}
  • code2: 算法笔记
#include <string>
#include <iostream>
using namespace std;
int N, e1, e2;;

void deal(string &s, int &e){
	while(s.length() > 0 && s[0] == '0'){
	//Q1:len > 0 需要加吗? 
	//去掉前导零 
		s.erase(s.begin()); 
	}
	if(s[0] == '.'){
	//如果是" .0012345 "类型的
		s.erase(s.begin());
		while(s[0] == '0'){
			s.erase(s.begin());
			e--;
		} 
	}
	else{
	//如果是" 12345"或" 123.45"类型的
		for(int i = 0; i < s.length(); ++i){
			if(s[i] == '.'){
			//如果找到" ." 删除"." 跳出循环
				s.erase(s.begin() + i);
				break; 
			}
			e++;
		}
	}
	if(s.length() == 0){
	//如果全是由0组成,如"000.00"或"0000"
		e = 0; 
	}
	//经过处理后的string都变成了12345的形式 
	//下面获得长度N的子串,如果长度不足,+='0' 
	int len = s.length(); 
	if(s.length() < N){
		for(int i = 0; i < N-len; ++i){
			s += '0';
		}
	}
	else{
		s.erase(N, s.length());
		//或s.erase(s.begin()+N, s.end()); 
	}
}

void equal(string s1, string s2){
	if(s1 == s2 && e1 == e2){
		cout << "YES 0." << s1 << "*10^" << e1;
	}
	else{
		cout << "NO 0." << s1 << "*10^" << e1 <<
				" 0." << s2 << "*10^" << e2;
	}
}

int main(){
	string s1, s2;
	cin >> N >> s1 >> s2;
	deal(s1, e1); deal(s2, e2);
	equal(s1, s2);
	return 0;
} 
  • 思路3:统一化:全转化为处理整数,初始 e=0

step 1:无论小数点在哪,先把小数点移到最末尾(转化为整数),如 0.00123 = 000123 * 10^-51.2300 = 12300 * 10 ^ -4 …, 即 小数点左移:e = 小数点到末尾的距离 = point_pos - s.size()
step 2: 问题转化为保留一个整数有效位的问题:000123 * 10 ^-5 = 0.123 * 10 ^ -2 12300 * 10 ^ -4 = 0.123 * 10 ^ 1

即:去掉前导零0后!把小数点右移到最前面:e += s.size()

【注意】去掉前导零后字符串可能为空,说明输入为: “0”,应令 e = 0;

  • 测试样例:
样例 2 : 3 122 122.00
output: YES 0.122*10^3

样例 3:  3 0.12 0.012 
output: NO 0.120*10^0 0.120*10^-1

样例 4:   5 00643.0008   0643.0
output: YES 0.64300*10^5

样例 6 : 6 000000000 000000.00 
output: YES 0.000000*10^0
  • T2 code:
#include <bits/stdc++.h>
#include <string>
using namespace std;

void Change(string & s, int & e, int n){
	int pos = s.find(".");
	if(pos != string::npos){
		e = (pos + 1) - s.size();
		s.erase(pos, 1);
	}
	while(s[0] == '0') s.erase(0, 1);
	if(s.size() == 0) e = 0;
	else e += s.size();
//	e += s.size();	//Wrong 1:未考虑0: 4 0000 0000.00 
	if(s.size() < n){
		string tmp(n - s.size(), '0');
		s += tmp;
	}else{
		s = s.substr(0, n);
	}
}

int main(){
	int n, e1 = 0, e2 = 0;
	string s1, s2;
	cin >> n >> s1 >> s2;
	Change(s1, e1, n);
	Change(s2, e2, n);
	if(s1 == s2 && e1 == e2){
		printf("YES 0.%s*10^%d", s1.c_str(), e1);
	}else{
		printf("NO 0.%s*10^%d 0.%s*10^%d", s1.c_str(), e1, s2.c_str(), e2);
	}
	return 0;
} 

  • T3 code:
#include <bits/stdc++.h>
using namespace std;


string GetE(string s, int n)
{
    int pos_point = s.size(), pos_value = -1;
    bool lock = true;
    for(int i = 0; i < s.size(); ++i)
    {
        if(s[i] == '.') pos_point = i;
        if(lock && '0' < s[i] && s[i] <= '9')
        {
            pos_value = i;
            lock = false;
        }
    }
    int e, cnt = 0;
    string ans = "0.", ans_suffix = "*10^";
    if(pos_value == -1) //无效输入: 0000000 00000.0000
    {
        e = 0;
    }else
    {
        e = pos_point - pos_value;
        for(int i = pos_value; cnt < n && i < s.size(); ++i)
        {
            if(s[i] != '.')
            {
                ans.push_back(s[i]);
                cnt++;
            }
        }
    }
    while(cnt < n)
    {
        ans.push_back('0');
        cnt++;
    }
    e = e < 0 ? e + 1 : e;
    return ans + ans_suffix + to_string(e);
}

int main()
{
    int n;
    scanf("%d", &n);
    string num1, num2;
    cin >> num1 >> num2;
    string ans1 = GetE(num1, n);
    string ans2 = GetE(num2, n);
    if(ans1 == ans2)
    {
        printf("YES %s", ans1.c_str());
    }else
    {
        printf("NO %s %s", ans1.c_str(), ans2.c_str());
    }
    return 0;
}

  • T4 code:
#include <bits/stdc++.h>
using namespace std;

void Change(string & s, int & e)
{
    auto it = s.find(".");
    if(it != s.npos)
    {
        e = -(s.size() - 1 - it);
        s.erase(it, 1);
    }
    while(s[0] == '0') s.erase(0, 1);
    if(s.size() == 0)
    {
        e = 0;
    }else
    {
        e += s.size();
    }
}
void Judge(string s1, string s2, int e1,int e2, int n)
{
    string sub1 = s1.substr(0, n), sub2 = s2.substr(0, n);
    while(sub1.size() < n) sub1 += '0';
    while(sub2.size() < n) sub2 += '0';
    if(sub1 == sub2 && e1 == e2)
    {
        printf("YES 0.%s*10^%d", sub1.c_str(), e1);
    }else
    {
        printf("NO 0.%s*10^%d 0.%s*10^%d", sub1.c_str(), e1, sub2.c_str(), e2);
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    string n1, n2;
    int e1 = 0, e2 = 0;
    cin >> n1 >> n2;
    Change(n1, e1);
    Change(n2, e2);
    Judge(n1, n2, e1, e2, n);
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值