Cola-簡單-無敵,一行代碼解決

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

其實,這個問題。最上面的可樂,如果剛喝兩瓶就拿來換是最聰明的選擇。兩瓶換三瓶。

意思就是,喝一瓶,count++,空瓶++,空瓶到兩瓶,就馬上去換。

#include<iostream>
using namespace std;
int count = 0;
int main()
{
	int n;
	while(cin>>n){
		int m = 0;
		count = 0;
		while(n > 0 || m >= 2){
			if(n){
//    可樂少了一瓶
				n--;
//喝了一瓶
				count++;
//空瓶多了一瓶
				m++;
			}	
			if(m >= 2){
//空瓶兩瓶,借一瓶換一瓶,還了,相當於0
				m=0;
//又喝了剛纔換得一瓶
				count++;
			}
				
		}
		cout<<count<<endl;
	}
	return 0;
}

然後今早醒來,突然想到,不就是兩個空瓶換一個???有那麼麻煩麼!!!!

#include<iostream>
using namespace std;
int main()
{
	int n;
	while(scanf("%d",&n)!= EOF){
		cout<<n+n/2<<endl;
	}
	return 0;
}

其實還可以這樣

#include<iostream>
using namespace std;
int main()
{
	int n;
	while(scanf("%d",&n)!= EOF){
		cout<<(n/2)*3+(n%2)<<endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章