PAT A 급 1002 자바 방법 및 일반적인 오류 분석

7376 단어
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 이 고 뒤 에는 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 입 니 다.
형식 이상 을 되 돌려 주 는 것 은 예 를 들 어 23 일 수 있 습 니 다. 0.001 1 0   리 턴 3, 1, 2, 3. 0 2 4.2
원인 추측:  하면, 만약, 만약... 1 1.2 2 4.2 3 0   그러나 실제 A3 = 0 즉 A3 항 은 계수 가 없 는 것 이 므 로 21, 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 분 의 한 가지 이유 입 니 다.
 
 
 

좋은 웹페이지 즐겨찾기