Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4450716
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Python/Ruby

2012-05-04 17:17:33

  第二题叙述冗长,编程同时考察阅读能力,不认真看描述,可能就写不对程序。

  题目大意:在[0, 30] 的数中,把任意一数拆成一个triplet,使得triplet中三个数之和等于这个数,同时这三个数必须在[0, 10]范围内。这三个数中任意两个数差的绝对值等于0或者1的叫做normal,等于2的叫做surprising,大于2的不允许出现。
  示例:(8, 8, 8) and (7, 8, 7) are not surprising. (6, 7, 8) and (6, 8, 8) are surprising. (7, 6, 9) will never happen.


  现在给出若干个数,要把这若干个数中的每一个都拆分成为triplet,每一triplet中最大的一个数叫做“值”,这一triplet的属性可以是normal或者surprising,但是允许出现surprising的次数有限制,限制已经给出。
  求这若干个数拆成triplet后,“值”大于等于给定数(给定数的范围也是[0, 10])的triplet出现的总数。(注意surprising的限制)


  这一题目的关键是:0,1,29,30这几个特例,否则程序无法正确。

  普通情况的分析
  数字n :
                     normal   surprising
  n % 3 == 0: 18 -> [6,6,6], [5,6,7]       18 // 3 = 6
  n % 3 == 1: 16 -> [5,5,6], [4,6,6]       16 // 3 = 5
  n % 3 == 2: 17 -> [5,6,6], [5,5,7]       17 // 3 = 5

所以结论很简单:数字根据余3的结果分成3类,每一类有一个(normal, surprising) 的数据对,这个数据对是数字除以3所得的商来填写的。
  拿16,17,18举例:
  quotient := 16 // 3 得到数据对就是(quotient + 1, quotient + 1)
  quotient := 17 // 3 得到数据对就是(quotient + 1, quotient + 2)
  quotient := 18 // 3 得到数据对就是(quotient, quotient + 1)


  特殊情况分析
    0得到(0,0), 30得到(10, 10), 1得到(1,1),29得到(10, 10)


程序清单:

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # encoding: utf-8

  3. import sys


  4. def read_file(fname):
  5.     fd = open(fname)
  6.     times = fd.readline()
  7.     times = int(times.rstrip('\n'))
  8.     content = fd.readlines()[:times]
  9.     fd.close()
  10.     return content

  11. def write_file(result):
  12.     fd = open('b.txt', 'w')
  13.     fd.writelines(result)
  14.     fd.close()

  15. def process(combination, N, S, p):
  16.     total = 0
  17.     for item in combination:
  18.         if item[0] >= p: # normal >= p
  19.             total += 1
  20.         elif item[1] >= p: # surprising >= p
  21.             if S > 0:
  22.                 total += 1
  23.                 S -= 1
  24.     return total

  25. def control(content):
  26.     result = []
  27.     for i, line in enumerate(content):
  28.         combination = []
  29.         line = line.rstrip('\n').split()
  30.         for item in line[3:]:
  31.             base = int(item) // 3
  32.             if int(item) % 3 == 0:
  33.                 if int(item) == 0 or int(item) == 30: # Each triplet of scores consists of three integer scores from 0 to 10 inclusive
  34.                     combination.append((base, base)) # if it is 0 or 30
  35.                 else:
  36.                     combination.append((base, base + 1)) # normal, surprising
  37.             elif int(item) % 3 == 1: # 1 don't have surprising
  38.                 combination.append((base + 1, base + 1))
  39.             elif int(item) % 3 == 2:
  40.                 if int(item) == 29: # 29 don't have surprising, 11 is invalid
  41.                     combination.append((base + 1, base + 1))
  42.                 else:
  43.                     combination.append((base + 1, base + 2))
  44.         total = process(combination, int(line[0]), int(line[1]), int(line[2]))
  45.         result.append('Case #' + str(i+1) + ': ' + str(total) + '\n')
  46.     return result

  47. if __name__ == '__main__':

  48.     if len(sys.argv) < 2:
  49.         print('Please set input file as a parameter.')
  50.         sys.exit(1)

  51.     content = read_file(sys.argv[1])
  52.     result = control(content)
  53.     write_file(result)

阅读(1046) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~