给出一个数字n,小于100000,求将它逆序以后再输出来,怎么实现比较好?
#include <stdio.h>
int main() {
int n;
while(scanf("%d", &n) != EOF) {
while(n) { // (1)
printf("%d", n % 10); // (2)
n /= 10; // (3)
}
printf("\n"); // (4)
}
return 0;
}
while(n)
和while(n != 0)
是完全等价的,也就是我们在试除的过程中,如果这个数字变成了零,我们就不需要再继续循环下去了;
#include<stdlib.h>
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d",&n);
char str[6];
itoa(n,str,10);
for (int i = sizeof str-1; i >= 0; i--)
{
printf("%c",str[i]);
}
return 0;
}