做作业的时候,邻座的小盆友问你:“五乘以七等于多少?”你应该不失礼貌地围笑着告诉他:“五十三。”本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积。
输入格式:
输入在第一行给出两个不超过 1000 的正整数 A 和 B,其间以空格分隔。
输出格式:
在一行中倒着输出 A 和 B 的乘积。
输入样例:
5 7
输出样例:
53
-
思路1:
将结果从后往前按位输出 -
错误:
Wrong1:倒序输出时要去掉前导0!
错误样例:101 100
-
code1:
#include <bits/stdc++.h>
using namespace std;
int main(){
int a, b;
scanf("%d %d", &a, &b);
int c = a * b;
bool f = false;
while(c){
// int dig = c % 10; //Wrong1: 实际数值问题 别忘了去掉前导0
// if(dig) printf("%d", dig);
//Wrong2:上面两行 把所有0都去掉了...
int dig = c % 10;
if(f || dig) f = true; //短路原理,第一次遇到非0后面的不用再进入
if(f) printf("%d", dig);
c /= 10;
}
return 0;
}
-
思路2: 字符串
将乘积转化为字符串:to_string();
翻转字符串:reverse(it1, it2);
字符串转化为数值:stoi();
-
code2:
#include <bits/stdc++.h>
using namespace std;
int main(){
int a, b;
scanf("%d %d", &a, &b);
string ans = to_string(a * b);
reverse(ans.begin(), ans.end());
int c = stoi(ans);
printf("%d", c);
return 0;
}