zoukankan      html  css  js  c++  java
  • PAT-1002 A+B for Polynomials 解答(C++/Java/python)

    1.Description:

    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<iostream>
     2 #include<map>
     3 #include<vector>
     4 using namespace std;
     5 
     6 int main() {
     7     map<int, float> polynomial;
     8     for (int i = 0; i < 2; ++i) { //按行存入map里面
     9         int n;
    10         scanf("%d", &n); //读取K值
    11         for (int i = 0; i < n; ++i) {
    12             int exp;
    13             float cof;
    14             scanf("%d%f", &exp, &cof); //读系数和读值
    15             polynomial[exp] += cof; //将对应系数值存入map中
    16         }
    17     }
    18     vector<pair<int, float> > output; //遍历map将值不等0的元组存入数组里
    19     for(auto it = polynomial.rbegin(); it != polynomial.rend(); it++) {
    20         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++) { //读取输入数据并按系数存入数组中
    14             int n = scan.nextInt();
    15             for (int j = 0; j < n; j++) {
    16                 int exp = scan.nextInt();
    17                 float conf = scan.nextFloat();
    18                 poly[exp] += conf;
    19             }
    20         }
    21         int count = 0;
    22         int[] out = 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: 我写的这个Java版本只是部分正确,找了很多遍,没有找出bug,恳请大神指点!

    Python Version One:

     1 def solution1():
     2     if __name__ == "__main__":
     3         '''读取两行多项式分割成list'''
     4         a = input().split()
     5         b = input().split()
     6         m = {}
     7 
     8         k1 = eval(a[0])
     9         k2 = eval(b[0])
    10 
    11         '''将第一行元素按系数存入字典中'''
    12         i = 0
    13         while i < k1:
    14             m[a[2*i+1]] = eval(a[2*i+2])
    15             i += 1
    16 
    17         '''将第二行元素按系数存入字典中'''
    18         j = 0
    19         while j < k2:
    20             if b[2*j+1] in m.keys():
    21                 m[b[2*j+1]] += eval(b[2*j+2])
    22             else:
    23                 m[b[2 * j + 1]] = eval(b[2 * j + 2])
    24             j += 1
    25         '''统计字典中值为非0的个数'''
    26         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: 提交的时候不要用函数!

    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: 提交的时候不要用函数!

    4.Summay

    1. 如果相加后值为0,不进行输出
    2. size等于0直接输入0(无空格)
    3. 输出时只能有一个空格 

     水滴石穿,笨鸟先飞!:D

  • 相关阅读:
    别闹了,这些都不是数字化转型
    对不起,“下一代ERP”仍旧是现在的ERP
    这世界真小
    SAP S4HANA 2020 Fully-Activated Appliance 虚拟机版分享
    花费巨资参加SAP培训真的有用吗?
    剑指 Offer 07. 重建二叉树
    剑指 Offer 06. 从尾到头打印链表
    剑指 Offer 05. 替换空格
    剑指 Offer 04.二维数组中的查找
    剑指 Offer 03. 数组中重复的数字
  • 原文地址:https://www.cnblogs.com/sheepcore/p/12369280.html
Copyright © 2011-2022 走看看