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

POJ 3002 Jackpot [解题报告] Java

Jackpot

问题描述 :

Bill has found the perfect way to make money playing the slot machines. After months of careful research, he has finally figured out the mechanics behind how the machines operate. Now he is ready to make profit of his findings.

But first an introduction to the game. A slot machine consists of a number of wheels, usually three or four, each with a number of symbols printed on it – cherries, oranges, bells, etc. – and will show one of its symbols at a given time. To play, you insert a coin, push a button and the wheels start spinning. After spinning for a while, each wheel stops – at random it seems – at one of its symbols. If all wheels stop at the same symbol, or some nice combination of symbols, the player wins. One combination that is especially desirable is having the jackpot symbol on all wheels. This combination is simply called ’jackpot’ and will make you rich for life.

What Bill has discovered is that each wheel will stop at the jackpot symbol with a certain periodicity, which differs a lot between wheels. He has also figured out (after some sneeking around at the slot-machine factory) that all newly manufactured slot-machines are delivered showing the jackpot combination, and that they all have a counter at the back, telling how many times the machine has been played. This counter is always set to zero at delivery.

Now, all Bill needs to do is to calculate the number of times a machine has to be played between two occurrences of the jackpot combination. We will call this number the jackpot periodicity. This is of course the same as the number of times the machine has to be played after leaving the factory, before it gives its first jackpot. Thus, with a glance at the counter on the back of a machine, Bill can figure out if it is about to give a jackpot.

As Bill knows that you are a skillful computer programmer, he turns to you with the problem of calculating the jackpot periodicity. For each machine, he will give you the number of wheels, and the periodicity with which the jackpot symbol shows up on each wheel.

输入:

One line with the number of machines n ≤ 20. For each machine, one line with the number of wheels w ≤ 5, and one line with w numbers, p1, …, pw the periodicity of each wheel pk ≤ 1000.

输出:

One line per machine: The jackpot periodicity of the machine, if it is less than or equal to a billion (109), otherwise output the text ’More than a billion.’.

样例输入:

1
3
10 6 15

样例输出:

30

解题代码:

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

 static long gcd(long a,long b)
{
  if(b==0) return a;
  return gcd(b,a%b);
}


 public static void main(String args[])
{
 Scanner sc=new Scanner(System.in);
 int t,n,i;
 long p[]=new long[6];
 t=sc.nextInt();
 while((t--)!=0)
 {
    n=sc.nextInt();
    for(i=0;i< n;i++)
      p[i]=sc.nextLong();
    long u=p[0];
    boolean bb=false;
    for(i=1;i< n;i++)
    {
	u=u*p[i]/gcd(p[i],u);
	if(u>1000000000){
         bb=true;
	  break;
	}
    }
    if(!bb) System.out.printf("%d\n",u);
    else System.out.println("More than a billion.");
   }
  }
}

  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?