問題
http://community.topcoder.com/stat?c=problem_statement&pm=6125&rd=11123
解き方
問題分の通りに実装すればよい。
ただし、c/tの計算はそれぞれc,tをかけてから割り算を行うとオーバーフローするので
c/tを毎回掛けて計算する必要がある。
コード
class ApproximateDivision {
public: double quotient(int a, int b, int terms) {
int t=1;
while(t<b)t*=2;
int c=t-b;
double ret=0,add=1/(double)t;
FORE(i,0,terms){
ret+=add;
add*=c/(double)t;
}
return ret*a;
}
};
public: double quotient(int a, int b, int terms) {
int t=1;
while(t<b)t*=2;
int c=t-b;
double ret=0,add=1/(double)t;
FORE(i,0,terms){
ret+=add;
add*=c/(double)t;
}
return ret*a;
}
};