1. 程式人生 > >L1-037 A除以B (10 分)

L1-037 A除以B (10 分)

ace txt 數字 輸出格式 pro lse pac mes long

題目鏈接:https://pintia.cn/problem-sets/994805046380707840/problems/994805094485180416

真的是簡單題哈 —— 給定兩個絕對值不超過100的整數A和B,要求你按照“A/B=商”的格式輸出結果。

輸入格式:

輸入在第一行給出兩個整數A和B(100A,B100),數字間以空格分隔。

輸出格式:

在一行中輸出結果:如果分母是正數,則輸出“A/B=商”;如果分母是負數,則要用括號把分母括起來輸出;如果分母為零,則輸出的商應為Error。輸出的商應保留小數點後2位。

輸入樣例1:

-1 2

輸出樣例1:

-1/2=-0.50

輸入樣例2:

1 -3

輸出樣例2:

1/(-3)=-0.33

輸入樣例3:

5 0

輸出樣例3:

5/0=Error

解題思路:

如果b小於0就左右加上括號,如果b=0就要輸出計算結果為Error,其余情況就輸出a/b的保留兩位小數的結果

AC代碼:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long


int main() {
#ifdef ONLINE_JUDGE
#else
	freopen("1.txt", "r", stdin);
#endif


	/* your code */
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int a, b;
	cin >> a >> b;
	cout << a << "/";
	if (b >= 0) {
		cout << b << "=";
	} else {
		cout << "(" << b << ")=";
	}
	if (b == 0) {
		cout << "Error";
	} else {
		printf("%.2f", a * 1.0 / b);
	}
	return 0;
}

  

L1-037 A除以B (10 分)