反向输出一个四位数
反向输出一个四位数
http://www.nowcoder.com/questionTerminal/1f7c1d67446e4361bf4af67c08e0b8b0
分析:
将一个四位数,反向输出。本题可以利用求余运算发然后每次对10求商即可,另一种思路可以将数字以字符串的形式读入然后逆向输出即可。
题解1:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 0;
scanf("%d", &a);
//利用求余获取a的最后一位,以此类推
printf("%d", a % 10);
a /= 10;
printf("%d", a % 10);
a /= 10;
printf("%d", a % 10);
a /= 10;
printf("%d\n", a);
return 0;
}题解2:
#include <bits/stdc++.h>
using namespace std;
int main() {
char str[10];
scanf("%s", str);
//将数字以字符串读入,只要输出字符串后四位即可
printf("%c%c%c%c\n", str[3], str[2], str[1], str[0]);
return 0;
}总结:
练习求余和求商运算符的灵活使用。