zoukankan      html  css  js  c++  java
  • 空瓶子换可乐

     题目:

          Once upon a time, there is a special coco-cola store. If you return three empty bottles to the shop,
    you’ll get a full bottle of coco-cola to drink. If you have n empty bottles right in your hand, how many
    full bottles of coco-cola can you drink?


    Input
    There will be at most 10 test cases, each containing a single line with an integer n (1 ≤ n ≤ 100). The
    input terminates with n = 0, which should not be processed.


    Output
    For each test case, print the number of full bottles of coco-cola that you can drink.
    Spoiler
    Let me tell you how to drink 5 full bottles with 10 empty bottles: get 3 full bottles with 9 empty
    bottles, drink them to get 3 empty bottles, and again get a full bottle from them. Now you have 2
    empty bottles. Borrow another empty bottle from the shop, then get another full bottle. Drink it, and
    finally return this empty bottle to the shop!


    Sample Input
    3
    10
    81
    0
    Sample Output
    1
    5
    40

    思路分析:

         使用的方法是取除数跟取余数。每次循环用当前的总空瓶子n来除以3就可以得到当前能换取的可乐瓶数,然后记录此时的余数b,那么当前的总空瓶数就是n+b。特殊的情况要考虑,就是还剩两个空瓶子时可以先借一个空瓶子,这样就多得到一个可乐。

    源代码:

     1 #include<stdio.h>
     2 int main()
     3 {
     4     int n;
     5     while (scanf("%d", &n) == 1 && n)
     6     {
     7         int a = 0, b = 0;
     8         int count = 0;
     9         if (n == 1 || n == 2)          //两种特殊情况
    10         {
    11             count = n - 1;                  
    12         }
    13         while ((n + b) >= 3)
    14         {
    15             a = n + b;
    16             n = (n + b) / 3;          //n+b为目前的所有空瓶子数
    17             b = a % 3;
    18             count += n;              //目前换取到的可乐数
    19 
    20             if (n + b == 2)          //如果最后还剩下两个空瓶子就可以先借一
    21                                                            //个空瓶子
    22                 count += 1;          //从而又可以多得一瓶可乐
    23         }
    24 
    25 
    26         printf("%d
    ", count);
    27     }
    28 
    29 
    30     return 0;
    31 }

    心得:

             这次比赛心态很不好,就这道题本来是简单的题,却很久才做出来,当时心里已经很浮躁了,后面的题更是无心去看了,结果整场比赛就做出这一道题,╮(╯▽╰)╭简直不能用文字形容自己的心情,经历了才知道。虽然自己不是那么在乎排名,但是浮躁的心态真的是个很大的障碍。不放弃!干吧得!

      

    ------------------------ 没有谁的人生不是斩棘前行 ---------------------------------------- JM
  • 相关阅读:
    libusb 示例
    里不是吧、
    ibeacon UUID
    Centos7系统下Docker开启认证的远程端口2376配置教程
    Consul 快速入门
    docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
    Docker 启动容器时,报错 WARNING:IPv4 forwarding is disabled. Networking will not work. 的解决办法
    【基线检查】(高)基线检查--禁用local-infile选项(访问控制)
    PyCharm 上安装 Package(以 pandas 为例)
    Python time模块和datetime模块
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4652645.html
Copyright © 2011-2022 走看看