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

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

Last updated at Posted at 2020-02-11

##はじめに
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
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
0
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?