做作业的时候,邻座的小盆友问你:“五乘以七等于多少?”你应该不失礼貌地围笑着告诉他:“五十三。”本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积。
输入格式:
输入在第一行给出两个不超过 1000 的正整数 A 和 B,其间以空格分隔。
输出格式:
在一行中倒着输出 A 和 B 的乘积。
输入样例:
5 7
输出样例:
53
思路:
注意个位为 0 的情况。
CODE
#include <iostream>
using namespace std;
#include <string>
int main()
{
int a, b;
cin >> a >> b;
string c = to_string(a * b);
bool have = false;
for (int i = c.length()-1; i>=0; i--)
{
if (c[i] == '0')
{
if (have)
{
cout << c[i];
}
continue;
}
else
{
have = true;
cout << c[i];
}
}
}
简化版:
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
c=a*b;
string str=to_string(c);//数字转字符串
reverse(str.begin(),str.end());//字符串逆序
int u=stoi(str);//字符串转数字
cout<<u<<endl;
}