PTA :天梯赛练习题
7-12 到底有多二 (15分)
一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一个给定整数到底有多二。
输入格式:
输入第一行给出一个不超过50位的整数N。
输出格式:
在一行中输出N犯二的程度,保留小数点后两位。
输入样例:
-13142223336
输出样例:
81.82%
问题代码但是可以AC
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char numbers[55];
int main()
{
double ans=1.0;
double count=0;
cin>>numbers;
int len=strlen(numbers);
for(int i=0; i<len; i++)
{
if(numbers[i]=='2')
count++;
}
int c=(numbers[(len-1)]-'0');
if(numbers[0]=='-')
{
ans*=1.5;
len--;
}
ans=ans*count/len;
if(c%2==0){
ans*=2.0;
}
printf("%0.2lf%%",ans*100);
return 0;
}

==本地运行结果是错误的,但是网站上可以AC,不知道是什么原因==