Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
I, entitled
Design of the sum of two functions are univariate polynomial and the product.
Input formats:
Input line 2 minutes, respectively, to each row number of the polynomial given non-zero entries, then descending exponential manner enter a nonzero polynomial coefficient and the exponent (integer not exceeding the absolute value of both 1000). Between numbers separated by a space.
Output formats:
Output in 2 rows, respectively descending exponential manner, and outputs the product polynomial and polynomial coefficients and non-zero entries in the index. Between numbers separated by spaces, but the end can not have extra spaces. Zero polynomial should be output 0 0
.
Sample input:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
Sample output:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
Second, the answer
import java.util.*;
public class Main {
public static void main(String[] args) {
//指数为key,系数为value,存放成map
HashMap<Integer,Integer> map1 = new HashMap<>();
HashMap<Integer,Integer> map2 = new HashMap<>();
Scanner sc = new Scanner(System.in);
int len1 = sc.nextInt();
for (int i = 0; i < len1 ; i++) {
int value = sc.nextInt();
int key = sc.nextInt();
map1.put(key, value);
}
int len2 = sc.nextInt();
for (int i = 0; i < len2 ; i++) {
int value = sc.nextInt();
int key = sc.nextInt();
map2.put(key, value);
}
printMap(multiply(map1,map2));
printMap(add(map1,map2));
}
public static HashMap<Integer,Integer> multiply(HashMap<Integer,Integer>map1, HashMap<Integer,Integer>map2){
HashMap<Integer,Integer> res = new HashMap<>();
for (int key1 : map1.keySet()) {
for (int key2 : map2.keySet()){
int newKey = key1+key2;
int newValue = map1.get(key1) * map2.get(key2);
if (res.containsKey(newKey)){
newValue = res.get(key1+key2) + newValue;
res.put(newKey,newValue);
} else {
res.put(newKey,newValue);
}
}
}
return res;
}
public static HashMap<Integer,Integer> add(HashMap<Integer,Integer>map1, HashMap<Integer,Integer>map2){
for (int key : map2.keySet()){
if (map1.containsKey(key)){
int newValue = map1.get(key) + map2.get(key) ;
map1.put(key,newValue);
} else {
map1.put(key,map2.get(key));
}
}
return map1;
}
public static void printMap(HashMap<Integer,Integer> map){
ArrayList<Integer> keys = new ArrayList<>(map.keySet());
Collections.sort(keys, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1>o2) return -1;
else if (o1<o2) return 1;
else return 0;
}
});
String res = "";
for (int key : keys) {
if (map.get(key)!=0) res = res + map.get(key) + " " + key + " ";
}
if (res.equals("")) res = "0 0";
System.out.println(res.trim());
}
}
- Problem-solving ideas: index key, coefficient value, stored as map
- List can () method to sort directly with the sort Collections of default from small to large, this problem rewrite sorted by descending
- Casual working to see: an all-zero polynomial output is 00
- If only one yuan polynomial sum, consider using a linked list to do