牛客巅峰赛
class Solution {
public:
/**
* 返回一个严格四舍五入保留两位小数的字符串
* @param n int整型 n
* @return string字符串
*/
string Probability(int n) {
// write code here
if(n==1) {
string s("1.00");
return s;
}
else if(n<=10){
double x=1,y=2;
n--;
while(n)
{
if(n&1) x*=y;
n>>=1;
y*=y;
}
int t=(int) 1.0/x*100;
if(1.0/x*100-1.0*t>=0.5) t++;
string ans;ans+="0.";ans+=t/10+'0';ans+=t%10+'0';
return ans;
}
else{
string s("0.00");
return s;
}
}
};
#题解#
