百俊年齢配列swift

1783 ワード

マイコード:
var N = Int(readLine()!)!
var newList : [[Any]] = Array(repeating: [], count: N)
//print(newList)

for index in 0..<N {
    let info = readLine()!.components(separatedBy: " ")
    let age = Int(info[0])!
    let name = info[1]

    newList[index].append(index)
    newList[index].append(age)
    newList[index].append(name)
}

var resultList = newList.sorted { anyList1, anyList2 in
    if let listAge = anyList1[1] as? Int ,
       let list2Age = anyList2[1] as? Int,
       let listIndex = anyList1[0] as? Int,
       let list2Index = anyList2[0] as? Int
    {
        if listAge == list2Age {
            return listIndex < list2Index
        }
        return listAge < list2Age
    }
    return false
}
//print(newList)
for index in 0..<resultList.count {
    print("\(resultList[index][1]) \(resultList[index][2])")
}
  • Anyタイプを練習してみます.
  • 分類エンクロージャを使用してみます.
  • 次は異なる分コードで、splitを使用すると、次のappendを実行する際にstring受信ではなくsubstring受信を使用します.したがって、mapを使用してstringに変換する必要があります.

    ただし、下図のようにcomponentsはstringに変換されて使えます.

    下記のように入力を受け入れる練習をしました.いろいろなケースを用意するために

    下と比較できますが、あまりよく見えません.

    次は他の人の簡潔なコードです.
  • 10000という大きな数字には触れず、年齢200という小さな数字を利用した.考えの転換...
  • その他の分コードソース:https://www.acmicpc.net/source/27314300