7-35 到底有多二 (15分)
7-35 到底有多二 (15分)
一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336
是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一个给定整数到底有多二。
输入格式:
输入第一行给出一个不超过50位的整数N
。
输出格式:
在一行中输出N
犯二的程度,保留小数点后两位。
输入样例:
-13142223336
输出样例:
81.82%
#include<stdio.h>
#include<string.h>
int main()
{
int weishu=0;
int count_2=0;
int n;
int dig;
int ou=1;
double fu=1;
char a[55];
scanf("%s",a);
weishu=strlen(a);
if((a[weishu-1]-'0')%2==0)
{
ou+=1;
}
int i;
for(i=0;i<weishu;i++)
{
if(a[i]=='2')
count_2++;
}
if(a[0]=='-')
{
fu+=0.5;
}
if(a[0]=='-'||a[0]=='+')
{
weishu--;
}
char c='%';
printf("%.2f%c",fu*ou*count_2/weishu*100,c);
return 0;
#include<string.h>
int main()
{
int weishu=0;
int count_2=0;
int n;
int dig;
int ou=1;
double fu=1;
char a[55];
scanf("%s",a);
weishu=strlen(a);
if((a[weishu-1]-'0')%2==0)
{
ou+=1;
}
int i;
for(i=0;i<weishu;i++)
{
if(a[i]=='2')
count_2++;
}
if(a[0]=='-')
{
fu+=0.5;
}
if(a[0]=='-'||a[0]=='+')
{
weishu--;
}
char c='%';
printf("%.2f%c",fu*ou*count_2/weishu*100,c);
return 0;
}