10000到99999中不含4的数的个数(两种方法)

对所需判断数的每一位数字进行判断:
#include <stdio.h>

int main() {
    int count = 0;
    for (int num = 10000; num <= 99999; num++) {
        int temp = num;
        int has_four = 0;
        while (temp > 0) {
            int digit = temp % 10;
            if (digit == 4) {
                has_four = 1;
                break;
            }
            temp /= 10;
        }
        if (!has_four) {
            count++;
        }
    }
    printf(&quot;%d\n&quot;, count);
    return 0;
}

把所需判断数转化为字符串进行判断:
#include <stdio.h>

int main(){
    int count=0;
    char str[8];
    for(int i=10000;i<=99999;i++){
        sprintf(str,&quot;%d&quot;,i);
        for(int j=0;j<5;j++){
            if(str[j]=='4'){
                count++;
                break;
            }
        }
    }
    printf(&quot;%d&quot;,90000-count);

return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务