首页 > 专题系列 > Java解POJ > POJ 2876 Cantoring Along [解题报告] Java
2013
11-12

POJ 2876 Cantoring Along [解题报告] Java

Cantoring Along

问题描述 :

The Cantor set was discovered by Georg Cantor. It is one of the simpler fractals. It is the result of an infinite process, so for this program, printing an approximation of the whole set is enough. The following steps describe one way of obtaining the desired output for a given order Cantor set:
  1. Start with a string of dashes, with length 3order
  2. Replace the middle third of the line of dashes with spaces. You are left with two lines of dashes at each end of the original string.
  3. Replace the middle third of each line of dashes with spaces. Repeat until the lines consist of a single dash.

For example, if the order of approximation is 3, start with a string of 27 dashes:

---------------------------

Remove the middle third of the string:

---------         ---------

and remove the middle third of each piece:

---   ---         ---   ---

and again:

- -   - -         - -   - -

The process stops here, when the groups of dashes are all of length 1. You should not print the intermediate steps in your program. Only the final result, given by the last line above, should be displayed.

输入:

Each line of input will be a single number between 0 and 12, inclusive, indicating the order of the approximation. The input stops when end-of-file is reached.

输出:

You must output the approximation of the Cantor set, followed by a newline. There is no whitespace before or after your Cantor set approximation. The only characters that should appear on your line are ‘-’ and ‘ ‘. Each set is followed by a newline, but there should be no extra newlines in your output.

样例输入:

0
1
3
2

样例输出:

-
- -
- -   - -         - -   - -
- -   - -

解题代码:

//* @author: [email protected]
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  StringBuffer[] sb=new StringBuffer[13];
  sb[0]=new StringBuffer("-");
  for(int i=1;i< 13;i++)
  {
	sb[i]=new StringBuffer(sb[i-1]);
	int k=(int)Math.pow(3, i-1);
	for(int w=0;w< k;w++)
		sb[i].append(" ");
	sb[i].append(sb[i-1]);
   }
  while(in.hasNext())
  {
	int a=in.nextInt();
	System.out.println(sb[a]);
  }
 }
}

  1. 你的理解应该是:即使主持人拿走一个箱子对结果没有影响。这样想,主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率,但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

  2. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。