L1-017. 到底有多二

    xiaoxiao2021-03-25  62

    一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字“-13142223336”是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11*1.5*2*100%,约为81.82%。本题就请你计算一个给定整数到底有多二。

    输入格式:

    输入第一行给出一个不超过50位的整数N。

    输出格式:

    在一行中输出N犯二的程度,保留小数点后两位。

    输入样例: -13142223336 输出样例: 81.82% C++输出控制,保留小数点后n位由setsflags(ios::fixed)和setprecision(n)一起控制!!! #include <iostream> #include<string> #include<iomanip> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int main(int argc, char** argv) { string num; double res; int n=0,m=0; cin>>num; for(int i=0;i<num.length();i++){ if(num[i]=='2'){ ++n; } } if(num[0]=='-'){ m=num.length()-1; res=1.5*n/m; } else { m=num.length(); res=1.0*n/m; } if((num[num.length()-1]-'0')%2==0){ res*=2; } cout<<setiosflags(ios::fixed)<<setprecision(2)<<res*100<<"%\n"; return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-33659.html

    最新回复(0)