C语言计算1到131数字中个位、十位、百位出现次数
#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/qsni 著作权归作者所有。请勿转载和采集!