首页 > 试题广场 >

从 1到 2018 这 2018 个数中,共有____[$#

[填空题]
从 1到 2018 这 2018 个数中,共有____1______个包含数字 8的数。包含数字8 的数是指有某一位是“8”的数, 例如“2018”与“188”。
先算出不含8的数的个数
一位数的:8个
两位数的:8*9个
三位数的:8*9*9个
小于两千的四位数的:9*9*9个
大于等于两千的有:19-2=17个
以上一共是1474个
答案为:2018-1474=544个
发表于 2019-10-02 18:02:03 回复(1)

    int count =0;
    
    for(int a=1;a<=2018;a++) {

           int temp1;
        int k;
        temp1=a;
        for( k=0;temp1>0;k++){
            
            temp1=temp1/10;

        }
    
        int temp2;
        temp2 =a;
        for(int m=1;m<=k;m++){
        
            temp2= temp2%10;
            if(temp2 == 8)  count ++;
            temp2= a/pow(10,m);
                        
        }            
    }
    cout<<count;
//这个算法有错吗,结果是602
发表于 2022-10-26 09:49:57 回复(1)