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 N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (,) are the exponents and coefficients, respectively. It is given that 1,0.

Output Specification:

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:

  1. 2 1 2.4 0 3.2
  2. 2 2 1.5 1 0.5
 

Sample Output:

  1. 3 2 1.5 1 2.9 0 3.2

题解:用C++写了两次只通过了部分数据额,最后看别人的博客是用C语言来写的,确实,用C++来控制输入输出的格式不太好控制。

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. float a[1001] = {0};
  7. int k;
  8. int exp;
  9. float coe;
  10.  
  11. cin >> k;
  12. for(int i = 0; i < k; ++i) {
  13. cin >> exp >> coe;
  14. a[exp] += coe;
  15. }
  16.  
  17. cin >> k;
  18. for(int i = 0; i < k; ++i) {
  19. cin >> exp >> coe;
  20. a[exp] += coe;
  21. }
  22.  
  23. int count = 0;
  24. for (int i = 0; i < 1001; ++i) {
  25. if (a[i] != 0) count++;
  26. }
  27.  
  28. cout << count;
  29. for (int i = 1000; i >= 0; --i) {
  30. if (a[i] > 0)
  31. cout << " " << i << " " << a[i];
  32. }
  33.  
  34. }

  


2021-01-28

Python列表表达式:[ expression for i in iterable ]

poly = [0 for _ in range(1001)]

按照习惯,有时候单个独立下划线是用作一个名字,来表示某个变量是临时的或无关紧要的。

例如,在下面的循环中,我们不需要访问正在运行的索引,我们可以使用“_”来表示它只是一个临时值

  1. poly = [0 for _ in range(1001)]
  2.  
  3. def add():
  4. global poly
  5. line = input().split()[1:]
  6. i = 0
  7. while i < len(line) - 1:
  8. poly[int(line[i])] += float(line[i + 1])
  9. i += 2
  10.  
  11. add()
  12. add()
  13.  
  14. count = 0
  15. for i in range(1000, -1, -1):
  16. if poly[i] != 0:
  17. count += 1
  18.  
  19. print(count, end='')
  20. for i in range(1000, -1, -1):
  21. if poly[i] != 0:
  22. print(" %d %.1f" % (i, poly[i]), end='')

1002 A+B for Polynomials (25分)的更多相关文章

  1. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  2. PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)

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

  3. PAT 1002 A+B for Polynomials (25分)

    题目 This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: E ...

  4. 【PAT甲级】1002 A+B for Polynomials (25 分)

    题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...

  5. 1002 A+B for Polynomials (25分) 格式错误

    算法笔记上能踩的坑都踩了. #include<iostream> using namespace std; float a[1001];//至少1000个位置 int main(){ in ...

  6. P1002 A+B for Polynomials (25分)

    1002 A+B for Polynomials (25分)   This time, you are supposed to find A+B where A and B are two polyn ...

  7. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  8. PAT甲级 1002 A+B for Polynomials (25)(25 分)

    1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...

  9. 1002 A+B for Polynomials (25)(25 point(s))

    problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...

随机推荐

  1. DRF的封装:APIView类及五大模块

    目录 一.drf框架的封装特点 1.APIView类 二.drf的基础组件 1.请求模块 1.1 请求模块做了什么 1.2 请求request参数 2.解析模块 3.响应模块 4.渲染模块(了解) 5 ...

  2. 微信小程序去除页面滚动条

    ::-webkit-scrollbar { width: 0; height: 0; color: transparent; display: none; } 父级元素(滚动的元素) width:10 ...

  3. POJ-3436(网络流+最大流+输出路径)

    ACM Computer Factory POJ-3436 题目就是一个工厂n个加工机器,每个机器有一个效率w,q个材料入口,q个材料出口,每个口有三个数表示状态,1表示一定有入/出的材料,0表示没有 ...

  4. Web微信协议

    [文档]Web微信协议 1.登录 1.1 获取uuid(get) 请求 https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&red ...

  5. linux安装uwsgi,报错问题解决

    uwsgi安装 uwsgi启动后出 -- unavailable modifier requested: 0 出现问题的的原因是找不到python的解释器(其他语言同理) 你使用的yum instal ...

  6. MyBatis(三):自定义持久层框架实现

    代码已上传至码云:https://gitee.com/rangers-sun/mybatis 新建Maven工程 架构端MyPersistent.使用端MyPersistentTest,使用端引入架构 ...

  7. FreeBSD 中文TTY控制台

    freebsd新型终端VT,支持cjk,所以丢个字体进去,就能显示中文了1,首先你没有改过控制台程序,使用的是默认的,,2,最新版本,本说明是以freebsd12.1release字体格式为.fnt命 ...

  8. (3)MySQL进阶篇SQL优化(索引)

    1.索引问题 索引是数据库优化中最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数 的SQL性能问题.本章节将对MySQL中的索引的分类.存储.使用方法做详细的介绍. 2.索引的存储分类 ...

  9. Mac下安装lightgb并在jupyter中使用

    1.先安装cmake和gcc brew install cmake brew install gcc 2.下载后确定自己的gcc版本 cd /usr/local/opt/gcc/lib/gcc/ 看到 ...

  10. Python基础之:Python中的IO

    目录 简介 linux输入输出 格式化输出 f格式化 format格式化 repr和str %格式化方法 读写文件 文件对象的方法 使用json 简介 IO就是输入和输出,任何一个程序如果和外部希望有 ...