乐逍遥xwl

导航

L1-017 到底有多二

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

输入格式:

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

输出格式:

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

输入样例:

-13142223336

输出样例:

81.82%
 
 
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 using namespace std;
 5 int main()
 6 {
 7     string str;
 8     cin>>str;
 9     float len=str.size();
10     if(str[0]=='-')
11     len--;
12     float ernum=0;
13     for(int i=0;i<str.size();i++)
14     {
15         if(str[i]=='2')
16         ernum++;
17     }
18     int flag;
19     if((str[str.size()-1]-'0')%2==1)
20     flag=0;
21     else if((str[str.size()-'0']%2==0))
22     flag=1;
23     float faner=0;
24     if(str[0]!='-'&&flag==0)//正奇数
25        faner=(ernum/len)*100;
26     else if(str[0]!='-'&&flag==1)//正偶数
27        faner=(ernum/len)*2*100;
28     else if(str[0]=='-'&&flag==0)//负奇数
29        faner=(ernum/len)*1.5*100;
30     else if(str[0]=='-'&&flag==1)//负偶数
31        faner=(ernum/len)*1.5*2*100;
32     printf("%.2f%%",faner);
33     return 0;
34 }

 

 

posted on 2019-01-20 17:55  乐逍遥xwl  阅读(141)  评论(0编辑  收藏  举报