1060. Are They Equal 解析

本文介绍了一种将各种形式的数字(包括整数、小数等)转换为统一科学记数法的方法,并通过一个C++程序实现了这一转换过程。特别地,文章详细讨论了如何处理诸如前导零、尾随零及特定精度要求等复杂情况。

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

这个题目考虑到的情况挺多的感觉。

首先科学记数法0.X 除了0以外X都不为零。

然后输入的数据也是很怪。。需要考虑进去。。

比如 0.001 这样的,小数但是小数点后面不是有效数而是0的。

再比如 00100 这样的,整数但是前面有0的。(被坑死。。。当时怎么都查不出来)

再再比如0 应该记为 0.00……0*10^0..

还有精确位数为4 数字为120这样的 还需要补0.

都考虑到了应该就好了。

补一个case3: 1 0.01 0.009

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int n;
struct node {
	string num;
	string ans;
	int dec;
	int fac;
	int e;
	node(){ans="0.";dec = 0;fac =0;e =0;};
};
node a,b;

string int2str(int n){
	if(n == 0)
		return "0";
	string t;
	while(n!=0){
		t.push_back('0'+ n%10);
		n/=10;
	}
	t.reserve();
	return t;
}


void str_cov(node & n1){
	int dec = 0 ,fac = 0 , e = 0;
	int c = 0;

	
		

	if(n1.num == "0"){
		while(c<n){
			n1.ans.push_back('0');
			c++;
		}
	}
	else{		
		int i = 0;
		if(n1.num.size() > 2 && n1.num[0] =='0' && n1.num[1] != '.')
			while(n1.num[i] == '0')
				i++;

		if(n1.num.size() > 2 && n1.num[0] =='0' && n1.num[1] == '.'){//0.0X型的小数
			i = 2;
			while(n1.num[i] == '0'){
				e--;i++;
			}
			while(c < n ){
				if(i < n1.num.size()){
					n1.ans.push_back(n1.num[i]);
					i++;
				}
				else//补0
					n1.ans.push_back('0');
				c++;
			}
		}

		else{
			while(i < n1.num.size() && n1.num[i] != '.'){
				if(c<n)
					n1.ans.push_back(n1.num[i]);
				e++;c++;i++;
			}
			if(i < n1.num.size() && n1.num[i] == '.')
				i++;

			while(c < n){//小数部分还有
				if(i < n1.num.size()){
					n1.ans.push_back(n1.num[i]);
					i++;				
				}
				else
					n1.ans.push_back('0');
				c++;
			}
		}
	}

	n1.ans += "*10^";
	if(e < 0){
		n1.ans.push_back('-');
		e = -e;
	}
	n1.ans += int2str(e);
	
}

int main(){

	cin >> n >> a.num >> b.num;

	str_cov(a);
	str_cov(b);

	if(a.ans != b.ans)
		cout << "NO " << a.ans << " " << b.ans << endl;
	else
		cout << "YES " << a.ans << endl;
	
	return 0;
}


### 使用 `Assert.AreEqual()` 比较索引值 `Assert.AreEqual()` 是单元测试框架中的一个重要方法,用于验证两个对象是否相等。当比较数组或列表的第一个元素 (`index[0]`) 时,可以按照如下方式使用该函数: ```csharp // 假设有一个整数数组 numbers 和预期的第一个元素 expectedFirstElement int[] numbers = { 1, 2, 3 }; int expectedFirstElement = 1; // 验证第一个元素是否等于预期值 Assert.AreEqual(expectedFirstElement, numbers[0]); ``` 此代码片段会检查 `numbers` 数组的第一个元素是否确实为 `expectedFirstElement` 所指定的值[^1]。 对于更复杂的数据结构,比如多维数组或其他集合类,则需注意访问特定位置的方式可能有所不同。例如,在处理二维数组的情况下,应该这样写断言语句来对比某个具体坐标的数值: ```csharp double[,] matrix = new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 } }; double expectedResultAtPosition = 2.0; Assert.AreEqual(expectedResultAtPosition, matrix[0, 1]); // 行列下标从0开始计数 ``` 另外需要注意的是,如果被测条件不满足,即实际值与期望值不符,那么 `Assert.AreEqual()` 将抛出异常并停止当前测试案例执行流程。这有助于快速定位问题所在之处[^2]。 在某些情况下,可能会遇到浮点数精度误差导致的失败情况。为了避免这种情况发生,可以在调用 `AreEqual` 方法时提供第三个参数作为允许的最大绝对差额(tolerance),从而实现近似匹配而非严格意义上的完全相同: ```csharp float actualValue = 0.1f + 0.2f; // 可能不会精确得到 0.3 float expectedValue = 0.3f; float tolerance = 0.0001f; Assert.AreEqual(expectedValue, actualValue, tolerance); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值