PAT-1002 A+B for Polynomials 解答(C++/Java/python)

1.Description:

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

Notes:

  •  Each case occupies 2 lines. N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​.

  • 1K10,0Nk<<N2<N1​​1000.

  • 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.

2.Example:

Input:
2 1 2.4 0 3.2 2 2 1.5 1 0.5
output:
3 2 1.5 1 2.9 0 3.2

 

3.Solutions:

C++ Version:

. 1 #include <the iostream>
 2 #include <Map>
 . 3 #include <Vector>
 . 4  the using  namespace STD;
 . 5  
. 6  int main () {
 . 7      Map < int , a float > Polynomial;
 . 8      for ( int I = 0 ; I < 2 ; I ++) { // row map stored inside 
. 9          int n-;
 10          Scanf ( " % D " , & n-); // read the value of K 
. 11          for ( int I =0 ; I <n-; ++ I) {
 12 is              int exp;
 13 is              a float COF;
 14              Scanf ( " % D% F " , & exp, & COF); // read coefficient and reading 
15              Polynomial [exp] + = COF; // corresponding coefficient value in the map 
16          }
 . 17      }
 18 is      Vector <pair < int , a float >> Output; // iterate map value ranging from 0 tuple is stored in the array 
. 19      for (= Auto Polynomial. rbegin ();! polynomial.rend IT = (); IT ++ ) {
 20 is          IF (IT-> SECOND =! 0) 
21             output.emplace_back(it->first, it->second);
22     }
23 
24     printf("%d", output.size());
25     for (int i = 0; i < output.size(); ++i) { //格式化输出
26         printf(" %d %.1f", output[i].first, output[i].second);
27     }
28     return 0;
29 }

 

Java Version:

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by SheepCore on 2020-2-26
 5  *
 6  */
 7 public class Main {
 8     public static void main(String[] args) {
 9         final int NUM = 2;
10         final int MAX = 1000;
11         float[] poly = new float[MAX];
12         Scanner scan = new Scanner(System.in);
13         for (int I = 0; I <NUM; I ++) { // read the input data into coefficients and press the array of 
14              int n-= scan.nextInt ();
 15              for ( int J = 0; J <n-; J ++ ) {
 16                  int exp = scan.nextInt ();
 . 17                  a float the conf = scan.nextFloat ();
 18 is                  poly [exp] + = the conf;
 . 19              }
 20 is          }
 21 is          int COUNT = 0 ;
 22 is          int [] = OUT new new  int [MAX] ;
 23          for ( int i = 0; i < MAX; i++) { //统计个数
24             if (poly[i] != 0.0) {
25                 out[count] = i;
26                 count++;
27             }
28         }
29         System.out.print(count);
30         for(int i = count - 1; i >= 0; i--) { //格式化打印
31             System.out.printf(" %d %.1f", out[i], poly[out[i]]);
32         }
33     }
34 }

 Note: I wrote this version of Java is only partially correct, find a lot of times, failed to identify the bug, kindly pointing Great God!

 

Python Version One:

. 1  DEF Solution1 ():
 2      IF  the __name__ == " __main__ " :
 . 3          '' ' to read the two lines into the polynomial List ' '' 
. 4          A = INPUT () Split ().
 . 5          B = INPUT () Split ().
 . 6          m = {}
 . 7  
. 8          K1 = the eval (A [0])
 . 9          K2 = the eval (B [0])
 10  
. 11          '' ' the first row elements stored in the dictionary by a factor of ' '' 
12 is          I = 0
 13 is          the while I < K1:
 14             m [A [I + 2 *. 1]] = the eval (A [2 * I + 2 ])
 15              I +. 1 =
 16  
. 17          '' ' of the second row elements stored in the dictionary by a factor of ' '' 
18 is          J = 0
 . 19          the while J < K2:
 20 is              IF B [2 * J +. 1] in m.keys ():
 21 is                  m [B [2 * J +. 1]] + = the eval (B [2 * J + 2 ])
 22 is              the else :
 23 is                  m [B [2 * J +. 1]] = the eval (B [2 * J + 2 ])
 24              J =. 1 +
 25          '' ' statistical value dictionary of the number of non-0 ' '' 
26 is          COUNT = 0
 27         for k, v in m.items():
28             if v != 0:
29                 count += 1
30 
31         '''格式化打印'''
32         if count:
33             print(count, end=" ")
34         else:
35             print(count, end="")
36         output = ""
37         if count != 0:
38             for (k, v) in sorted(m.items(), key=lambda x: x[0], reverse=True):
39                 if v != 0:
40                     output += k + " " + str(round(v, 1)) + " "
41             print(output.rstrip(), end='')

 Note: Do not use a function of the time of filing!

Python Version Two:

 1 def solution2():
 2     output = [0 for i in range(1001)]
 3     a = input().split(" ")
 4     b = input().split(" ")
 5     for i in range(int(a[0])):
 6         output[int(a[i * 2 + 1])] += float(a[i * 2 + 2])
 7     for i in range(int(b[0])):
 8         output[int(b[i * 2 + 1])] += float(b[i * 2 + 2])
 9     count = 0
10     for i in range(1001):
11         if output[i]:
12             count += 1
13     if count:
14         print(count, end=" ")
15     else:
16         print(count)
17     for i in range(1000, -1, -1):
18         if output[i]:
19             count -= 1
20             if count > 0:
21                 print(i, round(output[i], 1), end=" ")
22             else:
23                 print(i, round(output[i], 1))

Note: Do not use a function of the time of filing!

4.Summay

  1. If the sum value of 0, no output
  2. direct input size is equal to 0 0 (no spaces)
  3. There can be only one space output 

 Audio Bible, master, round trip! : D

 

Guess you like

Origin www.cnblogs.com/sheepcore/p/12369280.html