23660: 7的倍数取法有多少种
http://cs101.openjudge.cn/practice/23660/
在n个不同的正整数里,任意取若干个,不能重复取,要求它们的和是7的倍数,问有几种取法。
输入
第一行是整数t,表示有t组数据(t<10)。接下来有t行,每行是一组数据,每组数据第一个数是n(1 <= n <= 16),表示要从n个整数里取数,接下来就是n个整数。
输出
对每组数据,输出一行,表示取法的数目(一个都不取也算一种取法)。
样例输入
3
3 1 2 4
5 1 2 3 4 5
12 1 2 3 4 5 6 7 8 9 10 11 12样例输出
2
5
586来源
郭炜
python
def count_combinations(numbers, index, current_sum, count):
if index >= len(numbers):
if current_sum % 7 == 0:
return count + 1
else:
return count
# 选择取当前位置的数
count = count_combinations(numbers, index + 1, current_sum + numbers[index], count)
# 选择不取当前位置的数
count = count_combinations(numbers, index + 1, current_sum, count)
return count
# 主程序
t = int(input())
for _ in range(t):
data = list(map(int, input().split()))
n = data[0]
numbers = data[1:]
result = count_combinations(numbers, 0, 0, 0)
print(result)