您的位置:首页 > 其它

HDOJ 1342 Lotto

2011-07-18 10:41 211 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1342

题意:In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49}. A popular strategy to play Lotto - although it doesn't increase your chance of winning - is to select a subset S containing k (k>6) of these 49 numbers, and then play several games with choosing numbers only from S. For example, for k=8 and S = {1,2,3,5,8,13,21,34} there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], ... [3,5,8,13,21,34].

Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.

代码:
import java.util.Scanner;

public class Main {

	
	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		while(n!=0){
			int a[] = new int [n+1];
			int b[] = new int[7];
			for(int i = 1 ; i<=n;i++){
				a[i] = scan.nextInt();
			}
			new Main().subset(a, n, b, 1, true, 1);
			new Main().subset(a, n, b, 1, false, 1);
			
			n = scan.nextInt();
			if(n!=0)
				System.out.println();
		}

	}
	public void subset(int a [] ,int n,int b[] ,int i , boolean show , int count ){
		if(i>n)
			return;
		if(show){
			b[count] = a[i];
			if(count ==6){
				for(int x = 1 ; x<=6; x++){
					System.out.print(b[x]);
					if(x!=6)
						System.out.print(" ");
				}
				System.out.println();
			}
			else{
				subset(a,n,b,i+1,true,count+1);
				subset(a,n,b,i+1,false,count+1);
			}
		}
		else{
			subset(a,n,b,i+1,true,count);
			subset(a,n,b,i+1,false,count);
		}
	}

}
最后又是格式的问题纠结了一会,还是看题不仔细,最后一行加了空行。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: