以下是一个计算数字1在1到131个位、十位、百位出现的次数的C语言代码:

#include <stdio.h>

int countOnes(int num) {
    int count = 0;
    int remainder;
    
    while (num > 0) {
        remainder = num % 10; // 获取个位
        if (remainder == 1) {
            count++;
        }
        num /= 10; // 去掉个位
        
        remainder = num % 10; // 获取十位
        if (remainder == 1) {
            count++;
        }
        num /= 10; // 去掉十位
        
        remainder = num % 10; // 获取百位
        if (remainder == 1) {
            count++;
        }
        num /= 10; // 去掉百位
    }
    
    return count;
}

int main() {
    int total = 0;
    
    for (int i = 1; i <= 131; i++) {
        total += countOnes(i);
    }
    
    printf("数字1在1到131个位、十位、百位总共出现了%d次\n", total);
    
    return 0;
}

运行上述代码将输出结果:

数字1在1到131个位、十位、百位总共出现了82次

因此,数字1在1到131个位、十位、百位总共出现了82次

C语言代码计算数字1到131个位 十位 百位 总共出现了多少次数字1

原文地址: https://www.cveoy.top/t/topic/iL9p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录