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

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

Last updated at Posted at 2020-07-10

トピック #1 構造化プログラムⅠ

AIZU ONLINE JUDGEの問題にSwiftで解答したものになります。競技プログラミングは普段やりませんので、こうしたほうが簡単!などがあればコメントで教えて下さい。

5_A 長方形の描画

問題
入力される二つの数字の組み合わせに対して以下のような長方形を出力して下さい。
####
####
####
(3,4)
入力は複数のデータセットから構成されています。
H W
H, W がともに 0 のとき、入力の終わりとします。

解答

let a = readLine()!.split(separator: " ").map({Int($0)!})
while a[0] != 0 && a[1] != 0 {
    for _ in 0 ..< a[0] {
        print(String(repeating: "#", count: a[1]))
    }
}
let a = readLine()!.split(separator: " ").map({Int($0)!})
while a[0] != 0 && a[1] != 0 {
    for _ in 0 ..< a[0] {
        for _ in 0 ..< a[1] {
            print("#")
        }
    }
}

が模範解答に近い形なのですが、出力に改行が含まれてしまうためString(repeating: , count: )を用いました。

5_B フレームの描画

問題
5_Aの続き
(3,4)に対して
####
#。。#
####
となるようにして下さい。
解答

let a = readLine()!.split(separator: " ").map({Int($0)!})
while a[0] != 0 && a[1] != 0 {
    for i in 0 ..< a[0] {
        if i == 0  || i == a[0] - 1{
            print(String(repeating: "#", count: a[1]))
        } else {
            var b = String(repeating: ".", count: a[1] - 2)
            print("#\(b)#")
        }
    }
}

5_C チェスボードの描画

問題
5_Bの続き
(3,4)に対して
#。#。
。#。#
#。#。
となるように出力してください。
解答

let a = readLine()!.split(separator: " ").map({Int($0)!})
while a[0] != 0 && a[1] != 0 {
    for i in 0 ..< a[0] {
        if i.isMultiple(of: 2) {
            if a[1].isMultiple(of: 2) {
            print(String(repeating: "#.", count: a[1]/2))
            } else {
                var b = String(repeating: "#.", count: a[1]/2 - 1)
                print("\(b).")
            }
        } else {
            if a[1].isMultiple(of: 2) {
            print(String(repeating: ".#", count: a[1]/2))
            } else {
                var b = String(repeating: ".#", count: a[1]/2 - 1)
                print("\(b)#")
            }
        }
    }
}

5_D 構造化プログラミング

問題
入力された数字nに対しnまでに含まれる3の倍数または3を含む数字を出力して下さい。(世界のナベアツ)
解答

let a = Int(readLine()!)!
var arr:[String] = []

for i in 1 ... a {
    arr.append(String(i))
}

func check(_ num: [String]) -> [Int] {
    var resultArr:[String] = []
    for i in num {
        if Int(i)!.isMultiple(of: 3){
            resultArr.append(i)
        }
    }
    print(resultArr)
    var arr = num.filter({Int($0)! % 3 != 0}).map({String($0)})
    print(arr)
    for i in arr {
        if i.contains("3") {
            resultArr.append(i)
        }
    }
    return resultArr.map({Int($0)!})

}
print(check(arr).sorted())
0
1
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
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?