“李白街上走,提壶去买酒,遇店加一倍,见花喝一斗”,途中,遇见5次店,见了10此花,壶中原有2斗酒,最后刚好喝完酒,要求最后遇见的是花,求可能的情况有多少种?希望大家分享一下思路,谢谢!
我的思路很混乱,觉得直接暴力枚举能解决,但是枚举所有的情况比较不现实,希望大家解答啊!
PHP中文网2017-04-17 11:24:48
"""
这是今年蓝桥杯java中的一道填空题,当时我还以为做对了,写了33
结果我忽略了最明显的判断条件,5次店和10次花,少了下面的 check1 函数
简直为自己的智商捉鸡,加了之后就是14了,
最近学python,现用python实现这题,跟大家分享
"""
count=0;
def check(t):
sum = 2;
for ch in t:
if ch=='1':
sum=sum*2;
if ch=='0':
sum=sum-1;
return sum;
def check1(t):
c = 0;
for ch in t:
if(ch=='1'):
c+=1;
if c==5:
return True;
return False;
def f(t):
if(len(t)==15):
if check(t)==0 and t[14:15]=='0' and check1(t):
global count;
count+=1;
print(" ".join(t));
return;
f(t+'1');
f(t+'0');
f('');
print(count);
巴扎黑2017-04-17 11:24:48
Simply give a non-recursive python code:
queue = [(2, 5, 10)]
total = 0
def iter():
global queue
while queue:
i, queue = queue[0], queue[1:]
yield i
for (left, dian, hua) in iter():
if dian == 0:
total += 1 if left == hua else 0
else:
if left > hua or left == 0 or hua ==0:
continue
queue.append((left*2, dian-1, hua))
queue.append((left-1, dian, hua-1))
大家讲道理2017-04-17 11:24:48
Violent exhaustion is easy to do, let’s not talk about it anymore
If the last time is a flower, the value of 2 is 1 after 14 transformations,
If it is 0 after seeing flowers for 10 times, the initial value is 2 buckets of wine, which means a total of 8 buckets of wine have been added
The minimum value for adding wine is 1, and the maximum value is 4 (11114),
Because adding wine is a doubling method, the difference between two adjacent numbers in the sequence cannot be greater than 2 times, that is, 11114 is excluded. The number of wine added may be in the range of 1-3. Also due to the reason of doubling, the number after 1 is only It can be 2 (but 3 is followed by 1)
Because the initial value is 2, the first time you add wine can only be 1 or 2 buckets
So just find a few series that meet the conditions