1009-1012

来源:互联网 发布:淘宝交易款临时冻结 编辑:程序博客网 时间:2024/06/05 07:35

1009. Product of Polynomials (25)

This time, you are supposed to find A*B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 3 3.6 2 6.0 1 1.6

代码:

# 1009# author@cxlualine1 = input().split()line2 = input().split()# line1 = ['2', '1', '2.4', '0', '3.2']# line2 = ['2', '2', '1.5', '1', '0.5']# dict1 = {}num = 0dic = {}for i in range(1, len(line1), 2):#   dict1[int(line1[i])] = float(line1[i+1])# for i in range(1, len(line2), 2):#   dict2[int(line2[i])] = float(line2[i+1])# for key, value in dict2.items():    for j in range(1, len(line2), 2):        index = int(line1[i]) + int(line2[j])        if index not in dic.keys():            dic[index] = float(line1[i+1]) * float(line2[j+1])        else:            dic[index] += float(line1[i+1]) * float(line2[j+1])for key, value in dic.items():    dic[key] = round(value, 1)dic_sorted = sorted(dic.items(), key = lambda x : x[0], reverse = True)result = ''for i in range(len(dic_sorted)):    if dic_sorted[i][1] != 0:        result += ' ' + str(dic_sorted[i][0]) + ' ' + str(dic_sorted[i][1])        num += 1result = str(num) + resultprint(result)

用两行输入的嵌套循环建立字典,注意系数为零的项不应输出,以及不为零的时候项数(num)才加1.

1010. Radix (25)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.

Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible

代码:

#author@cxlualine1 = input().split()# num1 = line1[0]# num2 = line1[1]tag = int(line1[2])radix_of_tag = int(line1[3])###字符串操作dic = {}for i in range(48, 58):    dic[chr(i)] = i - 48for i in range(97, 123):    dic[chr(i)] = i - 87num_fixed = ''num_tosolve =''if tag == 1:    num_fixed = line1[0]    num_tosolve = line1[1]else:    num_fixed = line1[1]    num_tosolve = line1[0]def toint(a, r):    num = 0    b = a[::-1]    for i in range(len(a)):        num += dic[b[i]] * (r ** i)    return numdef convert(a, r):    global num_fixed_int    num = 0    b = a[::-1]    for i in range(len(a)):        num += dic[b[i]] * (r ** i)        if num > num_fixed_int:            return -1    if num == num_fixed_int:        return 1    else:        return 0def binarysearch(a):    global num_fixed_int, num_tosolve    R = num_fixed_int    L = dic[max(num_tosolve)] + 1    while L <= R:        mid = (L + R) // 2        res = convert(num_tosolve, mid)        if res == 1:            return mid        elif res == -1:            R = mid - 1        else:            L = mid + 1    return 'Impossible'if num_fixed == num_tosolve:    if len(num_fixed) == 1:         print(int(num_fixed) + 1)    else:        print(radix_of_tag)else:    num_fixed_int = toint(num_fixed, radix_of_tag)    if len(num_tosolve) == 1:        if toint(num_tosolve, 2) == num_fixed_int:            print(dic[max(num_tosolve)] + 1)        else:            print('Impossible')    else:        result = binarysearch(num_tosolve)        print(result)
  1. 简单顺序查找会超时,应使用二分查找,初始最小值是待求解数字的最小可能进制,即最大位数字加1,注意不是2,否则例如a0最小进制是11,表示数字为110,而不应该等于十进制的100;初始最大值是已知数字的值。
  2. 考虑两个数字相等的情况,若只有一位,则按照“输出最小进制”的要求,应输出该数字代表的值加1,若多于一位,则输出给定的进制。注意:若都是0,应输出2,而不是1。
  3. 多解情况只有在待求解数字是一位数字会出现,这时若待求解数字等于已知数字的值,则输出待求解数字的值加1,若不相等则输出’Impossible’。
  4. 算法剪枝:若当前求得的待求解数字的值已经大于已知数字的值,则不再继续求解当前值,而是直接试探更小的值。
    (虽然2和4不考虑好像也能过……)

1011. World Cup Betting (20)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

Chinese Football Lottery provided a “Triple Winning” game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results – namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner’s odd would be the product of the three odds times 65%.

For example, 3 games’ odds are given as the following:

W T L
1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1
To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).

Input

Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

Output

For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

Sample Input
1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1
Sample Output
T T W 37.98

代码:

#author@cxlualines = []dic = {}for i in range(3):    dic[i] = {}    lines.append(input().split())    dic[i]['W'] = float(lines[i][0])    dic[i]['T'] = float(lines[i][1])    dic[i]['L'] = float(lines[i][2])maxodd = []for i in range(3):    maxodd.append(max(dic[i], key = dic[i].get))profit = 1for i in range(3):    profit *= dic[i][maxodd[i]]profit = round((profit * 0.65 - 1) * 2, 2)result = ' '.join(maxodd)result += ' ' + str(profit)print(result)

这题没啥好说的吧,开始一个一个写的,感觉有点蠢就改成循环了。注意一下迭代对象为字典的max()函数的用法。

1012. The Best Rank (25)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output “N/A”.

Sample Input
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999
Sample Output
1 C
1 M
1 E
1 A
3 A
N/A

代码:

#author@cxluarecord = []line1 = input().split()num_record = int(line1[0])num_check = int(line1[1])for i in range(num_record):    record.append(input().split())check = []for i in range(num_check):    check.append(input())################ test ######################## num_record = 5# num_check = 6# record = [# ['310101', '98', '85', '88'],# ['310102', '70', '95', '88'],# ['310103', '82', '87', '94'],# ['310104', '91', '91', '91'],# ['310105', '85', '90', '90']# ]# check = ['310101','310102','310103','310104','310105','999999']#############################################def avg(a):    return round(sum(a) / len(a), 2)record_dic = {}for i in range(num_record):    record_dic[record[i][0]] = []    for j in range(1, 4):        record[i][j] = int(record[i][j])        # record_dic[record[i][0]].append(int(record[i][j]))    record_dic[record[i][0]].append(avg(record[i][1: 4]))    for j in range(1, 4):        record_dic[record[i][0]].append(record[i][j])# print(record_dic)rank = {}for key in record_dic.keys():        rank[key] = []for i in range(4):    a = sorted(record_dic.items(), key = lambda x: x[1][i], reverse = True)    # print(a)    for j in range(num_record):        #flag = 1        if j == 0:            rank[a[j][0]].append(1)        else:            if a[j][1][i] == a[j-1][1][i]:                rank[a[j][0]].append(rank[a[j-1][0]][i])            else:                rank[a[j][0]].append(j + 1)# print(rank)table = {1: 'C', 2: 'M', 3: 'E', 0: 'A'}for i in range(num_check):    if check[i] in rank.keys():        best_rank = min(rank[check[i]])        best_sjt = table[rank[check[i]].index(best_rank)]        print(str(best_rank) + ' ' + best_sjt)    else:        print('N/A')

tips:
1. 建立两个字典,分别存放学生的四个成绩和四个名次,键是学号,值是list.
2. 对每一个成绩循环排序,名次要考虑有相同成绩的情况,所以这里不能直接用a.index(), 只能循环赋值了,与前面的学生分数相同时,名次赋值为前一个学生的名次,不同时赋值为当前索引值+1,这里开始的时候错误地赋值为前一个名次+1了,case_3错误。
3. 对list找最小值元素并返回索引值时,若有相同返回的是索引最小的,所以根据题目的优先级要求,这里自然想到把成绩和名次的字典值List的元素直接按照优先级由大到小放置。
4. check[]输入时只有一个字符串,不需要split()啦不然check的元素又变成list了……第一次提交全是返回非零(unhashable)orz