問題
カート内の価格をすべて足し合わせてください。合計が discount_threshold 以上なら、合計全体から discount_percent パーセントを割り引きます。
cart_total(prices, discount_threshold, discount_percent) を実装してください。
入力
prices: 各商品の価格です。 discount_threshold: 割引が適用される合計金額です。 discount_percent: 割引率(パーセント)です。
出力
支払う金額を返してください。
制約
0 ≤ 長さ ≤ 1,000; 0 ≤ 各価格 ≤ 100,000; 0 ≤ discount_percent ≤ 100
例
例 1
入力:
prices = [40, 35, 25]
discount_threshold = 100
discount_percent = 10
期待される出力:
expected = 90.0
入力:
std::vector<long long> prices = {40, 35, 25};
long long discount_threshold = 100;
long long discount_percent = 10;
期待される出力:
double expected = 90.0;
入力:
long[] prices = new long[] {40, 35, 25};
long discount_threshold = 100;
long discount_percent = 10;
期待される出力:
double expected = 90.0;
解説
合計は 100 でしきい値に達しているので、10% 引きになり 90 です。
例 2
入力:
prices = [20, 30]
discount_threshold = 100
discount_percent = 10
期待される出力:
expected = 50.0
入力:
std::vector<long long> prices = {20, 30};
long long discount_threshold = 100;
long long discount_percent = 10;
期待される出力:
double expected = 50.0;
入力:
long[] prices = new long[] {20, 30};
long discount_threshold = 100;
long discount_percent = 10;
期待される出力:
double expected = 50.0;
解説
合計 50 は 100 未満なので割引はなく、50 です。
解き方のヒント
まずは自分で考えてから、ヒントを1つずつ開いてみましょう。
ヒント 1
まず、割引前のカート合計を求めます。
ヒント 2
合計がしきい値に達したときだけ、割引率を適用します。
ヒント 3
割引は各商品の合計ではなく、注文全体から引くことに注意してください。
目安の計算量
時間 O(n)、追加メモリ O(1)
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。