AC:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
double a,c;
int b;
cin>>a>>b>>c;
double low=0,high=100,mid,s,n;
while(high - low > 0.0001){
mid=(low+high)/2;
// printf("%.3f%%\n",mid*100);
s=a;
for(int i=0;i<b;i++){
s=s*(1+mid/100.0)-c;
}
// cout<<"S:"<<s<<endl;
if(s<0){
low=mid;
}
else{
high=mid;
}
}
printf("%.3lf%%\n",mid);
return 0;
}
一刷:
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int judge(double a,int b,double c,double x){
double n;
for(int i=1;i<=b;i++){
n=a*(1+x)-c;
a=n;
}
if(fabs(n)<0.1)
return 1;
else if(n>0)
return 0;
else
return 2;
}
int main(){
double a,c,x;// 0.00797;
int b;
double ans;
cin>>a>>b>>c;
double low=0,high=1;
while(1){
double mid=(low+high)/2;
int v=judge(a,b,c,mid);
if(v==1){
printf("%.3f%%\n",mid*100);
break;
}
else if(v==0){
high=mid;
}
else
low=mid;
}
return 0;
}
二刷:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
double a,c;
int b;
cin>>a>>b>>c;
double low=0,high=1,mid,s,n;
while(low<=high){
mid=(low+high)/2;
// printf("%.3f%%\n",mid*100);
s=a;
for(int i=0;i<b;i++){
s=s*(1+mid)-c;
}
// cout<<"S:"<<s<<endl;
if(fabs(s)<=0.01){
break;
}
else if(s<0){
low=mid;
}
else{
high=mid;
}
}
printf("%.3f%%\n",mid*100);
return 0;
}