一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一个给定整数到底有多二。
输入格式:
输入第一行给出一个不超过50位的整数N。
输出格式:
在一行中输出N犯二的程度,保留小数点后两位。
输入样例:
-13142223336
输出样例:
81.82%
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
using namespace std;
struct node {
ll rng;
ll edg;
ll lgd;
ll sng;
}S[100005];
int main() {
string s;
while (cin >> s) {
double length = s.size();
double fu = 1.0;//判断负数
double ou = 1.0;//判断偶数
if (s[0] == '-') {
fu = 1.5;
length--;//因为除的时候只除数字长度,所以如果有负号要减一
}
if ((s[s.size() - 1] - '0') % 2 == 0) {
ou = 2.0;//判断偶数
}
double count = 0.0;//存2的个数
for (ll i = 0; i < s.size(); i++) {
if (s[i] == '2') {
count++;
}
}
double sum;
sum = ((double)(count) / (double)(length)) * ou * fu * 100;
printf("%.2f", sum);//控制小数点位数输出
cout << "%" << endl;
}
}