Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@c3drive

ABC081B - Shift onlyを2で割れる回数の最小値から導く方法(Golang)

More than 1 year has passed since last update.

はじめに

ABC081B - Shift onlyを実際の操作のシミュレーションでのカウントではなく、
2で割れる回数の最小値から導く方法です。

package main

import (
  "fmt"
)

func main() {
  var n int
  min := 1000000000 // Aiの取りうる値の最大値
  fmt.Scanf("%d", &n)
  nums := make([]int, n)

  // n個の値を取得
  for i := 0; i < n; i++ {
    fmt.Scan(&nums[i])
  }

  for i :=0; i < n; i++ {
    lcnt := 0;
    // 2で割り切れる限り回し続ける
    for nums[i]%2 == 0 {
      nums[i] = nums[i]/2
      lcnt++ // 割った数をカウント
    }

    // 最小値より小さいカウントの場合入れ替え
    if min > lcnt {
      min = lcnt
    }
  }
  fmt.Println(min)
}

おわりに

説明文の操作の通り上から順序よく処理したくなりますが、選択肢として持っておきたいため書きました。

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
c3drive

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?