0
0

終了判定 2 Python3編

Last updated at Posted at 2024-01-10

さて久しぶりにPythonから。
ここでwhileを使いました。たぶんpythonでPaizaやり始めてから初めてじゃないかな。

N,K = map(int,input().split())
cnt = 0
while N < K:
    cnt += 1
    N = N * 2
    
print(cnt)

ところでなんで、+=は知ってるのに、*=はなぜしなかったの?と自分で自分を突っ込んでしまった。いやおっしゃるとおり。
こうですね。

N,K = map(int,input().split())
cnt = 0
while N < K:
    cnt += 1
    N *= 2
    
print(cnt)

とりあえずphpでもやっておきます。

<?php
    $A = explode(" ",fgets(STDIN));
    $N = $A[0];
    $K = $A[1];
    $cnt = 0;
    while($N<$K){
        $N *= 2;
        $cnt += 1;
    }
    print($cnt);
?>

解答はないので、自分でコードを評価してみます。
pythonみたいに、複数の変数に入れられないのかなと思ってたら、
list()でできるようです。
さらに配列$Aにいれないまま直接リストにつっこんでもよさそうです。
やってみましょう。

<?php
    list($N,$K) = explode(" ",fgets(STDIN));
    $cnt = 0;
    while($N<$K){
        $N *= 2;
        $cnt += 1;
    }
    print($cnt);
?>

こうですね。
2行減りました。
思った通りうまくいったようです。

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