L1-017. How many two

The "degree of committing two" of an integer is defined as the ratio of the number of twos in the number to its number of digits. If the number is negative, the degree is increased by a factor of 0.5; if it is an even number, it is increased by a factor of 1. For example, the number "-13142223336" is an 11-digit number, and there are 3 2s in it, and it is a negative number and an even number, then its 2-degree degree is calculated as: 3/11*1.5*2*100%, which is about 81.82%. This question asks you to calculate how many two a given integer is.
Input format:
The first line of input gives an integer N of no more than 50 digits.
Output format:
output the degree of N committing two in one line, keeping two decimal places.
Input sample:
-13142223336
Output sample:
81.82%
Note: The idea is very simple, first find the number of 2, and then discuss the final output result by situation

#include<stdio.h>
#include<string.h>
int main()
{
    char N[50];//定义不超过50位的整数
    int len, i, count=0;
    double n;//n一定要是双精度
    scanf("%s", N);
    len = strlen(N);
    for (i=0; i<len; i++){
        if (N[i]=='2') count++;
    }
    if (N[0]=='-'){//N[0]默认读取的是字符串中相对应数字的数值
        if (N[len-1]%2==0) n = count*1.0/(len-1)*1.5*2;//一定不能不*1.0!否则后面的是以整形数据相乘,结果为零
        else n = count*1.0/(len-1)*1.5;//当输入的数为负数时,负号同时占用了长度,所以计算位数要减去1
    }
    else{
        if (N[len-1]%2==0) n = count*1.0/len*2;
        else n = count*1.0/len;
    }
    printf("%.2f%%", n*100);//百分号的转义字符%%
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325584489&siteId=291194637