2020/7/1
问题
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 N1 aN1 N2 aN2 … NK aNK 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. 第一个数表示多项式中项的个数,第二个表示第几项,第三个数表示项的数值,如1 1 2.4 表示多项式只含一项,第1项为2.4。我们需要将第二个数相同的项进行相加,得出结果。
java代码1.0
import java
.util
.*
;
public class Main{
public static void main(String
[] args
){
Scanner sc
=new Scanner(System
.in
);
int n1
=sc
.nextInt();
int[] a1
=new int[n1
];
double[] a2
=new double[n1
];
HashMap hashMap
= new HashMap();
ArrayList
<Integer> str
=new ArrayList<Integer>();
for(int i
=0;i
<n1
;i
++){
a1
[i
]=sc
.nextInt();
a2
[i
]=sc
.nextDouble();
hashMap
.put(a1
[i
],a2
[i
]);
str
.add(a1
[i
]);
}
int n2
=sc
.nextInt();
int[] b1
=new int[n2
];
double[] b2
=new double[n2
];
for(int i
=0;i
<n2
;i
++){
b1
[i
]=sc
.nextInt();
b2
[i
]=sc
.nextDouble();
if(hashMap
.get(b1
[i
])!=null
){
hashMap
.put(b1
[i
],(double)hashMap
.get(b1
[i
])+b2
[i
]);
}else {
hashMap
.put(b1
[i
],b2
[i
]);
str
.add(b1
[i
]);
}
}
str
.sort(new Comparator() {
@Override
public int compare(Object o1
, Object o2
) {
if(o1
.hashCode()>o2
.hashCode()){
return -1;
}else{
return 1;
}
}
});
System
.out
.print(str
.size()+" ");
for(int i
=0;i
<str
.size()-1;i
++){
System
.out
.print(str
.get(i
)+" "+hashMap
.get(str
.get(i
))+" ");
}
System
.out
.print(str
.get(str
.size()-1)+" "+hashMap
.get(str
.get(str
.size()-1)));
}
}
答案错误原因
需要四舍五入,如
1 1 0.023
1 1 0.027
正常保留一位小数 应该是1 1 0.1 但实际输出结果是0 因为保留小数没有四舍五入
import java
.util
.ArrayList
;
import java
.util
.Comparator
;
import java
.util
.HashMap
;
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
){
Scanner sc
=new Scanner(System
.in
);
int n1
=sc
.nextInt();
int[] a1
=new int[n1
];
double[] a2
=new double[n1
];
HashMap hashMap
= new HashMap();
ArrayList
<Integer> str
=new ArrayList<Integer>();
for(int i
=0;i
<n1
;i
++){
a1
[i
]=sc
.nextInt();
a2
[i
]=sc
.nextDouble();
hashMap
.put(a1
[i
],a2
[i
]);
str
.add(a1
[i
]);
}
int n2
=sc
.nextInt();
int[] b1
=new int[n2
];
double[] b2
=new double[n2
];
for(int i
=0;i
<n2
;i
++){
b1
[i
]=sc
.nextInt();
b2
[i
]=sc
.nextDouble();
if(hashMap
.get(b1
[i
])!=null
){
hashMap
.put(b1
[i
],(double)hashMap
.get(b1
[i
])+b2
[i
]);
}else {
hashMap
.put(b1
[i
],b2
[i
]);
str
.add(b1
[i
]);
}
}
str
.sort(new Comparator() {
@Override
public int compare(Object o1
, Object o2
) {
if(o1
.hashCode()>o2
.hashCode()){
return -1;
}else{
return 1;
}
}
});
int length
=str
.size();
for(int i
=0;i
<length
;i
++){
double result
= (double)hashMap
.get(str
.get(i
));
if(-0.05<result
&& result
<0.05){
hashMap
.remove(str
.get(i
));
str
.remove(i
);
length
--;
i
--;
continue;
}
hashMap
.put(str
.get(i
),hashMap
.get(str
.get(i
)));
}
length
=str
.size();
System
.out
.print(length
);
if(length
>0){
System
.out
.print(" ");
}
for(int i
=0;i
<length
-1;i
++){
System
.out
.print(str
.get(i
)+" "+hashMap
.get(str
.get(i
))+" ");
}
System
.out
.print(str
.get(length
-1)+" "+hashMap
.get(str
.get(length
-1)));
}
}
注:还是存在一个错误答案,日后再找吧
更为详细介绍见 https://blog.csdn.net/qq_31310291/article/details/86497442