ask:
[Title] basis: there are n personal cordons Arranging the order. From the first person countin (report number from 1-3), who report 3 people exit the circle, and asked who the last remaining original No. few. (N entered by the keyboard, for example n = 100)
[Raise] the question: have a bunch of peaches, five monkeys to points on the beach. The first monkey credentials this pile of peaches divided into five parts, one more, one more than the monkey thrown into the sea, he took one. The second monkey and the rest of the peaches were divided into five, one more, it also put more than one thrown into the sea, took a third, fourth, fifth monkeys are doing and I asked originally on the beach at least how many peaches?
method 1:
n = int(input("输入参与人数:")) steps, num_list,kill_num_list = 3, list(map(int, list(range(1, n + 1)))), [] step, num_list_bk = steps - 1, num_list[:] def ysfh(steps): global num_list, step out_number = num_list[step] kill_num_list.append(out_number) print("{} was killed!".format(out_number)) print(num_list) print(step) step = step + steps while step >= len(num_list): STEP -=len (num_list) num_list = List (SET (num_list) - SET (kill_num_list)) num_list.sort () the else : IF len (num_list) ==. 1 : Pass the else : ysfh (Steps) ysfh (Steps) Print ( " Last {a} is the number of surviving " .format (num_list_bk.index (num_list.pop (0)) +. 1))
Method 2:
int = NUM (INPUT ( ' Enter the total number of people: ' )) # the owner into an array list_count = List (Range (. 1, NUM +. 1 )) # set a variable, for calculating the number of packets COUNT = 0 the while len (list_count)>. 1: # circulated when there are at least 2 array element list_count_new list_count = [:] # copies the original array into a new array, for limiting the number of times the inner loop for I in Range (0, len (list_count_new)): # the inner loop is started, the number of packets start from the first individual count = count +. 1 # per packet time, count counter is incremented. 1 IF count == 0% 3: # If the count is divisible by 3 , is a report of 3 people Print ( ' No. out of {} ' .format (list_count_new [I])) list_count.remove (list_count_new [I]) # The report 3 people to remove the original array, for the next cycle Print ( ' final left is the original number of {} ' .format (list_count [0]))
Method 3:
INPUT = n-( ' Enter Number ' ) Circle = List (Range (. 1, int (n-) +. 1 )) I = 0 COUNT =. 1 the while len (Circle)>. 1 : IF COUNT ==. 3 : Print (F ' {circle [i]} dequeue ' ) del (Circle [I]) COUNT =. 1 I = (I +. 1)% len (Circle) COUNT + =. 1 Print (F ' last dequeued is {circle [0] } ' )
Method 4:
INPUT = n-( ' Enter Number ' ) Circle = List (Range (. 1, int (n-) +. 1 )) I = 0 COUNT =. 1 the while len (Circle)>. 1 : IF COUNT ==. 3 : Print (F ' {circle [i]} dequeue ' ) del (Circle [I]) COUNT =. 1 I = (I +. 1)% len (Circle) COUNT + =. 1 Print (F ' last dequeued is {circle [0] } ' )
Method 5:
def circulation(peopleNumber): lic = list(range(1, peopleNumber+1)) count = 0 while len(lic) >1: lico = lic[:] print(lico) for i in range(0, len(lico)): count = count+1 if count % 3 == 0: lic.remove(lico[i]) print(lic) if __name__ == "__main__": peopleNumber Int = (the INPUT ( ' Please enter how many people: ' )) . Circulation (peopleNumber)
[Raise] the question: have a bunch of peaches, five monkeys to points on the beach. The first monkey credentials this pile of peaches divided into five parts, one more, one more than the monkey thrown into the sea, he took one. The second monkey and the rest of the peaches were divided into five, one more, it also put more than one thrown into the sea, took a third, fourth, fifth monkeys are doing and I asked originally on the beach at least how many peaches?
method 1:
rest = 4 while True: flag = 1 sum = rest for i in range(5): if sum % 4 != 0: flag = 0 break sum = sum * 5.0 / 4 + 1 if flag == 1: break else: rest += 4 print(f'最少有{int(sum)}个')
Method 2:
# The algorithm proceeds from the last monkey reverse down DEF Peach (Monkey =. 5 ): PEA =. 4 # last monkey will spread the remaining peaches the while . 1 : NUM = PEA for I in Range (Monkey): NUM = NUM + NUM / . 1 + 4 IF ! NUM = 0. 1%: # If the fractional separation of the ends of the inner loop PEA + 4 = # last peach must be an integer multiple of 4 BREAK IF NUM == 0. 1%: # If aliquot the end BREAK return PEA, NUM IF __name__ ==" __Main__ ' : pea whether = Peach () Print (pea, whether)
Method 3:
# Enter the number of monkeys Monkey = int (INPUT ( " the Input Monkey NUM: " )) # define the total number of function Peaches DEF Show (n-): # of cycles for I in Range (. 1,. 1 + Monkey ): # at a monkey number peach be entrained T = (n--. 1) / monkey # formatted output Print (U ' % d% d has a peach, of eating monkeys% i 1, take of% s. ' % (I, n-, I, T)) # after a monkey away before the remainder of a total number of peach peaches n-= (. 1-monkey) * T # define performance function defFun (): # starting at 1 k = 1 the while True: t = k # cycles for i in the Range (Monkey-1 ): # Current monkeys should take a few peaches to tc, the total amount should take before eating monkey * tc + 1, the previous number of peach monkey took TP, there is (monkey-. 1) * TC + TP * monkey. 1 = T = T + *. 1 monkey # is contained in the for loop break directly terminates the loop, not performing else iF T% (. 1-Monkey): BREAK T / = (. 1-Monkey ) # when the End of iterations iteration object is empty and located in the else clause executed, i.e. the smallest integer item found else : Print ( ' If only monkey% d: ' % Monkey) Print ( ' Number of peaches for the% d: ' % (Monkey T + *. 1 )) Show (Monkey * T +. 1 ) BREAK K + =. 1 IF the __name__ == ' __main__ ' : Fun ()