1002 A+B for Polynomials (25 分)
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.
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
翻译一下 多项式加法 没有的项注意消掉 前面整数是An 的n 后面代表An 比如 A1是2.4 A0是3.2 第二行 A2是1.5 A1是0.5
import java.util.*;
/**
* @ClassName ${Name}
* @Description TODO
* @Author < a href="jcsong50@best-inc.com">sqc</a>
* @Date 2019/1/15 14:15
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
HashMap hashMap = new HashMap();
HashSet hashSet =new HashSet();
for(int i=0;i<k;i++){
int num = scanner.nextInt();
double a = scanner.nextDouble();
boolean success = Math.abs(a)<0.1&&Math.abs(a)*100%10<5;
if(!success) {
hashMap.put(num, a);
hashSet.add(num);
}
}
int l = scanner.nextInt();
for(int i=0;i<l;i++){
int num = scanner.nextInt();
double a = scanner.nextDouble();
if(hashSet.contains(num)){
double sum = (double)hashMap.get(num)+a;
if(Math.abs(sum)<0.1&& Math.abs(sum)*100%10<5){
hashMap.put(num, 0);
}else {
hashMap.put(num, (double) hashMap.get(num) + a);
}
}else{
boolean success = Math.abs(a)<0.1&&Math.abs(a)*100%10<5;
if(!success){
hashSet.add(num);
hashMap.put(num, a);
}
}
}
List list =new ArrayList(hashSet);
list.sort(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1.hashCode()>o2.hashCode()){
return 1;
}
return -1;
}
});
int length = list.size();
System.out.print(length+" ");
for(int i=length-1;i>0;i--){
double round = (double)hashMap.get(list.get(i))*100;
if(round%10>=5){
round+=10;
}
round = round - round%10;
double mapResult = round/10;
mapResult/=10;
if(mapResult!=0.0) {
System.out.print(list.get(i) + " " + mapResult + " ");
}
}
double round = (double)hashMap.get(list.get(0))*100;
if(round%10>=5){
round+=10;
}
round = round - round%10;
double mapResult = round/10;
mapResult/=10;
if(mapResult!=0.0) {
System.out.print(list.get(0) + " " + mapResult);
}
}
}
第一版本 脑袋里都没有构思好到底要怎么写,三个正确 17分
主要说第二个版本 第二个版本重新写过
import java.util.*;
public class PAT2{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int lineOne = scanner.nextInt();
double[] b1 = new double[lineOne];
int []a1 = new int[lineOne];
HashMap hashMap = new HashMap();
List arrayList = new ArrayList();
for(int i=0;i<lineOne;i++){
a1[i]= scanner.nextInt();
b1[i]= scanner.nextDouble();
hashMap.put(a1[i],b1[i]);
arrayList.add(a1[i]);
}
int lineTwo = scanner.nextInt();
double[] b2 = new double[lineTwo];
int[] a2 = new int[lineTwo];
for(int i=0;i<lineTwo;i++){
a2[i]= scanner.nextInt();
b2[i]= scanner.nextDouble();
if(hashMap.get(a2[i])!=null){
hashMap.put(a2[i],(double)hashMap.get(a2[i])+b2[i]);
}else{
hashMap.put(a2[i],b2[i]);
arrayList.add(a2[i]);
}
}
arrayList.sort(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1.hashCode()>o2.hashCode()){
return -1;
}else{
return 1;
}
}
});
int length = arrayList.size();
for(int i=0;i<length;i++){
double result = (double)hashMap.get(arrayList.get(i));
String stringResult = String.format("%.1f", result);
double result2 =Double.parseDouble(stringResult);
if(result2==0){
hashMap.remove(arrayList.get(i));
arrayList.remove(i);
length--;
i--;
continue;
}
}
length = arrayList.size();
System.out.print(length+" ");
for(int i=0;i<length-1;i++){
System.out.print(arrayList.get(i)+" "+hashMap.get(arrayList.get(i)));
if((double)hashMap.get(arrayList.get(i+1))!=0){
System.out.print(" ");
}
}
System.out.print(arrayList.get(length-1)+" "+hashMap.get(arrayList.get(length-1)));
}
}
主要两个错误,一个测试用例失败 一个返回格式异常 大致可以猜想出原因
测试用例失败可能是因为 1 1 0.023 回车 1 1 0.027 正常保留一位小数 应该是1 1 0.1 但实际输出结果是0 因为保留小数没有四舍五入
返回格式异常可能是因为 举个例子 2 3 0.001 1 0 回车 3 1 1.2 3 0 2 4.2
猜测原因: 如果正常输出应该是 3 1 1.2 2 4.2 3 0 但实际A3=0 也就是A3项是无系数的 应该变成 2 1 1.2 2 4.2 这里 4.2的后面实际上是有一个" "空格的 因为输出前一项时 是不知道后一项是否存在 所以带了空格
实际原因: 进行小数点1位精确操作以后 没有把操作后的值放回map中
特殊情况 上下两行 刚好抵消 项数为0 但是后面多追加了一个" "空格
提示就是 PAT的题如果答案错误是不会提供错误的测试用例的 所以原因需要自己猜测 最好一开始逻辑就很清楚
有空格之类的格式问题 建议测试的时候 代码里的" "用"#"或其他特殊字符代替
满分代码:
import java.util.*;
/**
* 方法实现说明 TODO
*description:1002 A+B for Polynomials (25 分)
* 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:
* where K is the number of nonzero terms in the polynomial
* 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
*@return
*throws
*author < a href="jcsong50@best-inc.com">sqc</a>
*@date 2019/1/15 20:03
*/
public class PAT2{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int lineOne = scanner.nextInt();
double[] b1 = new double[lineOne];
int []a1 = new int[lineOne];
//用来存储An
HashMap hashMap = new HashMap();
//用来存储n
List arrayList = new ArrayList();
//输入第一行
for(int i=0;i<lineOne;i++){
a1[i]= scanner.nextInt();
b1[i]= scanner.nextDouble();
hashMap.put(a1[i],b1[i]);
arrayList.add(a1[i]);
}
int lineTwo = scanner.nextInt();
double[] b2 = new double[lineTwo];
int[] a2 = new int[lineTwo];
//输入的第二行 如果有系数相同 直接合并 2A1 和 0.5A1 直接2.5A1
for(int i=0;i<lineTwo;i++){
a2[i]= scanner.nextInt();
b2[i]= scanner.nextDouble();
if(hashMap.get(a2[i])!=null){
hashMap.put(a2[i],(double)hashMap.get(a2[i])+b2[i]);
}else{
hashMap.put(a2[i],b2[i]);
arrayList.add(a2[i]);
}
}
arrayList.sort(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1.hashCode()>o2.hashCode()){
return -1;
}else{
return 1;
}
}
});
//做一次筛选 把An的绝对值小于0.05的项全部看成0 并相应的减少项数
int length = arrayList.size();
for(int i=0;i<length;i++){
double result = (double)hashMap.get(arrayList.get(i));
String stringResult = String.format("%.1f", result);
double result2 =Double.parseDouble(stringResult);
if(result2==0){
hashMap.remove(arrayList.get(i));
arrayList.remove(i);
length--;
i--;
continue;
}
//这里在hashmap里存放的是 进行过四舍五入和精确小数点计算的值
hashMap.put(arrayList.get(i), result2);
}
length = arrayList.size();
System.out.print(length);
//避免0项报错 格式错误
if(length>0) {
System.out.print(" ");
}
for(int i=0;i<length-1;i++){
System.out.print(arrayList.get(i)+" "+hashMap.get(arrayList.get(i))+" ");
}
System.out.print(arrayList.get(length-1)+" "+hashMap.get(arrayList.get(length-1)));
}
}
另外:某ACM队员一眼看出我可能犯什么错 而且果然 是之前21分的一条原因