2018/09/07 阿里问答题二: N元送红包(无循环)

本文探讨了在处理大数问题时使用字符串而非double类型的优势,通过一个具体的代码示例,展示了如何利用字符串来避免数值溢出,确保计算的准确性。

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

原题:

没考虑大数问题(数字位数太长,double不够大或不够精细),用字符串处理比较好,不过懒得重新写一遍。

以下是没考虑大数问题代码:

#include<iostream>
#include<string>
using namespace std;
double first = 0, second = 0;
double temp=0,n=0,point=0;

bool judge(double first, double second, double N)
{
	if (first > 10 && second > 10 && (first == N || second == N || first + second == N))
		return true;
	else
		return false;
}
void spilt(string str, int begin, int end, double N)
{
	if (begin > end)
		cout << "false"<<endl;
	else if (str[begin] == ',')
	{
		first = second;
		if (point==0)
		second = temp;
		else
		{
			second = temp ;
			point = 0;
		}
		//cout << first << second << endl;
		bool res = judge(first, second,N);
		if (res)
		{
			cout << "true" << endl;
		}
		else
		{
			temp = 0;
			point = 0;
			spilt(str, begin + 1, end,N);
		}
	}
	else if (str[begin] == '.')
	{
		point = 1;
		spilt(str, begin + 1, end, N);
	}
	else
	{
		if (point==0)
		temp = temp * 10 + str[begin] - '0';
		else
		{
			point *= 10;
			temp = temp  + (str[begin] - '0') / point;
		}
		spilt(str,begin+1,end,N);
	}
}
int main()
{
	string str;
	double N;
	getline(cin, str);
	cin >> N;
	/*cout << str << endl;
	cout << N;*/
	str = str + ",";
	spilt(str,0,str.length()-1,N);

	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值