C语言代码计算数字1到131个位 十位 百位 分别出现了多少次数字1输出结果
#include <stdio.h>
int countOnes(int num) { int count = 0; while (num > 0) { if (num % 10 == 1) count++; num /= 10; } return count; }
int main() { int onesDigitCount = 0, tensDigitCount = 0, hundredsDigitCount = 0;
for (int num = 1; num <= 131; num++) {
onesDigitCount += countOnes(num);
int tensDigit = num / 10 % 10;
if (tensDigit == 1)
tensDigitCount++;
int hundredsDigit = num / 100;
if (hundredsDigit == 1)
hundredsDigitCount++;
}
printf("Ones digit count: %d\n", onesDigitCount);
printf("Tens digit count: %d\n", tensDigitCount);
printf("Hundreds digit count: %d\n", hundredsDigitCount);
return 0;
原文地址: https://www.cveoy.top/t/topic/iL9Y 著作权归作者所有。请勿转载和采集!