# Conditional Statements# Arithmetic Operations# Input/Output

d140 - On Sale

🔗 前往 ZeroJudge 原題

題目描述

題目要求根據商品價格的不同範圍,計算折扣後的價格。如果原始價格小於 100 元,則需要額外加收 8 元的稅。最後,以指定的格式輸出折扣後的價格,並無條件捨去到小數第二位。

解題思路

程式首先讀取商品原始價格,並將其轉換為以美分為單位的整數。然後,根據價格範圍應用不同的折扣。如果價格小於 100,則加收 8 元稅。折扣計算分別為 10%、20% 和 40%。計算出折扣後的價格後,程式將其轉換為美元格式,並輸出到小數點後兩位。

複雜度分析

  • 時間複雜度: O(n),其中 n 是輸入的商品數量。程式需要對每個商品執行常數時間的操作。
  • 空間複雜度: O(1)。程式只使用固定數量的變數,不隨輸入大小而變化。

程式碼

#include <iostream>
using namespace std;
int main(){
	cin.tie(0); ios::sync_with_stdio(false);
	long long int a,b;
	char dot;
	while(cin >> a >> dot >> b){
		a=a*100+b;
		if(a>=1&&a<=10000)
			b=a*0.9+800;
		else if(a>=10001&&a<=50000)
			b=a*0.8;
		else
			b=a*0.6;
		cout << "$" << b/100 << "." << (b%100)/10 << b%10 << "\n"; 
	}
}

Discussion