PAT B1086 就不告訴你 (15分)

題目鏈接https://pintia.cn/problem-sets/994805260223102976/problems/1038429065476579328

題目描述
做作業的時候,鄰座的小盆友問你:“五乘以七等於多少?”你應該不失禮貌地圍笑着告訴他:“五十三。”本題就要求你,對任何一對給定的正整數,倒着輸出它們的乘積。
在這裏插入圖片描述

輸入
輸入在第一行給出兩個不超過 1000 的正整數 A 和 B,其間以空格分隔。

輸出
在一行中倒着輸出 A 和 B 的乘積。

樣例輸入
5 7

樣例輸出
53

代碼

方法1:

#include <iostream>
using namespace std;

int main() {
	int a, b, c;
	int t = 0;
	cin >> a >> b;
	c = a * b;
	do{	
		if(c % 10 == 0 && t == 0) {		//開頭的0不輸出!
			c /= 10;
            continue;
		}
		cout << c % 10;
		c = c / 10;
		t++;
	}while(c > 0) ;
	return 0;
}

方法2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
  unsigned num1,num2;
  cin>>num1>>num2;
  string result=to_string(num1*num2);		//轉化成字符串
  reverse(result.begin(),result.end());
  cout<<stoi(result);						//轉化成數值輸出
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章