题目
把整数各位逆序输出
思路
while (x) { printf("%d", x%10); x/=10; } 或先存余数数组再输出;注意原数为 0 时要输出一位 0。
解题分析
取个位 n%10 输出,n/=10。若要求不换行空格分隔,注意最后一个后面不要多余空格。通用写法对任意正整数都适用,不限三位。
完整程序
#include <stdio.h>
int main(void)
{
int n;
if (scanf("%d", &n) != 1) {
return 1;
}
if (n == 0) {
printf("0\n");
return 0;
}
while (n) {
printf("%d", n % 10);
n /= 10;
if (n) {
putchar(' ');
}
}
putchar('\n');
return 0;
}
运行示例
输入:
1234输出:
4 3 2 1