首页 > 专题系列 > Java解POJ > POJ 3210 Coins [解题报告] Java
2013
11-12

POJ 3210 Coins [解题报告] Java

Coins

问题描述 :

Snoopy has three coins. One day he tossed them on a table then and tried to flip some of them so that they had either all heads or all tails facing up. After several attempts, he found that regardless of the initial configuration of the coins, he could always achieve the goal by doing exactly two flippings, under the condition that only one coin could be flipped each time and a coin could be flipped more than once. He also noticed that he could never succeed with less than two flippings.

Snoopy then wondered, if he had n coins, was there a minimum number x such that he could do exactly x flippings to satisfy his requirements?

输入:

The input contains multiple test cases. Each test case consists of a single positive integer n (n < 10,000) on a separate line. A zero indicates the end of input and should not be processed.

输出:

For each test case output a single line containing your answer without leading or trailing spaces. If the answer does not exist, output “No Solution!

样例输入:

2
3
0

样例输出:

No Solution!
2

解题代码:

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
 public static void main(String[] args){
  Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
	int n;
	while (true){
           n=scanner.nextInt();
	   if (n==0){
		break;
	   }
	   if (n%2==0){
		System.out.println("No Solution!");
	   }
	   else{
		System.out.println(n-1);
	  }
	}
  }
}

  1. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?