Title description While
doing homework, the friend next to him asked you: "How much is five times seven?" You should smile politely and tell him: "Fifty-three."
This question requires you to output their product backwards for any given pair of positive integers.
Input format
Input two positive integers A and B not exceeding 1000 in the first line, separated by a space.
Output format
Output the product of A and B backwards in one line.
Input example
5 7
Sample output
53
Problem solution one:
解题思路
: As long as the last digit is not 0, the preceding 0 can be output;
- General situation:
35
——>53
; - With leading 0:
1200
——>21
; - There is 0 in the middle:
1002
——>2001
;
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b;
c = a * b;
bool flag = false;
while(c)
{
if(!(c % 10) && flag) cout << 0;
else if(c % 10) cout << c % 10, flag = true;
c /= 10;
}
return 0;
}
Problem solution two
Library function:
stoi
: Turn a string into a number;reverse
: Flip the string horizontally;to_string
: Turn the number into a string;
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
string c = to_string(a * b);
reverse(c.begin(), c.end());
cout << stoi(c) << endl;
return 0;
}