[Daily Blue Bridge] 31. One-five-year provincial competition Java group real question "Beverage Redemption"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Drink Redemption

Leyangyang Beverage Factory normally holds a promotional activity. Leyangyang C-type beverage can be exchanged for another bottle of C-type beverage with 3 caps, and it can be recycled forever (but temporary loan or credit is not allowed)

Please calculate, if Xiao Ming does not waste bottle caps and participates in activities as much as he wants, then for the n bottles of beverages he initially bought, how many bottles of beverages he can drink in the end?

Input: a positive integer n, which represents the number of beverages originally purchased. (0<n<10000)

Output: an integer representing the number of drinks actually obtained

E.g:

User input:

100

The program should output:
149

 

User input:

101

The program should output:

151

 

Please output strictly according to the requirements, please do not print extra text similar to "please enter..."

All the source code is placed in the same file, after debugging, copy and submit the source code

Problem-solving ideas:

There are two ideas for reference when solving this problem:

The first is conventional thinking:

When we now have n bottles of beverages, that is, there are n bottle caps, we calculate how many bottles of beverages can be exchanged for the caps on hand, and how many caps are left at the same time, and add the number of exchanged beverages to the number of exchanged beverages. The number of remaining bottle caps is the number of bottle caps currently on hand, then it is back to the initial situation of the problem, just recursively, and the number of new drinks exchanged each time can be accumulated. When the number of bottle caps is less than 3, it can no longer be exchanged. Then add the last remaining caps to get the final answer.

The second idea:

Suppose we now have n bottles of beverages and record how many bottles of beverages we have drunk. We know that for every three bottles of beverages we can get one bottle of beverage, then it is equivalent to drinking three bottles of beverages, but the number of remaining undrinked beverages is two less. Use Circulation, when the remaining number of undrinked drinks is less than three, it cannot be redeemed. At this time, using the number of drinks already drunk plus the number of remaining drinks is the final answer.

The specific code analysis is as follows:

 

Answer source code:

The first solution:

public class Year2015_Bt8 {

	static int ans = 0;
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		ans = n;
		f(n);
		System.out.println(ans);
	}

	private static void f(int n) {
		if (n<3) {
			return ;
		}
		int sum = n/3;
		n = sum + n%3;
		ans += sum;
		f(n);
		
	}

}

The second solution:

import java.util.Scanner;

public class Year2015_Bt8_2 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int ans = 0;
		while (n>=3) {
			n-=2;
			ans+=3;
		}
		ans+=n;
		System.out.println(ans);
	}

}

 

Sample output:

 

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/114847512