OJ Question Record: Question B: Blue Bridge Cup (JAVA Group) 2015-8 Drink redemption

Question B: Blue Bridge Cup (JAVA Group) 2015-8 Drink redemption

Subject request:
Leyangyang Beverage Factory is holding a promotional activity. For Leyangyang Type C Beverage, you can exchange for another bottle of Type C Beverage with 3 caps, and it can be recycled forever, but credit is not allowed.

Please calculate, if Xiao Ming does not waste bottle caps and participates in activities as much as possible, then, for the n bottles of drinks he initially bought, how many bottles of drinks he can get in the end.

Enter
an integer n, indicating the number of beverages to purchase (0<n<10000)

Output
an integer, representing the number of drinks actually obtained.
Sample input Copy
100
Sample output Copy
149

Problem-solving idea:
every three bottles can be replaced with a new bottle of beverage.
We divide the total number of beverages we own by 3 to get the number of beverages that can be exchanged this time, and there may be 0-2 bottles of beverages left that cannot be exchanged for new beverages. We can use the total number of beverages we have now Take the remainder of 3 to get the number of bottles remaining in this exchange. Then after one exchange,
the number of drinks we have == the drinks exchanged + exchanged for the remaining drinks .
We continue to exchange for the next time, until the number of drinks we have is less than 3 and cannot continue to exchange.
At this time, we get the total number of beverage bottles that we can exchange, plus the number of beverage bottles we had at the beginning, that is, the total number of beverage bottles we got.

Customs clearance code:

#include <iostream>

using namespace std;

int main() {
    
    
	int bought, n, res = 0;
	
	cin >> n;
	
	bought = n;
	
	while (n >= 3) {
    
    
		res = res + (n / 3);
		n = (n / 3) + (n % 3);	
	}
	
	cout << bought + res;
	
	return 0;
}

complete.

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108620437