0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AtCoder Beginner Contest 158の[C - Tax Increas]

Last updated at Posted at 2021-10-26

SwiftでAtCoder Beginner Contest 158の[C - Tax Increas]を解きました。

問題内容

問題文
消費税率が 8 %のとき A 円、10 %のとき B 円の消費税が課されるような商品の税抜き価格を求めてください。

ただし、税抜き価格は正の整数でなければならないものとし、消費税の計算において小数点以下は切り捨てて計算するものとします。

条件を満たす税抜き価格が複数存在する場合は最も小さい金額を出力してください。また、条件を満たす税抜き価格が存在しない場合は -1 と出力してください。

解答コード

    import Foundation

    let aa = readLine()!.split(separator: " ").map{ Double($0)! }
    var array : [Int] = []
    
    let minA = Int(ceil(aa[0] / 0.08))
    let maxA = Int(floor((aa[0] + 1) / 0.08))
    
    let minB = Int(ceil(aa[1] / 0.1))
    let maxB = Int(floor((aa[1] + 1) / 0.1))
    
    for i in minA...maxA - 1{
        array.append(i)
    }
    for i in minB...maxB - 1{
        array.append(i)
    }
    var set = Set<Int>()
    let repeated = NSOrderedSet(array: array.filter { !set.insert($0).inserted }).array
    
    print(repeated.first ?? -1)

配列の重複部分だけ取り出す際コードは以下のQAを参考にしました。

Swiftのお役立ち情報

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?