UVA11150 Cola【数学】

132 篇文章 1 订阅
96 篇文章 5 订阅

You see the following special offer by the convenience store:

“A bottle of Choco Cola for every 3 empty bottles returned”

  Now you decide to buy some (say N) bottles of cola from the store. You would like to know how you can get the most cola from them.

  The figure below shows the case where N = 8. Method 1 is the standard way: after finishing your 8 bottles of cola, you have 8 empty bottles. Take 6 of them and you get 2 new bottles of cola. Now after drinking them you have 4 empty bottles, so you take 3 of them to get yet another new cola. Finally, you have only 2 bottles in hand, so you cannot get new cola any more. Hence, you have enjoyed 8 + 2+ 1 = 11 bottles of cola.

  You can actually do better! In Method 2, you first borrow an empty bottle from your friend (?! Or the storekeeper??), then you can enjoy 8 + 3 + 1 = 12 bottles of cola! Of course, you will have to return your remaining empty bottle back to your friend.


Input

Input consists of several lines, each containing an integer N (1 ≤ N ≤ 200).

Output

For each case, your program should output the maximum number of bottles of cola you can enjoy. You may borrow empty bottles from others, but if you do that, make sure that you have enough bottles afterwards to return to them.

Note: Drinking too much cola is bad for your health, so... don’t try this at home!! :-)

Sample Input

8

Sample Output

12


问题链接UVA11150 Cola

问题简述:(略)

问题分析

  喝可乐,每3个空瓶可以换一瓶可乐,迭代计算问题。

  如果最后剩下2瓶则可以换一瓶。喝掉这2瓶再从朋友那里借一个空瓶,3个空瓶换一瓶,喝了之后,将空瓶还给朋友。

  既然2瓶可以喝3瓶,那直接用数学公式算就可以了,时间复杂度为O(1)。

程序说明:(略)

题记:(略)


参考链接:(略)


AC的C++语言程序如下:

/* UVA11150 Cola */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(~scanf("%d", &n))
        printf("%d\n", (n / 2 )* 3 + n % 2);

    return 0;
}

AC的C++语言程序(模拟法)如下:

/* UVA11150 Cola */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, ans;
    while(~scanf("%d", &n)) {
        ans = n;
        while(n >= 3) {
            ans += n / 3;
            n = n / 3 + n % 3;
        }
        if(n == 2)
            ans++;

        printf("%d\n", ans);
    }

    return 0;
}


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值