PAT甲级 1002 java做法 及常见错误分析

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 N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯

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="[email protected]">sqc
 * @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;io2.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;io2.hashCode()){
                    return -1;
                }else{
                    return 1;
                }
            }
        });
        int length = arrayList.size();
        for(int i=0;i

主要两个错误,一个测试用例失败 一个返回格式异常  大致可以猜想出原因 

测试用例失败可能是因为 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="[email protected]">sqc
  *@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;io2.hashCode()){
                    return -1;
                }else{
                    return 1;
                }
            }
        });
        //做一次筛选 把An的绝对值小于0.05的项全部看成0  并相应的减少项数
        int length = arrayList.size();
        for(int i=0;i0) {
            System.out.print(" ");
        }
        for(int i=0;i

       

另外:某ACM队员一眼看出我可能犯什么错  而且果然 是之前21分的一条原因

 

 

 

 

你可能感兴趣的:(PAT甲级 1002 java做法 及常见错误分析)