C 求数列和(数值转字符串,数值小数精度转换)[EOF 组合键Ctrl+Z结束多组数据输入]

 

void 求数列的和()
{//缘由https://bbs.csdn.net/topics/394827750
	int i=0, n=0, m=0;
	double x=1.0, he=0;
	cout << "Windows下为组合键Ctrl+Z结束" << endl;
	while (scanf_s("%d %d", &n, &m) != EOF)
	{
		he += x *= n;
		while (m > ++i){ cout << sqrt(x)<<endl; he += x=sqrt(x); }
		printf_s("%.2f\n", he);
		he = 0; x = 1.0; i = n = m = 0;
	}
}
void 求数列的和()
{//缘由https://bbs.csdn.net/topics/394827750
	int i=0, n=0, m=0,xu=0;
	double x = 1.0, he = 0, 和集合[44] = {0};
	cout << "Windows下为组合键Ctrl+Z结束" << endl;
	while (scanf_s("%d %d", &n, &m) != EOF)
	{
		he += x *= n;
		while (m > ++i){ cout << sqrt(x)<<endl; he += x=sqrt(x); }
		和集合[xu] = he; ++xu;
		he = 0; x = 1.0; i = n = m = 0;
	}
	while (xu)printf_s("%.2f\n", 和集合[--xu]);
}
void 求数列的和()
{//缘由https://bbs.csdn.net/topics/394827750
	int i = 0, n = 0, m = 0, xu = 0;
	double x = 1.0, he = 0, 和集合[44] = { 0 };
	string shuch = "";
	cout << "Windows下为组合键Ctrl+Z结束" << endl;
	while (scanf_s("%d %d", &n, &m) != EOF)
	{
		he += x *= n;
		while (m > ++i){ cout << sqrt(x) << endl; he += x = sqrt(x); }
		//和集合[xu++] = he;
		shuch += to_string((__int64)(he*100.0+0.5)/100.0) + "\n";//100.0表示2位to_string(9).find("9")==string::npos//对比字符串包含
		he = 0; x = 1; i = 0;
	}
	//while (xu)printf_s("%.2f\n", 和集合[--xu]);
	cout << shuch << endl;
}
2种正序输出

	i = xu;
	while (xu)printf_s("%.2f\n", 和集合[i - xu--]);

	i = 0;
	while (i<xu)printf_s("%.2f\n", 和集合[i++]);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值