当前位置:网站首页>PTA class a 1002
PTA class a 1002
2022-07-26 10:18:00 【Fabulouskkk】
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 N1aN1 N2aN2 … NKaNK 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 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:
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
The first digit refers to the number of polynomials , Every two numbers form a pair , The first number in a pair is the exponent , The latter is the coefficient, that is 1.5x2, That is to merge the same kind
use Map To store the corresponding index and coefficient
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numberA = sc.nextInt();
Map<Integer, Double> map1 = new HashMap<>();
for (int i = 0; i < numberA; i++) {
Integer key = sc.nextInt();
Double value = sc.nextDouble();
if (map1.containsKey(key)) {
map1.put(key, map1.get(key) + value);
} else {
map1.put(key, value);
}
}
Map<Integer, Double> map2 = new HashMap<>();
int numberB = sc.nextInt();
for (int i = 0; i < numberB; i++) {
Integer key = sc.nextInt();
Double value = sc.nextDouble();
if (map2.containsKey(key)) {
map2.put(key, map2.get(key) + value);
} else {
map2.put(key, value);
}
}
Map<Integer, Double> map3 = new HashMap<>(map2);
map1.forEach((integer, aDouble) -> {
if (map2.containsKey(integer)) {
map3.put(integer, map1.get(integer) + map2.get(integer));
} else {
map3.put(integer, aDouble);
}
});
Iterator<Map.Entry<Integer, Double>> it = map3.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Double> next = it.next();
if (next.getValue() == 0.0) {
it.remove();
}
}
if ((map3.size() <= 1 && map3.containsKey(0))||map3.size() == 0) {
System.out.print("0");
return;
} else {
System.out.print(map3.size() + " ");
List<Map.Entry<Integer, Double>> list = new ArrayList<>(map3.entrySet());
Collections.sort(list, (o1, o2) -> o2.getKey() - o1.getKey());
StringBuilder sb = new StringBuilder();
for (Map.Entry<Integer, Double> maps : list) {
sb.append(maps.getKey()).append(' ').append(BigDecimal.valueOf(maps.getValue()).setScale(1, RoundingMode.HALF_UP)).append(' ');
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
System.out.println(sb);
}
}
}
There are a few disgusting points :
1、 Keep one decimal place
2、 The last digit cannot be a space
3、 The sum of the two sets of coefficients may be zero , No more output
4、 And 3 Similar only 1 And the coefficient of this term is 0 Only output 0
( I tried several broken formats for more than an hour There are always several test points **
To sum up
Learned a new word -> polynomials: polynomial
Traverse delete Map An element in the set needs to use Iterator To traverse the deletion instead of forEach
Iterator<Map.Entry<Integer, Double>> it = map3.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Double> next = it.next();
if (next.getValue() == 0.0) {
it.remove();
}
}
Customize Map The sorting of sets is based on Key still Value It can be used Collections Medium sort
List<Map.Entry<Integer, Double>> list = new ArrayList<>(map3.entrySet());
Collections.sort(list, (o1, o2) -> o2.getKey() - o1.getKey());
边栏推荐
- C language course design Tetris (Part 2)
- SQL优化的魅力!从 30248s 到 0.001s
- Review of database -- 1. Overview
- Distributed network communication framework: how to publish local services into RPC services
- Encapsulation of tabbarcontroller
- Session based recommendations with recurrent neural networks
- Leetcode 504. Hex number
- Reproduce the snake game in C language (I) build pages and construct snakes
- How to use Gmail to pick up / send mail on Foxmail
- 论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
猜你喜欢

Applet record

新建福厦铁路全线贯通 这将给福建沿海带来什么?

MySQL 5.7.25 source code installation record

Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component

Learning about opencv (4)

服务发现原理分析与源码解读

【Halcon视觉】图像的傅里叶变换

Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills

【Halcon视觉】形态学腐蚀

Like, "new programmer" e-book is free for a limited time!
随机推荐
如何写一篇百万阅读量的文章
Study notes of the third week of sophomore year
Show default image when wechat applet image cannot be displayed
Getting started with SQL - combined tables
Installation and use of cocoapods
Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
在.NET 6.0中配置WebHostBuilder
Vs2019 configuring opencv
El table implements adding / deleting rows, and a parameter changes accordingly
Flask框架初学-03-模板
Learning about opencv (3)
数通基础-网络基础知识
The practice of OpenCV -- bank card number recognition
输入整数后输入整行字符串的解决方法
Phpexcel export Emoji symbol error
Wechat applet learning notes 1
C language course design Tetris (Part 2)
RecyclerView最后一条显示不全或显示部分的问题解决
Wu Enda linear regression of machine learning
Network related journals and conferences in CS