PTA: in the end how two (15 points) (C language)

An integer "committed two degree" 2 ratio of the number of its digits is defined as the number contained. If this number is negative, the degree of increase of 0.5 times; if still even, then doubled. For example a 11-digit number is -13142223336, including 3 2, and is negative, is an even number, it is made of two degree calculation: 3/11 × 1.5 × 2 × 100%, of about 81.82 percent. This question will ask you to calculate how much a given integer in the end two.

Input format:
given a first line of input integer not more than 50 N.

Output Format:
Output made two degree N in a row, to two decimal places.

Sample input:
-13,142,223,336

Sample output:
81.82%

#include <stdio.h>
#include <string.h>

int main()
{
    int sign;
    int notodd = 1;
    int fool;
    char str[51] = {0};
    int i, j, n;
    float power = 1.0;
    i = 0;
    scanf("%s", &str);
    if (str[0] == '-')
    {
        sign = -1;
        i++;
    }
    else
        sign = 1;
    n = 0;
    while(str[i])
    {
        if (str[i] == '2')
            n++;
        i++;
    }
    j = (sign == -1) ? strlen(str) - 1 : strlen(str);
    if ((str[strlen(str) - 1] - '0') % 2 == 0)
        notodd = 2;
    if (sign == -1)
        power = 1.5;
    float sum;
    sum = n*1.0 / j *power*notodd*100;
    printf("%0.2f%%", sum);

    return 0;
}
Published 58 original articles · won praise 21 · views 602

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105399620
Recommended