- 論壇徽章:
- 0
|
無聊看到京東眾籌的抽獎算法,就簡單的實現(xiàn)了下,依據(jù)第三版抽獎規(guī)則編寫的:http://zbbs.jd.com/thread-399-1-1.html
JDRoll.png
1.png (21.49 KB, 下載次數(shù): 14)
下載附件
2015-07-10 13:22 上傳
JDRoll.java- import java.util.Scanner;
-
- public class JDRoll {
-
- public static void main(String[] args) {
- System.out.println("單行輸入,用空格隔開!");
- System.out.println("雙色球最大、最小、藍(lán)色、參與人數(shù)、獲獎名額:");
- Scanner scan = new Scanner(System.in);
- //雙色球最大最小及藍(lán)色球A
- String aStr = scan.next() + scan.next() + scan.next();
- int a = Integer.valueOf(aStr);
- System.out.println("A: " + a);
- //人數(shù)N
- String nStr = scan.next();
- int n = Integer.valueOf(nStr);
- System.out.println("N: " + n);
- //名額P
- String pStr = scan.next();
- int p = Integer.valueOf(pStr);
- System.out.println("P: " + p);
- //人數(shù)N除以名額P,取商B
- int b = n/p;
- System.out.println("N / P = B: " + b);
-
- //A除以B,余數(shù)為首個中獎號X
- int x = a%b;
- System.out.println("A % B = X: " + x);
-
- int[] rs = new int[p];
- rs[0] = x;
-
- //其他中獎號X+B,X+2B,X+3B,......,X+(P-1)B
- int i = 1;
- while(i < p) {
- rs[i] = x + i*b;
- ++i;
- }
-
- for (int j = 0; j < p; ++j) {
- System.out.println("第" + (j+1) + "個中獎號碼為:" + rs[j]);
- }
- }
-
- }
復(fù)制代碼 |
|