ZeroJudge C079: Peter's Smokes

題目敘述

本題採EOF方式收資料,每筆資料有兩個正整數N和K。Peter手上有N條煙,吸完一支煙後會剩下一個煙尾巴,K個煙尾巴又可以在換成一條煙。要求輸出Peter總共可以吸幾條煙。


範例輸入 #1

4 3

10 3

100 5

範例輸出 #1

5

14

124


解題思路

使用While迴圈,每次吸一隻煙後就煙尾巴+1,每次判斷現在的煙尾巴可不可以在換成一條煙,如果沒有煙了 and 煙尾巴<K,那就中止迴圈輸出答案。

解題程式碼如下 (僅供參考):

#include <iostream>
using namespace std;

int main() {
  int N, K;
  while (cin >> N >> K)
    {
      int ans = 0, count = 0;
      while (true)
        {
          if (N > 0)
          {
            ans++;
            count++;
            N--;
          }
          else if (count >= K)
          {
            ans++;
            count-=K;
            count++;
          }
          else break;
        }
      cout << ans << endl;
    }
}

留言

這個網誌中的熱門文章

ZeroJudge M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge A263: 日期差幾天