7-3 逆序的三位数 (10 分)
程序每次读入一个正3位数,然后输出按位逆序的数字。注意:当输入的数字含有结尾的0时,输出不应带有前导的0。比如输入700,输出应该是7。
输入格式:
每个测试是一个3位的正整数。
输出格式:
输出按位逆序的数。
输入样例:
123
输出样例:
321
┗|`O′|┛ 嗷~~还是过不了什么鬼啦!!
这次学到了:
[PTA报错]warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result时,可以查看这个回答:
Can someone help me understand this warning?(解决方案)
还有就是,可以增加代码的强壮性的步骤:
if (scanf("%d",&n) == 1) {
printf("%d", sum);
} else {
printf("Failed to read integer.\n");
}
答案是这个:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int x=0;
for(int i=0;i<3;i++)
{
x=x*10+n%10;
n/=10;
}
printf("%d",x);
return 0;
}
还有我这个感觉思路没问题但总是报错的:T-T
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a=0;
int b=0;
int c=0;
int sum=0;
a = n%100;//求模得百位
b = (n-a*100)%10;//求模得十位
c = (n-a*100-b*10)%1;
sum=a+b*10+c*100;
if (scanf("%d",&n) == 1) {
printf("%d", sum);
} else {
printf("Failed to read integer.\n");
}
return 0;
}