zoukankan      html  css  js  c++  java
  • PAT甲级练习题1001、1002

    1001 A+B Format (20 分)

     

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

    Input Specification:

    Each input file contains one test case. Each case contains a pair of integers a and b where −. The numbers are separated by a space.

    Output Specification:

    For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

    Sample Input:

    -1000000 9
    

    Sample Output:

    -999,991


    解题时注意取余时i的位置
    满分代码:
    import java.util.Scanner;
    
    public class Main{
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) {
                int a = in.nextInt();
                int b = in.nextInt();
                int res = a+b;
                String A = String.valueOf(res);
                   if(res<0) {
                       int len = A.length();
                       int c = (len-1)%3;
                       System.out.print('-');
                       for(int i=1;i<=c;i++) {
                           System.out.print(A.charAt(i));
                       }
                       if(c!=0&&len>3) System.out.print(',');
                       
                       for(int i=c+1;i<A.length();i++) {
                           if((i-c-1)!=0&&(i-c-1)%3==0&&i!=A.length()-1) System.out.print(',');
                           System.out.print(A.charAt(i));
                       }
                       System.out.println();
                   }else {
                       int len = A.length();
                       int c = len%3;
                       for(int i=0;i<c;i++) {
                           System.out.print(A.charAt(i));
                       }
                       if(c!=0&&len>3) System.out.print(',');
                       
                       for(int i=c;i<A.length();i++) {
                           if((i-c)!=0&&(i-c)%3==0&&i!=A.length()-1) System.out.print(',');
                           System.out.print(A.charAt(i));
                       }
                       System.out.println();
                   }
            }
            in.close();
        }
    }

    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:

    N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​

    where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (,) are the exponents and coefficients, respectively. It is given that 1,0.

    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

    注意为0项需要溢出map表
    满分代码:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    public class Main{
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) {
                int keyMax = 0;
                int up = in.nextInt();
                Map<Integer, Double> hm = new HashMap<>();
                for (int i = 0; i < up; i++) {
                    int x = in.nextInt();
                    if (x > keyMax)
                        keyMax = x;
                    hm.put(x, in.nextDouble());
                }
                int down = in.nextInt();
                for (int i = 0; i < down; i++) {
                    int x = in.nextInt();
                    if (x > keyMax)
                        keyMax = x;
    
                    if (!hm.containsKey(x)) {
                        hm.put(x, in.nextDouble());
                    } else {
                        double y = hm.get(x);
                        y += in.nextDouble();
                        if(y!=0)
                        hm.put(x, y);
                        else hm.remove(x);
                    }
                }
                if(hm.size()==0) System.out.print("0");
                else
                    System.out.print(hm.size() + " ");
                int sign = 0;
                for (int i = keyMax; i >= 0; i--) {
                    if (hm.containsKey(i)) {
                        sign++;
                            
                        System.out.print(i + " ");
                        if (sign != hm.size())
                            System.out.print(String.format("%.1f", hm.get(i)) + " ");
                        else
                            System.out.print(String.format("%.1f", hm.get(i)));
                    }
                }
            }
        }
    }
     
  • 相关阅读:
    mac 系统下删除目录的所有.svn文件
    java DES加密解密文件
    也许,未来需要重新规划
    android选择图片或拍照图片上传到服务器(包括上传参数)
    iOS DES ECB模式对称加密解密
    iOS开发中防止键盘挡住UITextField解决方案
    xCode 4.X 免证书真机公布及调试
    iOS 获取手机的型号,系统版本,软件名称,软件版本
    java DES ECB模式对称加密解密
    解决error: failed to launch"/private/var/mobile/Applications/XX" timed out waiting for app to launch
  • 原文地址:https://www.cnblogs.com/godoforange/p/10895063.html
Copyright © 2011-2022 走看看