牛客巅峰赛

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;
        }
    }
};


#题解#
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务