当前位置:网站首页>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());
边栏推荐
- Cause: could't make a guess for solution
- 分布式网络通信框架:本地服务怎么发布成RPC服务
- 汉诺塔II|汉诺塔4柱
- [qualcomm][network] QTI service analysis
- Rocky basic exercise -shell script 2
- PMM (percona monitoring and management) installation record
- Draw arrows with openlayer
- [Qualcomm][Network] qti服务分析
- Basic usage of protobuf
- Redis realizes distributed lock and gets a watchdog
猜你喜欢

Installation and use of cocoapods

Flask framework beginner-03-template

The charm of SQL optimization! From 30248s to 0.001s

Beginner of flask framework-04-flask blueprint and code separation

Principle analysis and source code interpretation of service discovery

Data communication foundation telnet remote management equipment

Review of database -- 3. SQL language

数通基础-TCPIP参考模型

Leetcode 504. Hex number

分布式网络通信框架:本地服务怎么发布成RPC服务
随机推荐
Study notes of the fifth week of sophomore year
【Halcon视觉】编程逻辑
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
Wechat H5 payment on WAP, for non wechat browsers
Using undertow, Nacos offline logout delay after service stop
Tower of Hanoi II | tower of Hanoi 4 columns
PHP one-time request lifecycle
Learning about opencv (1)
The problem of incomplete or partial display of the last recyclerview is solved
SQL Server 2008 R2 installation problems
Show default image when wechat applet image cannot be displayed
Flask框架初学-03-模板
How to write a million reading article
数通基础-网络基础知识
【Halcon视觉】图像的傅里叶变换
【Halcon视觉】阈值分割
Data communication foundation telnet remote management equipment
Applet record
【Halcon视觉】图像滤波
The fourth week of summer vacation