C语言——100到10000有那几个整数的个位数字之和等于5

来源:百度知道 编辑:UC知道 时间:2024/05/23 14:27:45
做出那几个整数没的数位的和等于5

#include <stdio.h>
int main(int argc, char *argv[])
{
int n,s;

for(int i=100; i<=10000; i++){
n = i;
s=0;

s += n/1000;
n -= (n/1000)*1000;

s += n/100;
n -= (n/100)*100;

s += n/10;
n -= (n/10)*10;

s += n;
if( s==5 )
printf("%d\t", i);
}
return 0;
}

结果:
104 113 122 131 140 203 212 221 230 302
311 320 401 410 500 1004 1013 1022 1031 1040
1103 1112 1121 1130 1202 1211 1220 1301 1310 1400
2003 2012 2021 2030 2102 2111 2120 2201 2210 2300
3002 3011 3020 3101 3110 3200 4001 4010 4100 5000