Python解决打靶问题

原帖见:http://blog.csdn.net/d8111/archive/2008/09/21/2956832.aspx

动态语言永远是那么的飘逸。就似绝世神剑。高手用来心中有剑,低手则走火入魔。

  1. from time import time
  2. def shot(n, remain, his):
  3.         """n goes 0-9"""
  4.         if(remain < 0 or remain > (n + 1) * 10):
  5.             return;
  6.         if(n == 0):
  7.             his[0] = remain;
  8.             print his
  9.             globals()["counter"] += 1
  10.         else:
  11.             for score in scores:
  12.                 his[n] = score;
  13.                 shot(n - 1, remain - score, his)
  14.                 
  15. if __name__ == "__main__":
  16.     scores = range(11)
  17.     init = [0000000000]
  18.     counter = 0
  19.     begin=time()
  20.     shot(990, init)
  21.     end=time()
  22.     print counter  
  23.     print end-begin

不过貌似比较可惜的地方是,耗时27.2189998627,比起java来可慢的多咯

你可能感兴趣的:(Python解决打靶问题)