0
1

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 3 years have passed since last update.

AIZU ONLINE JUDGE の問題をSwiftでやってみた(プログラミング入門 構造化プログラムⅡ編)

Last updated at Posted at 2020-07-11

トピック #7 構造化プログラミングⅡ

AIZU ONLINE JUDGEの問題にSwiftで解答したものになります。競技プログラミングは普段やりませんので、こうしたほうが簡単!などがあればコメントで教えて下さい。
ブラウザでプログラミング・実行ができる「オンライン実行環境」paiza.IOでテスト出力が行えます。

7_A 成績

問題
あなたの課題は、あるクラスの数学の成績を付けるプログラムを作成することです。プログラムは複数の学生のテストの点数を読み込みます。テストの点数は、中間試験の点数 m、期末試験の点数 f、再試験の点数 r で構成されています。
中間試験と期末試験は 50 点満点(m, f ≤ 50)、再試験は 100 点満点 (r ≤ 100)です。試験を受けていない場合は点数を -1 とします以下の手順で成績が付けられます:中間試験、期末試験のいずれかを欠席した場合成績は F。
中間試験と期末試験の合計点数が 80 以上ならば成績は A 。
中間試験と期末試験の合計点数が 65 以上 80 未満ならば成績は B。
中間試験と期末試験の合計点数が 50 以上 65 未満ならば成績は C。
中間試験と期末試験の合計点数が 30 以上 50 未満ならば成績は D。 ただし、再試験の点数が 50 以上ならば成績は C。
中間試験と期末試験の合計点数が 30 未満ならば成績は F。
解答

while(true) {
    let a = readLine()!.split(separator: " ").map({Int($0)!})
    if a[0] == -1 && a[1] == -1 && a[2] == -1 {
        break
    }
    if a[0] == -1 || a[1] == -1 {
        print("F")
    } else {
    var sum = a[0] + a[1]

    switch sum {
    case 80 ... 100:
        print("A")
    case 65 ..< 80:
        print("B")
    case 50 ..< 65:
        print("C")
    case 30 ..< 50:
        if a[2] >= 50 {
            print("C")
        } else {
            print("D")
        }
    default:
        print("F")
    }
    }
}

7_B 組み合わせの数

問題
1 から n までの数の中から、重複無しで3つの数を選びそれらの合計が x となる組み合わせの数を求めるプログラムを作成して下さい。

例えば、1 から 5 までの数から3つを選んでそれらの合計が 9 となる組み合わせは、

1 + 3 + 5 = 9
2 + 3 + 4 = 9
の2通りがあります。
解答

let a = readLine()!.split(separator: " ").map({Int($0)!})
for i in 1 ... a[0] {
    for j in i ... a[0]{
        for k in i ... a[0]{
            if i + j + k == a[1] && (i != j) && (j != k) && (i != k) && (i < j) && (j < k) {
                print(i,j,k)
            }
        }
    }
}

7_C 表計算

問題
表計算を行う簡単なプログラムを作成します。

表の行数rと列数c、r × c の要素を持つ表を読み込んで、各行と列の合計を挿入した新しい表を出力するプログラムを作成して下さい。
入力
4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6

出力
1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56
解答

let a = readLine()!.split(separator: " ").map({Int($0)!})
var arr = [[Int]]()
var q = [Int](repeating: 0, count: a[1] + 1)
for i in 0 ..< a[0] {
    arr.append(readLine()!.split(separator: " ").map({Int($0)!}))
}

var b = { (arr: [Int]) -> Int in
    var sum = 0
    for i in 0 ..< arr.count {
        sum += arr[i]
    }
    return sum
}

for i in 0 ..< a[0] {
    arr[i].append(b(arr[i]))
}



for i in 0 ..< arr.count {
    for j in 0 ..< arr[i].count {
        q[j] += arr[i][j]
    }
}
arr.append(q)

for i in arr {
    for j in i {
        print(j, terminator: " ")
    }
    print("\n")
}

7_D 行列の積

問題

$n\times m$の行列$A$と$m\times l$の行列$B$を入力し、それらの積である$n\times l$の行列$C$を出力するプログラムを作成して下さい。

解答

let a = readLine()!.split(separator: " ").map({Int($0)!})
var matrix1 = [[Int]](repeatElement([Int](repeatElement(0, count: a[1])), count: a[0]))
var matrix2 = [[Int]](repeatElement([Int](repeatElement(0, count: a[2])), count: a[1]))

for i in 0 ..< a[0] {
    let b = readLine()!.split(separator: " ").map({Int($0)!})
    for j in 0 ..< a[1] {
        matrix1[i][j] = b[j]
    }
}

for i in 0 ..< a[1] {
    let c = readLine()!.split(separator: " ").map({Int($0)!})
    for j in 0 ..< a[2] {
        matrix2[i][j] = c[j]
    }
}

func matrixtime(_ matrix1: [[Int]], _ matrix2: [[Int]]) -> [[Int]] {
    var solutionMatrix = [[Int]](repeating: [Int](repeating: 0, count: matrix2[0].count), count: matrix1.count)
    for i in 0 ..< matrix1.count {
        for j in 0 ..< matrix2[0].count {
            for k in 0 ..< matrix1[0].count {
            solutionMatrix[i][j] += matrix1[i][k] * matrix2[k][j]
            }
        }
    }
    return solutionMatrix
}

print(matrixtime(matrix1, matrix2))
0
1
2

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?