PAT1002甲级 java

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 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​​<⋯<N​2​​<N​1​​≤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];//存储n
        double[] a2=new double[n1];//存储an
        HashMap hashMap = new HashMap();//存储n 对应的an
        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];//存储n
        double[] b2=new double[n2];//存储an
        for(int i=0;i<n2;i++){
            b1[i]=sc.nextInt();
            b2[i]=sc.nextDouble();
            if(hashMap.get(b1[i])!=null){//n相同
                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];//存储n
        double[] a2=new double[n1];//存储an
        HashMap hashMap = new HashMap();//存储n 对应的an
        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];//存储n
        double[] b2=new double[n2];//存储an
        for(int i=0;i<n2;i++){
            b1[i]=sc.nextInt();
            b2[i]=sc.nextDouble();
            if(hashMap.get(b1[i])!=null){//n相同
                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;
                }
            }
        });
        //进行筛选,把An的绝对值小于0.05的项全部看成0  并相应的减少项数
         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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值