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.
代码:
题意: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); } } }最后又是格式的问题纠结了一会,还是看题不仔细,最后一行加了空行。。。
相关文章推荐
- hdoj1342Lotto【dfs】
- HDOJ1342 Lotto(DFS,回溯)
- HDOJ1342lotto(DFS)
- HDOJ 题目1342Lotto(DFS)
- hdoj 1342 Lotto
- hdoj 1342 Lotto【dfs】
- hdoj 1342 Lotto 【全排列(从n个数中选m个数按字典需排列)】
- hdoj_1342Lotto
- hdoj_1342Lotto
- hdoj--1342--Lotto(dfs)
- hdoj1342 Lotto
- hdoj_1342Lotto
- hdoj--1342--Lotto(dfs)
- hdoj 1342 Lotto 【经典DFS】 【附上三种DFS写法】
- HDU 1342 Lotto
- HDU 1342 Lotto
- hdu 1342 Lotto (简单dfs)
- HDU 1342 Lotto(DFS)
- 1342 Lotto【dfs】
- hdu 1342&&poj 2245&&zoj 1089 Lotto