刚学C语言,不太会,用多重循环,求大佬赐教
输出 1000 到 20000 之间的数据中有 3 个数字为 9 的所有整数,每行输出 8 个数
程序代码:#include<stdio.h>
int main(void) {
int i, n, cnt, index;
for(i = 1000, index = 0; i <= 20000; ++i) {
n = i;
cnt = 0;
while( n ) {
if(n % 10 == 9) {
++cnt;
}
n /= 10;
}
if(3 == cnt) {
printf("%d\t", i);
++index;
if(index%8 == 0) {
printf("\n");
}
}
}
return 0;
}





程序代码:#include <stdio.h>
int main(void)
{
int i, x, a[] = { 1000,100,10,1 };
for (i = 1; i != 72; ++i)
{
x = a[i % 36 / 9];
printf("%5d%c", i / 36 * 10000 + 999 % x + 999 / x * x * 10 + i % 9 * x, i % 8 ? ' ':'\n');
//" \n"[i%8==0]等同于字符串常量指针运算,二选一的话用三目运算更容易理解些
}
}[此贴子已经被作者于2020-4-4 07:32编辑过]
