论坛首页 Java企业应用论坛

JAVA每日一题01

浏览 8629 次
精华帖 (0) :: 良好帖 (5) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-11   最后修改:2009-03-23

还是我来继续jythoner的JAVA每日一题吧!最近比较闲哦!希望jythone不要见怪哦!

真是不好意思啊!早上有点忙!呵呵!

 

题目:一项抽奖程序要求读者从整数1-49之间选择6个不同的数字。编写一个程序来完成这项工作,并生成5组结果。

 

package com.tengfei.lesson01;
public class Lottery {
  public static void main(String[]args) {
    int setCount = 5;      // Number of sets of lucky numbers.
    int setSize = 6;       // Number of lucky numbers in the set.
    int range = 49;        // Assume selecting integers between 1 and range.
    int lucky;             // Holds a lucky number candidate.
    int luckyCount;        // Holds count of lucky numbers in a set.

    for(int i = 0; i < setCount; i++) {
      int lucky1 = 0;                  // Lucky numbers for the set of 6.
      int lucky2 = 0;                   
      int lucky3 = 0;                   
      int lucky4 = 0;
      int lucky5 = 0;                   
      int lucky6 = 0;    

      luckyCount = 0;                   // Count of numbers found in the current set
      while(luckyCount < setSize) {
       // Generate a lucky number between 0 and 48 and add 1:
       lucky = (int)(range*Math.random()) + 1;
        switch(luckyCount) {
          case 0:                      // It is the first one 
            lucky1 = lucky;            // so just store it
            luckyCount++;              // and increment the count
            break;
          case 1:                      // For the second we must
            if(lucky != lucky1) {      // check that it is different from the first
              lucky2 = lucky;          // It is, so store it
              luckyCount++;            // and increment the count
            }
            break;
          case 2:                      // For the third we check aginst the previous two
            if(lucky != lucky1 && lucky != lucky2) {
              lucky3 = lucky;
              luckyCount++;
            }
            break;
           case 3:                     // Check against the previous three...
            if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3) {
              lucky4 = lucky;
              luckyCount++;
            }
            break;
           case 4:                     // Check against the previous four...
            if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3 && lucky != lucky4) {
              lucky5 = lucky;
              luckyCount++;
            }
            break;
           case 5:                    // Check against the previous five...
            if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3 && lucky != lucky4 && lucky != lucky5) {
              lucky6 = lucky;
              luckyCount++;
            }
            break;
        }
      }

      System.out.print("\nSet " + (i + 1) + ":");                        // Identify the set

      System.out.print(" " + lucky1 + " " + lucky2  + " " + lucky3  +    // and output the numbers
                       " " + lucky4  + " " + lucky5  + " " + lucky6);

       
      // If you want to be sure the numbers line up in columns you could use a
      // rather more complicated statement here instead of the above:
/*
      System.out.print((lucky1>9 ? " " :"  ") + lucky1 + 
                       (lucky2>9 ? " " :"  ") + lucky2 +
                       (lucky3>9 ? " " :"  ") + lucky3 +
                       (lucky4>9 ? " " :"  ") + lucky4 +
                       (lucky5>9 ? " " :"  ") + lucky5 +
                       (lucky6>9 ? " " :"  ") + lucky6);
*/
      // This makes use of the conditional operator to output an extra space
      // when a lucky number is a single digit.
    }
  }
}



 

 

 

   发表时间:2009-03-23  
import java.util.Arrays;
import java.util.Random;

public class Lottery {
	private Random rand = new Random();
	private int[] numbers;
	
	public Lottery(int range) {
		numbers = new int[range];
		for(int i=0; i<numbers.length; i++)
			numbers[i] = i+1;
	}
	
	private void swap(int i, int j) {
		if(i==j) return;
		int x = numbers[i];
		numbers[i] = numbers[j];
		numbers[j] = x;
	}
	
	public int[] randomChoose(int setSize)
	{
		int[] rt = new int[setSize];
		for(int i=0; i<setSize; i++)
		{
			int chosenIndex = rand.nextInt(numbers.length-i);
			rt[i] = numbers[chosenIndex];
			swap(chosenIndex, numbers.length-1-i);
		}
		return rt;
	}

	public static void main(String[] args) {
		Lottery l = new Lottery(49);
		for(int i=0; i<5; i++)
			System.out.println(Arrays.toString(l.randomChoose(6)));
	}

}
0 请登录后投票
   发表时间:2009-04-08  
不是很明白楼主的意思,一个人猜六次?怎么没显示正确的结果呢??
0 请登录后投票
   发表时间:2009-04-15  
每次运行一下LZ这个程序去买双色球
0 请登录后投票
   发表时间:2009-04-15  
三楼为什么要swap一下?
0 请登录后投票
   发表时间:2009-04-15   最后修改:2009-04-22
路过...

改了下,更傻的版本如下:
module Main where
import Random
import Data.List

main = do f []; f []; f []; f []; f [] where
	f li 
		|length li == 6 = print li
		|otherwise      = do
			n <- randomRIO (1::Int, 49)
			if elem n li then f li else f (n:li)

0 请登录后投票
   发表时间:2009-04-17   最后修改:2009-04-17
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
#define MAX 49
#define PUTVALUE 6
int number[MAX];
int putDigit[PUTVALUE];
void init()
{
     for(int i = 0;i < MAX;i++)
     {
          number[i] = i+1;   
     }
}
void _test()
{
    static int t = 0;
    init();
    int gg[100] = {0};
     for(int i = 0;i <PUTVALUE;)
     {
        int flag = t;
        srand(time(NULL));
        t = rand()%50;
        while(flag == t)
        { 
            srand(time(NULL));
            t = rand()%50;
        }
        //排重 
        for(int j = 0;j < 50;j++)
        {
           while(t == gg[j])
           {
               gg[j] = 0; 
               srand(time(NULL));
               t = rand()%50;
           }   
        }
         gg[i] = t;
         putDigit[i] = number[t];
          cout<<putDigit[i]<<" ";
         i++;
     } 
     cout<<endl;
}
int main()
{
     for(int j = 0;j < 5;j++){
       _test();
       cout<<"----------------------"<<endl;
       for(int i = 0;i < PUTVALUE;i++)
       {
            cout<<putDigit[i]<<" "; 
       }
       cout<<endl;
     }
     system("pause");
}
0 请登录后投票
   发表时间:2009-04-22   最后修改:2009-04-22
打酱油~~~~
import random

def gen(n,a=1,b=49):
 	result = []
 	while True:
 		tmp = random.randint(a,b)
 		if tmp not in result:
 			result.append(tmp)
 		if len(result) == n:
 			return result


if __name__ == "__main__":
    for i in xrange(5):
        print gen(6)



btw:为啥javaeye代码标签可以用jython不能用python?
0 请登录后投票
   发表时间:2009-04-22  
简洁的ruby:
5.times{ p (1..49).to_a.sort_by{rand}[0..5]}
0 请登录后投票
   发表时间:2009-04-22  
3L的swap是把最后一个数与随机到的数字做个交换,每次循环随机数的范围-1,这样不会选到重复的数字
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics