PAT 1002 A+B for Polynomials (25)__PAT集錦

來源:互聯網
上載者:User

題目英文 題目中文 輸入 輸出 樣本輸入 樣本輸出 分析 程式1 程式2

題目英文

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

Input

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

For each test case you should output the sum 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 to 1 decimal place.
Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 2 1.5 1 2.9 0 3.2 題目中文

這一次,希望你可以計算出A+B,這次的A,B都是多項式。 輸入

每個輸入檔案包含一個測試案例。每個測試案例佔兩行,每一行的資訊類似如下多項式:k N1 aN1 N2 aN2 …NK aNK ,其中k是多項式中非0的項數, Ni和aNi是其中的典型的指數和係數。K將會是1到10之間的一個數,指數NK小於1000。 輸出

對於每個測試案例你都應該在一行中輸出A和B的加和。與輸入的格式一致。注意在每一行末尾不可以有空格。請精確到數字後面的一位。 樣本輸入

2 1 2.4 0 3.2
2 2 1.5 1 0.5 樣本輸出

3 2 1.5 1 2.9 0 3.2 分析

從標準的資料來看,系統希望我們按照降冪來排列
程式1

分析,發現愛上了python,也不知道是否上機場地都支援python,但是單純的先使用起來,畢竟高效簡潔,其他關於python和c的對比我也沒必要說,網路大把,此處喜歡用python。
看到這種多項式,想起了使用字典來記錄相應冪的係數,然後可以用字典相加的演算法計算了。
nnd 看到很多都不支援python,並且pat對python支援也很爛,哎,理想很豐滿,現實很骨感,看來還得是c或者c++
下面的程式可以滿足樣本運行,但是不知道為什麼無法通過測試,只好放一放了。。。。。。

import stringinput1 = raw_input()input2 = raw_input()array1 = input1.split()array2 = input2.split()print array1,array2dirt1 = {}dirt2 = {}for i in range(int(array1[0])):        dirt1[array1[2*i+1]] = array1[2*i+2]for i in range(int(array2[0])):        dirt2[array2[2*i+1]] = array2[2*i+2]dirttotal = dirt1.copy()for k,v in dirt2.items():        if k in dirt1.keys():                dirttotal[k] = string.atof(dirttotal[k]) + string.atof(v)        else:                dirttotal[k] = string.atof(v)result = sorted(dirttotal.items(),key = lambda e : e[0],reverse = True)print len(result),for item in result:        print item[0],        print item[1],print ''
程式2

還是迴歸到我比較熟悉的C上去吧,說熟悉,其實也不熟悉,呵呵
心得1:scanf後面再次scanf需要使用fflush清空stdin緩衝器
心得2:scanf的時候,資料需要嚴格執行,要麼是int,要麼是float,之前將後面的float給截斷了用了int,導致了int無法正確識別,從而導致了scanf很快就退出了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.