首页 > 专题系列 > Java解POJ > POJ 1353 Color Change of Go Game Pieces [解题报告] Java
2013
11-09

POJ 1353 Color Change of Go Game Pieces [解题报告] Java

Color Change of Go Game Pieces

问题描述 :

Assume that there are lots of two colors of black and white Go Game Pieces in a box, we take out n Go Game Pieces (0 < n < 129, n is a natural number) each time from the box, among which all the m pieces taken out earlier are white and the latter pieces are all black. Place all pieces around a circle in sequence uniformly. Thereafter according to the distributing of color of these pieces of this circle, taking out other pieces from this box and placing another circle in sequence uniformly, namely putting a new piece between two piece from head to end of this circle, put a black piece between every two white pieces; put a white between every white and black pieces; put a black between every two black pieces; put a white between every black and white pieces. In such way, there are also n pieces in the new circle. After that the original pieces are all taken off. Then repeat the above process. Doing in such way, that the original set of the pieces on the circle is taken away as soon as new set of the pieces has being placed on, how many white pieces are on the circle after the k-th time of repetition (k is a natural number, k < 10001)?

For example, when n=8, m=2, k=2, the situation is as following:


    Original    first repetition  second repetition

输入:

On every line a group of data is given. In each group there are three natural number n, m, k, separated by commas. After all data are given there is -1 as the symbol of termination.

输出:

Find the number of white pieces corresponding to the input data of each group. One line is for every output. Its fore part is a repetition of the input data and then it follows a colon and a space. The last part of it is the computed number of the white pieces.

样例输入:

8,2,2
3,1,1234
-1

样例输出:

8,2,2: 4
3,1,1234: 2

解题代码:

//* @author:
import java.util.*;
public class Main{
 

public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    int a[]=new int[200],b[]=new int[200];
    int n,m,k;
    while(sc.hasNext()) {
        String s=sc.nextLine();
        Scanner scan=new Scanner(s);
        scan.useDelimiter(",");
        n=scan.nextInt();
        if(n==-1) break;
        m=scan.nextInt();

        k=scan.nextInt();
        for (int i=1;i<=n;i++) {
            if (i<=m) a[i]=-1;
            else a[i]=1;
        }
        for (int j=1;j<=k;j++) {
            a[n+1]=a[1];
            for (int i=1;i<=n;i++) b[i]=a[i]*a[i+1];
            for (int i=1;i<=n;i++) a[i]=b[i];
        }
        int ans=0;
        for (int i=1;i<=n;i++) if (a[i]==-1) ans++;
        System.out.printf("%d,%d,%d: %d\n",n,m,k,ans);
    }
  }
}

  1. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  2. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。