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;
}
全部评论

相关推荐

牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
哇哇的菜鸡oc:他这不叫校招offer,而是实习offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务