PAT 1002 A+b for polynomials __pat collection

Source: Internet
Author: User

Chinese Input Output example input output Analysis Program 1 Program 2

Topic English

This is 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 Coeffici Ents, 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 is NO extra spaces at the "End of". You are 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 topic Chinese

This time, I hope you can calculate the a+b, this time the a,b is polynomial. input

Each input file contains a test case. Each test case takes up two lines, and the information in each row resembles the following polynomial: K N1 aN1 N2 aN2 ... NK Ank, where K is the number of items in the polynomial Central Africa 0, NI and Ani are typical indices and coefficients. K will be a number between 1 and 10, with an exponent of NK less than 1000. Output

For each test case you should output the tax of A and B on one line. Consistent with the format entered. Note that you cannot have spaces at the end of each line. Please be accurate to the one behind the number. 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 Analysis

Judging from the standard data, the system wants us to rank by the power down.
Program 1

Analysis, found in love with Python, also do not know whether the machine site support Python, but simple to use first, after all, efficient and concise, and other about Python and C contrast I do not need to say, the network is a lot, here like Python.
See this polynomial, think of the use of a dictionary to record the corresponding power coefficients, and then can be added by the dictionary algorithm calculation.
Nnd see a lot of do not support Python, and Pat is very bad for Python support, Ah, ideal is very full, the reality is very bony, it seems to be C or C + +
The following program can satisfy the example run, but do not know why can not pass the test, had to put a release ...

Import string
input1 = Raw_input ()
input2 = Raw_input ()
array1 = Input1.split () array2
= Input2.split ()
print Array1,array2
dirt1 = {}
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 ''
Program 2

or return to my more familiar C go up, say familiar, actually also not familiar with, hehe
Experience 1:scanf back again scanf need to use Fflush to empty stdin buffers
When the 2:scanf, the data needs to be strictly enforced, or int, or float, before the float is truncated to use an int, resulting in an int not properly recognized, resulting in the scanf quickly quit the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.