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.
时间: 2023-09-11 15:09:27 浏览: 43
Here is a Python program that takes k and S as input and generates all possible games:
```python
import itertools
k = int(input("Enter k: "))
S = set(map(int, input("Enter subset S: ").split()))
# Generate all possible games
games = itertools.combinations(S, 6)
# Print the games
for game in games:
print(list(game))
```
Explanation:
1. We first read in the value of k and the set S from the user.
2. We use the `combinations` function from the `itertools` module to generate all possible combinations of 6 elements from the set S.
3. We then loop through each combination and print it as a list.
Sample Input:
```
Enter k: 8
Enter subset S: 1 2 3 5 8 13 21 34
```
Sample Output:
```
[1, 2, 3, 5, 8, 13]
[1, 2, 3, 5, 8, 21]
[1, 2, 3, 5, 8, 34]
[1, 2, 3, 5, 13, 21]
[1, 2, 3, 5, 13, 34]
[1, 2, 3, 5, 21, 34]
[1, 2, 3, 8, 13, 21]
[1, 2, 3, 8, 13, 34]
[1, 2, 3, 8, 21, 34]
[1, 2, 3, 13, 21, 34]
[1, 2, 5, 8, 13, 21]
[1, 2, 5, 8, 13, 34]
[1, 2, 5, 8, 21, 34]
[1, 2, 5, 13, 21, 34]
[1, 2, 8, 13, 21, 34]
[1, 3, 5, 8, 13, 21]
[1, 3, 5, 8, 13, 34]
[1, 3, 5, 8, 21, 34]
[1, 3, 5, 13, 21, 34]
[1, 3, 8, 13, 21, 34]
[1, 5, 8, 13, 21, 34]
[2, 3, 5, 8, 13, 21]
[2, 3, 5, 8, 13, 34]
[2, 3, 5, 8, 21, 34]
[2, 3, 5, 13, 21, 34]
[2, 3, 8, 13, 21, 34]
[2, 5, 8, 13, 21, 34]
[3, 5, 8, 13, 21, 34]
```