找到规律后如下,只需要用高精度加法和四舍五入(本质也是高精度加法就能做),如果没有找到规律,就得自己写高精度乘法和加法,不熟练很容易错。
//#include<bits/stdc++.h>
#include<iostream>
#include<math.h>
using namespace std;
#define int long long
#define endl '\n'
string d;
int n;
//高精度加
string my_add(string a){
int len = a.size(),x = 0;
string temp = a;
reverse(a.begin(),a.end());
reverse(temp.begin(),temp.end());
for(int i=0;i<a.size();i++){
int y = temp[i]-'0'+a[i]-'0'+x;
if(y<=9){
a[i]=y+'0';
x=0;
}else{
a[i]=y%10+'0';
y/=10,x=y;
}
}
if(x)a.append(1,x+'0');
reverse(a.begin(),a.end());
return a;
}
//四舍五入
string my_round(string ans){
unsigned long index = ans.find('.');
char ch = ans[index+1];
ans = ans.erase(index);
if(ch-'0'>=5){
reverse(ans.begin(),ans.end());
unsigned long len = ans.size();
int x = 1;
for(int i=0;i<len;i++){
int y = ans[i]-'0'+x;
if(y<=9){
ans[i]=y+'0';
x=0;
}else{
ans[i]=y%10+'0';
y/=10,x=y;
}
}
if(x)ans.append(to_string(x));
reverse(ans.begin(),ans.end());
return ans;
}else return ans;
}
void solve(){
cin>>n>>d;
int len = d.size();
if(d[len-1]=='0'){d.pop_back();len--;}
int index = d.find('.');
int re_index = len-index;
auto it = remove(d.begin(), d.end(), '.');
string ans="";
d.erase(it,d.end());
for(int i=1;i<=n;i++){
d = my_add(d);
}
ans = d;
ans.insert(ans.begin()+ans.size()-re_index+1,1,'.');
cout<<my_round(ans)<<endl;
}
signed main( ){
int T = 1;
while(T--)solve();
return 0;
}