P1980 计数问题 试计算在区间 11 到 n n的所有整数中,数字 x(0 ≤ x ≤ 9)x(0≤x≤9)共出现了多少次?例如,在 11到 11 11中,即在 1,2,3,4,5,6,7,8,9,10,111,2,3,4,5,6,7,8,9,10,11 中,数字 11 出现了 44 次。 输入输出格式 输入格式: 22个整数n,xn,x,之间用一个空格隔开。 输出格式: 11个整数,表示

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, x, i, j, m, N, s[10], a, b, c, d, e, f, h;
    scanf("%d %d", &n, &x);
    m = 0;
    for (i=1; i<=n; i++)    //n次循环,遍历n个数
    {
        a = i/1000000;          //分解1000000内数字的各个位数字
        b = (i%1000000) / 100000;
        c = (i%100000) / 10000;
        d = (i%10000) / 1000;
        e = (i%1000) / 100;
        f = (i%100) / 10;
        h = i%10;
        s[0] = h;       //将各个位上的数字赋值到数组s中
        s[1] = f;
        s[2] = e;
        s[3] = d;
        s[4] = c;
        s[5] = b;
        s[6] = a;
        if(i < 10) N = 1;       //判断数字位数,将位数赋值给N作为循环搜索条件
        if(9<i  && i<100) N = 2;
        if(99<i  && i<1000) N = 3;
        if(999<i  && i<10000) N = 4;
        if(9999<i  && i<100000) N = 5;
        if(99999<i  && i<1000000) N = 6;
        if(999999<i  && i<10000000) N = 7;
        for (j=0; j<N; j++)     //对N位数的N次循环搜索
        {
            if (s[j] == x)
            {
                m++;
            }
        }
    }
    printf("%d\n", m);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Tristan-Adams/p/9648005.html