4
0

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.

AtCoder185 A問B問をKotlinで解いた

Posted at

普段はPHPしか触らないのですが、Kotlinを書いてみたくなったので、
手始めにAtCoderをやってみました。
とりあえずACされますが、もっといい書き方があるかもしれません。

A

入力の最低値を出す問題ですね。

入力例
5 3 7 11

出力例
3

/*A - ABC Preparation  /
実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 :100点

問題文
高橋君は、プログラミングコンテストを何回か開くことにしました。
コンテストを
1回開くには、100点問題、200点問題、300点問題、400点問題が
1問ずつ必要です。
100,200,300,400点問題の案がそれぞれ

A1,A2,A3,A4
 個あるとき、コンテストを最大で何回開けるでしょうか?
なお、同じ問題案は1 度しか使えません。

制約
1 ≤ Ai ≤ 100
(1 ≤ i ≤ 4)
入力は全て整数
*/

fun main(args: Array<String>) {
  val array = readLine()!!.split(" ").map(String::toInt)
  println(array.min())
}

B

  • 初回入力でバッテリー残量、カフェに行く回数、最終的に家に帰る時間の、3つを受け取る
  • カフェにいる間は時間1あたり1充電される
  • 充電はバッテリ容量以上にされない
  • 次回以降の入力でカフェ入店時間と退転時間の2つが渡される
  • 一度でも充電が0になればNo,帰るまでに残っていればYesを出力する
  • 0.5という謎の値が渡されるが無視でいい...普通に混乱しました
fun main() {
  var departTime = 0
  var list: List<Int>

  val (battery, countGoingCafe, timeToBack) = readLine()!!.split(" ").map(String::toInt)
  var remain: Int = battery

  for (i in 1..countGoingCafe) {
    list = readLine()!!.split(" ").map(String::toInt)
    val consumeDuration = list[0] - departTime
    val stayCharge = list[1] - list[0]

    remain -= consumeDuration //カフェに着くまでの消費

    if (remain <= 0) {
      return println("No") //途中で0になる場合
    }

    remain += stayCharge

    if (remain > battery) {
      remain = battery //容量以上に残量が増えないように処理
    }

    departTime = list[1] //カフェを出る時間の記憶
  }

  remain -= (timeToBack - departTime) //最後のカフェを出てから家までの消費

  if (remain <= 0) {
    return println("No")
  }
  return println("Yes")
}
4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?