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=n/3+n%3),每次都要统计所喝的瓶子的总数量。

    如果还剩两个空瓶子时可以借一个空瓶子再喝一瓶可乐。当n=0时不执行,程序结束。

    程序代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 
     5 int n[100];
     6 
     7 int main()
     8 {
     9     int n,a;
    10     while(scanf("%d",&n))      //输入空瓶子数量
    11     {
    12         if(n==0)               //当n=0时程序结束
    13             break;
    14             a=0;
    15         while(n>2)             //n>2时做此循环,统计所喝饮料的总数a
    16         {
    17             a+=n/3;            
    18             n=n/3+n%3;
    19         }
    20         if(n==2)
    21             a++;        
    22         cout<<a<<endl;
    23     }
    24     return 0;
    25 } 

    心得:
    这道题主要考数学思考能力,把要做的公式想到,题目就变得简单了,而且题中还给了提示。虽然一道很简单的题目,我却做了一个多小时,有一处错误总也改不对,后来静下心来终于改对了,是一处很小的错误,这提醒我以后写程序时要注意细节,不要大体过了就算了。

  • 相关阅读:
    判断一本书是否值得买
    【Python】对我自己的博客进行统计,看看哪年哪月发帖量最大
    在python中使用正则表达式(转载)
    【English】What is a Java StringWriter, and when should I use it?(转帖)
    【Java】利用java.io.PrintWriter写出文本文件
    [MySql]当虚拟机的IP地址自动更换后,JDBC使用原来的配置连不上MySql数据库时所报的异常。
    java中的lastIndexOf( )函数是什么意思
    day63_SpringMVC学习笔记_01
    day62_Mybatis学习笔记_02
    day61_Mybatis学习笔记_01
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4654706.html
Copyright © 2011-2022 走看看