B - Cure

 题意:给一个数n,输出从1/(1*1)+1/(2*2)+...+1/(n*n)的值。

思路:打表存到n的数,注意当n很大的时候1/(n*n)越小,那么他就趋近于0,所以当n很大的时候我们输出趋近的值就可以了。注意在实现的时候不能直接算1/(n*n),需要1/n/n。因为这个数可能很大,所以我们用字符串读入,保留5位小数,那我们打表打到1e6就可以了。

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int N=1e6+5;
typedef long long ll;
double a[N];
string s;
void init(){
	double sum=0;
	for(int i=1;i<=1e6;i++){
		sum+=(double)(1.0/i/i);
		a[i]=sum;
	}
}
void sove(){
	string s;
	while(cin>>s){
		if(s.size()>=7){
			printf("%.5f\n",a[1000000]);
		}else{
			int t=stoi(s);//将字符串转换为整数的函数
				if(t>=1000000){
					printf("%.5f\n",a[1000000]);
				}else	printf("%.5f\n",a[t]);
		}
	}
}
int main(){
	int t=1;
	init();
	while(t--){
		sove();
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值