1, given a maximum value n (10>n>=0), to find the number of integers between 0-n can be composed of how many odd numbers.
Analysis:
First of all, the composition of the number of the first: can not be 0, can put the number n
and then analysis of the last: must be odd, can put the number for Odd[1,n] a
final analysis of the middle bit: the range of any number, can be placed in the number of n+1
def get_odd (n): I
= n last
= Len ([x to X in range (n+1) if x%2!=0])
sum = Last+first*last
if n <2 : Return sum to
I in range (2,n+1):
middle = (n + 1) * * (i-1)
sum = first*middle*last return
sum
10, there are a pile of peaches on the beach, five monkeys to score. The first monkey put the pile of peaches on average five, one more, the monkey ate more than one, took a copy. The second monkey divided the remaining peaches into an average of five, another one, it also ate more than one, took a copy, third, four, fifth monkeys are doing so, asked the beach on the original at least how many peaches
def get_min_peaches (n):
i = 1
plus = n (n-1)
def if_match (ret): for
J in Range (n):
MODN = (ret-1)% n
if modn!= 0: Return
False
ret = (ret-1)/n* (n-1) return
true while
true:
if If_match (i):
break
i + = plus return
i
11, a frog can jump up to 1 steps at a time, can also jump up to 2 levels ... It can also jump up n levels. How many jumps are there in total for the frog to jump up the N-level steps?
def jump_step (n): result
= lambda n:n if n < 2 else 2 * JUMP_STEP (n-1) return result
(n)
100. Realize a binary tree
class Tree (): def __init__ (self,ltree = 0,rtree = 0,data = 0): Self.ltree = Ltree
Self.rtree = Rtree Self.data = Data Class Btree (): def __init__ (self,base = 0): Self.base = base def _empty (self): if self.base = = 0:return True else:return False def Qout (self,tree_base): if tree_base = = 0:return Print Tree_base.data self.qout (tree_b
Ase.ltree) Self.qout (tree_base.rtree) def mout (self,tree_base): if tree_base = = 0:return Self.mout (tree_base.ltree) print Tree_base.data self.mout (tree_base.rtree) def hout (Self,tree _base): if tree_base = = 0:return Self.hout (tree_base.ltree) self.hout (tree_base.rtre e) Print Tree_base.data