1. 程式人生 > >牛客網PAT乙級真題及java實現樣例(真題1-14)

牛客網PAT乙級真題及java實現樣例(真題1-14)

由於之前在PAT官網的練習場有時間限制,而且限制是針對c的基本上100ms到400ms,由於java需要虛擬機器才能執行,因此有些題實在不是因為演算法的原因才超時,牛客網的時間限制為1s,而且給我感覺後臺伺服器也比PAT官網跑的快,一樣的程式PAT官網要80ms,牛客網只要20ms,因此我現在把程式碼提交到了牛客網。下面是牛客網乙級真題的題目和我實現的方法。

1001.A+B和C

題目描述
給定區間[-2的31次方, 2的31次方]內的3個整數A、B和C,請判斷A+B是否大於C。

輸入描述:
輸入第1行給出正整數T(<=10),是測試用例的個數。隨後給出T組測試用例,每組佔一行,順序給出A、B和C。整數間以空格分隔。

輸出描述:
對每組測試用例,在一行中輸出“Case #X: true”如果A+B>C,否則輸出“Case #X: false”,其中X是測試用例的編號(從1開始)。

輸入例子:
4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647
輸出例子:
Case #1: false
Case #2: true
Case #3: true
Case #4: false

注:這個題目沒什麼難度,就是考慮整數會超過int長度,可以選擇Long型別。程式碼如下所示:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        //System.out.print(Integer.SIZE);
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        sc.nextLine();
        List<String> list = new ArrayList();
        for(int i=0; i<T; i++){
            list.add(sc.nextLine());
        }
        for(int i=0; i<T; i++){
            String[] strarr = list.get(i).split(" ");
            long A = Long.parseLong(strarr[0]);
            long B = Long.parseLong(strarr[1]);
            long C = Long.parseLong(strarr[2]);
            //System.out.print(A+" "+B+" "+C+" ");
            if(A+B > C)
                System.out.println("Case #"+(i+1)+": true");
            else
                System.out.println("Case #"+(i+1)+": false");
        }
    }
}
1002.數字分類

題目描述
給定一系列正整數,請按要求對數字進行分類,並輸出以下5個數字:
A1 = 能被5整除的數字中所有偶數的和;
A2 = 將被5除後餘1的數字按給出順序進行交錯求和,即計算n1-n2+n3-n4...;
A3 = 被5除後餘2的數字的個數;
A4 = 被5除後餘3的數字的平均數,精確到小數點後1位;
A5 = 被5除後餘4的數字中最大數字。


輸入描述:
每個輸入包含1個測試用例。每個測試用例先給出一個不超過1000的正整數N,隨後給出N個不超過1000的待分類的正整數。數字間以空格分隔。


輸出描述:
對給定的N個正整數,按題目要求計算A1~A5並在一行中順序輸出。數字間以空格分隔,但行末不得有多餘空格。
若其中某一類數字不存在,則在相應位置輸出“N”。


輸入例子:
13 1 2 3 4 5 6 7 8 9 10 20 16 18


輸出例子:
30 11 2 9.7 9

注:這個題目也沒什麼難度就是計算的時候可以考慮一些重複計算的東西,儘量程式中不要出現重複計算的程式碼。實現程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Scanner;

/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A1 = 0;
        int A2 = 0;
        int flag = 1;
        int numA2 = 0;
        int A3 = 0;
        double A4 = 0;
        int numA4 = 0;
        //int[] arrA5 = new int[1000];
        int A5 = 0;
        
        int index = 0;
        int num;
        boolean flagA5 = false;
        for(int i=0; i<N; i++){
            num = sc.nextInt();
            if(num%10 == 0)
                A1 += num;
            else if(num%5 ==1){
                A2 = flag*num + A2;
                flag *= -1;
                numA2++;
            }
            else if(num%5 == 2 )
                 A3++;
            else if(num%5 == 3 ){
                 numA4++;
                 A4 += num;
            }
            else if(num%5 == 4 )
                 if(!flagA5){
                        A5 = num;
                        flagA5 = true;
                    }
                    else
                        if(num >= A5)
                            A5 = num;    
            }
        
        if(A1>0)
            System.out.print(A1);
        else
            System.out.print("N");
        if(numA2>0)
            System.out.print(" "+A2);
        else
            System.out.print(" N");
        if(A3>0)
            System.out.print(" "+A3);
        else
            System.out.print(" N");
        if(numA4 >0){
            A4 = A4/numA4;
            System.out.printf(" %.1f",A4);
        }
        else
            System.out.print(" N");
        if(flagA5){
            System.out.print(" "+A5);
        }
        else
            System.out.print(" N");  
    }
}
1003.數素數

題目描述
令Pi表示第i個素數。現任給兩個正整數M <= N <= 10000,請輸出PM到PN的所有素數。


輸入描述:
輸入在一行中給出M和N,其間以空格分隔。


輸出描述:
輸出從PM到PN的所有素數,每10個數字佔1行,其間以空格分隔,但行末不得有多餘空格。


輸入例子:
5 27


輸出例子:
11 13 17 19 23 29 31 37 41 43


47 53 59 61 67 71 73 79 83 89


97 101 103
注:這個題目需要注意的地方是在判斷素數的時候只要從2開始到sqrt(m)。如果從2到m-1的話,就可能造成超時。程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int M = sc.nextInt();
        int N = sc.nextInt();
        int i=1;
        int numPrime = 0;
        int numPrint = 0;
        while(true){
           /* if(i>2)
                i=i+2;
            else*/
                i++;
            if(isPrime(i)){
                numPrime++;
                if(numPrime>=M && numPrime<=N){
                    numPrint ++;
                    if(numPrint%10 == 1)
                        System.out.print(i);
                    else if(numPrint%10 == 0)
                        System.out.println(" "+i);
                    else
                        System.out.print(" "+i);
                }
            }
            if(numPrime > N)
                break;
                
        }
    }
    public static boolean isPrime(int num){
        if(num <2)
            return false;
        if(num ==2)
            return true;
        for(int i=2; i<Math.sqrt(num)+1; i++){
            if(num%i == 0)
                return false;
        }
        return true;
    }
}
1004.福爾摩斯的約會

題目描述
大偵探福爾摩斯接到一張奇怪的字條:“我們約會吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大偵探很
快就明白了,字條上奇怪的亂碼實際上就是約會的時間“星期四 14:04”,因為前面兩字串中第1對相同的大寫英文字母(大小寫有區分)是
第4個字母'D',代表星期四;第2對相同的字元是'E',那是第5個英文字母,代表一天裡的第14個鐘頭(於是一天的0點到23點由數字0到9、
以及大寫字母A到N表示);後面兩字串第1對相同的英文字母's'出現在第4個位置(從0開始計數)上,代表第4分鐘。現給定兩對字串,
請幫助福爾摩斯解碼得到約會的時間。


輸入描述:
輸入在4行中分別給出4個非空、不包含空格、且長度不超過60的字串。


輸出描述:
在一行中輸出約會的時間,格式為“DAY HH:MM”,其中“DAY”是某星期的3字元縮寫,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期
四,FRI表示星期五,SAT表示星期六,SUN表示星期日。題目輸入保證每個測試存在唯一解。


輸入例子:
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm


輸出例子:
THU 14:04

注:這個題目需要注意的是當小時和分鐘小於10的時候要在前面輸出0。程式碼如下所示:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String agrs[]){
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
        String str3 = sc.nextLine();
        String str4 = sc.nextLine();
        char char1 = '0';
        char char2 = '0';
        int charnum = 0;
        for(int i=0; (i<Math.min(str1.length(), str2.length())&&charnum<2); i++){
            char ch = str1.charAt(i);
            if(charnum == 0 && ch==str2.charAt(i) && ch>='A'&&ch<='G'){
                char1 = ch;
                charnum++;
                continue;
            }
            if(charnum == 1&&(ch==str2.charAt(i))
                &&(ch>='A'&&ch<='N'||ch>='0'&&ch<='9')){
                char2 = ch;
                charnum++;
                continue;
            }
        }
        int index = 0;
        for(int i=0; i<Math.min(str3.length(), str4.length());i++){
            int ch = str3.charAt(i);
            if(ch == str4.charAt(i)&&(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')){
                index = i;
                break;
            }
        }
        String out = new String();
        switch(char1){
            case 'A':
                out += "MON ";
                break;
            case 'B':
                out += "TUE ";
                break;
            case 'C':
                out += "WED ";
                break;
            case 'D':
                out += "THU ";
                break;
            case 'E':
                out += "FRI ";
                break;
            case 'F':
                out += "SAT ";
                break;
            case 'G':
                out += "SUN ";
                break;
            default:
                break;
        }
        if(char2<='9')
            out += "0"+char2;
        else
            out += char2 - 'A' +10;
        if(index<10)
            out +=  ":0"+index;
        else
            out += ":"+index;
        System.out.println(out);
    }
}

1005.德才論

題目描述
宋代史學家司馬光在《資治通鑑》中有一段著名的“德才論”:“是故才德全盡謂之聖人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之
小人。凡取人之術,苟不得聖人,君子而與之,與其得小人,不若得愚人。”
現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。


輸入描述:
輸入第1行給出3個正整數,分別為:N(<=105),即考生總數;L(>=60),為錄取最低分數線,即德分和才分均不低於L的考生才有資格
被考慮錄取;H(<100),為優先錄取線——德分和才分均不低於此線的被定義為“才德全盡”,此類考生按德才總分從高到低排序;才分不到
但德分到線的一類考生屬於“德勝才”,也按總分排序,但排在第一類考生之後;德才分均低於H,但是德分不低於才分的考生屬於“才德兼
亡”但尚有“德勝才”者,按總分排序,但排在第二類考生之後;其他達到最低線L的考生也按總分排序,但排在第三類考生之後。
隨後N行,每行給出一位考生的資訊,包括:准考證號、德分、才分,其中准考證號為8位整數,德才分為區間[0, 100]內的整數。數字間以空格分隔。


輸出描述:
輸出第1行首先給出達到最低分數線的考生人數M,隨後M行,每行按照輸入格式輸出一位考生的資訊,考生按輸入中說明的規則從高到低排序。當某類考生中有多人
總分相同時,按其德分降序排列;若德分也並列,則按准考證號的升序輸出。


輸入例子:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60


輸出例子:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

注:這個題目關鍵是排序,排序的時候自己實現comparator介面需要使用內部類。程式碼如下

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
/**
 *
 * @author zjb
 */
public class Main{
    public static class CMP implements Comparator<String>{
        public int compare(String str1,String str2){
        String[] strarr1 = str1.split(" ");
        String[] strarr2 = str2.split(" ");
        if(Integer.parseInt(strarr1[1])+Integer.parseInt(strarr1[2]) > 
                Integer.parseInt(strarr2[1])+Integer.parseInt(strarr2[2]))
            return -1;
        else if(Integer.parseInt(strarr1[1])+Integer.parseInt(strarr1[2]) < 
                Integer.parseInt(strarr2[1])+Integer.parseInt(strarr2[2]))
            return 1;
        else {
            if(Integer.parseInt(strarr1[1]) > Integer.parseInt(strarr2[1]))
                return -1;
            else if(Integer.parseInt(strarr1[1]) < Integer.parseInt(strarr2[1]))
                return 1;
            else{
                if(Integer.parseInt(strarr1[0]) > Integer.parseInt(strarr2[0]))
                    return 1;
                else
                    return -1;
            }
        }
    }
    }
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int L = sc.nextInt();
        int H = sc.nextInt();
        //List<String> list = new ArrayList();
        List<String> caideqj = new ArrayList();
        List<String> deshengc = new ArrayList();
        List<String> caidejw = new ArrayList();
        List<String> other = new ArrayList();
        sc.nextLine();
        for(int i=0; i<N; i++){
            String str  = sc.nextLine();
            String[] strarr = str.split(" ");
            if(Integer.parseInt(strarr[1])>=L && Integer.parseInt(strarr[2])>=L){
                if(Integer.parseInt(strarr[1])>=H && Integer.parseInt(strarr[2])>=H){
                    caideqj.add(str);                    
                }
                else if(Integer.parseInt(strarr[1])>=H && Integer.parseInt(strarr[2])<H){
                    deshengc.add(str);   
                }
                else if(Integer.parseInt(strarr[1])<H && Integer.parseInt(strarr[2])<H 
                        && Integer.parseInt(strarr[1])>=Integer.parseInt(strarr[2])){
                    caidejw.add(str);
                }
                else{
                    other.add(str);
                }
            }
        }
        System.out.println(caideqj.size()+deshengc.size()+caidejw.size()+other.size());
        CMP cmp = new CMP();
        if(caideqj.size()>1)
            Collections.sort(caideqj, cmp);
        if(deshengc.size()>1)
            Collections.sort(deshengc, cmp);
        if(caidejw.size()>1)
            Collections.sort(caidejw, cmp);
        if(other.size()>1)
            Collections.sort(other, cmp);
        for(int i=0; i<caideqj.size(); i++){
            System.out.println(caideqj.get(i));
        }
         for(int i=0; i<deshengc.size(); i++){
            System.out.println(deshengc.get(i));
        }
          for(int i=0; i<caidejw.size(); i++){
            System.out.println(caidejw.get(i));
        }
           for(int i=0; i<other.size(); i++){
            System.out.println(other.get(i));
        }
  
    }
}

1006.部分A+B

題目描述
正整數A的“DA(為1位整數)部分”定義為由A中所有DA組成的新整數PA。例如:給定A = 3862767,DA = 6,則A的“6部分”PA是66,因為A中有2個6。
現給定A、DA、B、DB,請編寫程式計算PA + PB。


輸入描述:
輸入在一行中依次給出A、DA、B、DB,中間以空格分隔,其中0 < A, B < 1010。


輸出描述:
在一行中輸出PA + PB的值。


輸入例子:
3862767 6 13530293 3


輸出例子:
399

注:這個題目很簡單沒有什麼需要注意的點,程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int DA = sc.nextInt();
        int B = sc.nextInt();
        int DB = sc.nextInt();
        int PA = 0;
        int PB = 0;
        while(A>0){
            int num = A%10;
            if(num == DA)
                PA = PA*10 + DA;
            A = A/10;
        }
        while(B>0){
            int num = B%10;
            if(num == DB)
                PB = PB*10 + DB;
            B = B/10;
        }
        System.out.println(PA+PB);
        
    }
}
1007.A除以B

題目描述
本題要求計算A/B,其中A是不超過1000位的正整數,B是1位正整數。你需要輸出商數Q和餘數R,使得A = B * Q + R成立。


輸入描述:
輸入在1行中依次給出A和B,中間以1空格分隔。


輸出描述:
在1行中依次輸出Q和R,中間以1空格分隔。


輸入例子:
123456789050987654321 7


輸出例子:
17636684150141093474 3

注:由於題目要求的是不超過1000位的正整數,因此利用資料型別接收輸入肯定是不行的,這個時候需要利用整形陣列接收輸入,然後利用除法的基本原理求解,程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        List<Integer> list = new ArrayList();
        Scanner sc = new Scanner(System.in);
        String strA = sc.next();
        int B = sc.nextInt();
        int temp = 0;
        for(int i=0; i<strA.length(); i++){
            temp = temp*10 + strA.charAt(i)-'0';
            if(temp < B)
                list.add(0);
            else{
                list.add(temp/B);
                temp = temp % B;
            }
            
        }
        for(int i=0; i<list.size(); i++){
            if(i==0 && list.get(i)==0)
                ;
            else
                System.out.print(list.get(i)); 
        }
         System.out.println(" "+temp); 
    }
}
1008.錘子剪刀布

題目描述
大家應該都會玩“錘子剪刀布”的遊戲:
現給出兩人的交鋒記錄,請統計雙方的勝、平、負次數,並且給出雙方分別出什麼手勢的勝算最大。


輸入描述:
輸入第1行給出正整數N(<=105),即雙方交鋒的次數。隨後N行,每行給出一次交鋒的資訊,即甲、乙雙方同時給出的的手勢。C代表“錘子”、J代表“剪刀”、B代
表“布”,第1個字母代表甲方,第2個代表乙方,中間有1個空格。


輸出描述:
輸出第1、2行分別給出甲、乙的勝、平、負次數,數字間以1個空格分隔。第3行給出兩個字母,分別代表甲、乙獲勝次數最多的手勢,中間有1個空格。如果解不唯
一,則輸出按字母序最小的解。


輸入例子:
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J


輸出例子:
5 3 2
2 3 5
B B

注:這個題目需要注意的是三者之間的關係,還有甲乙的勝負之間的關係,程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Scanner;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.nextLine();
        int winCA = 0;
        int winCB = 0;
        int winJA = 0;
        int winJB = 0;
        int winBA = 0;
        int winBB = 0;
       // System.out.println(N);
        for(int i=0; i<N; i++){
            String str = sc.nextLine();
            //System.out.println(str);
            //String strB = sc.nextLine();
            char chA = str.charAt(0);
            char chB = str.charAt(2);
            if(chA == 'C'){
                if(chB == 'J')
                    winCA++;
                else if(chB == 'B')
                    winBB++;
            }
            else if(chA == 'J'){
                if(chB == 'B')
                    winJA++;
                else if(chB == 'C')
                    winCB++;
            }
            else if(chA == 'B'){
                if(chB == 'C')
                    winBA++;
                else if(chB == 'J')
                    winJB++;
            }
        }
        int totalwinA = winCA+winJA+winBA;
        int totalwinB = winCB+winJB+winBB;
        int totalping = N-(winCA+winJA+winBA+winCB+winJB+winBB);
        System.out.println(totalwinA+" "+totalping+" "+totalwinB);
        System.out.println(totalwinB+" "+totalping+" "+totalwinA);
        System.out.println(findmax(winCA,winJA,winBA)+" "+findmax(winCB,winJB,winBB));
    }
    public static char findmax(int winC, int winJ, int winB){
        char max = 'B';
        int maxnum = winB;
        if(maxnum < winC){
            max = 'C';
            maxnum = winC;
        }
        if(maxnum < winJ){
            max = 'J';
            maxnum = winJ;
        }
            
        return max;
    }
}
1009.數字黑洞
題目描述
給定任一個各位數字不完全相同的4位正整數,如果我們先把4個數字按非遞增排序,再按非遞減排序,然後用第1個數字減第2個數字,將得到
一個新的數字。一直重複這樣做,我們很快會停在有“數字黑洞”之稱的6174,這個神奇的數字也叫Kaprekar常數。


例如,我們從6767開始,將得到
7766 - 6677 = 1089


9810 - 0189 = 9621


9621 - 1269 = 8352


8532 - 2358 = 6174


7641 - 1467 = 6174


... ...
現給定任意4位正整數,請編寫程式演示到達黑洞的過程。


輸入描述:
輸入給出一個(0, 10000)區間內的正整數N。


輸出描述:
如果N的4位數字全相等,則在一行內輸出“N - N = 0000”;否則將計算的每一步在一行內輸出,直到6174作為差出現,輸出格式見樣例。注意每個數字按4位數格


式輸出。


輸入例子:
6767


輸出例子:
7766 - 6677 = 1089


9810 - 0189 = 9621


9621 - 1269 = 8352


8532 - 2358 = 6174
注:這個題目需要注意的是當輸入不足4位的時候需要用0在前面補齊。程式碼如下:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
 *
 * @author zjb
 */
public class Main {
     public static class CMP implements Comparator<Integer>{
        @Override
        public int compare(Integer o1, Integer o2) {
            if(o1 > o2)
                return -1;
            else if(o1 < o2)
                return 1;
            else
                return 0;
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        Integer a[] = new Integer[4];
        if(str.length() == 1){
            a[0] = a[1] = a[2] = 0; 
            a[3] = str.charAt(0) - '0';
        }
        else if(str.length() == 2){
            a[0] = a[1] = 0;
            a[2] = str.charAt(0) - '0';
            a[3] = str.charAt(1) - '0';
        }
        else if(str.length() == 3){
            a[0] = 0;
            a[1] = str.charAt(0) - '0';
            a[2] = str.charAt(1) - '0';
            a[3] = str.charAt(2) - '0';
        }
            
       /* else {
            if(str.charAt(0) == str.charAt(1) && str.charAt(1)== str.charAt(2) 
                && str.charAt(2) == str.charAt(3))
            System.out.println(str+" - "+str+" = 0000");*/
        else{
            for(int i =0; i<4; i++){
                a[i] = str.charAt(i) - '0';
            }
        }
        if(a[0] == a[1] && a[2] == a[1] && a[3] == a[2]){
            String out = ""+a[0]+a[1]+a[2]+a[3];
            System.out.println(out+" - "+out+" = 0000");
        }
        else{
            int num1 = 0;
            int num2 = 0;
            int differ = 0;
            CMP cmp = new CMP();
           // Integer[] temp = new Integer[4];
            do{
            Arrays.sort(a,cmp);
            num1 = 1000*a[0] + 100*a[1] + 10*a[2] + a[3];
            for(int i=0; i<4; i++){
                System.out.print(a[i]);
            }
            System.out.print(" - ");
            Arrays.sort(a);
            num2 = 1000*a[0] + 100*a[1] + 10*a[2] + a[3];
            for(int i=0; i<4; i++){
                System.out.print(a[i]);
            }
            differ = num1 - num2;
            System.out.println(" = "+differ);
            a[0] = differ/1000;
            differ = differ%1000;
            a[1] = differ/100;
            differ = differ%100;
            a[2] = differ/10;
            differ = differ%10;
            a[3] = differ;
           // Collections.sort(a);
            }while((num1 - num2) != 6174);
        
        }  
        
    }
}

1010.月餅

題目描述
月餅是中國人在中秋佳節時吃的一種傳統食品,不同地區有許多不同風味的月餅。現給定所有種類月餅的庫存量、總售價、以及市場的最大需
求量,請你計算可以獲得的最大收益是多少。


注意:銷售時允許取出一部分庫存。樣例給出的情形是這樣的:假如我們有3種月餅,其庫存量分別為18、15、10萬噸,總售價分別為75、
72、45億元。如果市場的最大需求量只有20萬噸,那麼我們最大收益策略應該是賣出全部15萬噸第2種月餅、以及5萬噸第3種月餅,獲得
72 + 45/2 = 94.5(億元)。


輸入描述:
每個輸入包含1個測試用例。每個測試用例先給出一個不超過1000的正整數N表示月餅的種類數、以及不超過500(以萬噸為單位)的正整數
D表示市場最大需求量。隨後一行給出N個正數表示每種月餅的庫存量(以萬噸為單位);最後一行給出N個正數表示每種月餅的總售價(以億
元為單位)。數字間以空格分隔。


輸出描述:
對每組測試用例,在一行中輸出最大收益,以億元為單位並精確到小數點後2位。
輸入例子:
3 20

18 15 10

75 72 45


輸出例子:
94.50

注:這個題目其實很簡單,主要意思就是單價,單價高的賣出的自然越多越好,明白這點題目就很簡單了。程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pat.basiclevel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author zjb
 */
public class B1020 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        List<yuebing> list = new ArrayList();
        int N = sc.nextInt();
        int D = sc.nextInt();
        sc.nextLine();
        double sum = 0;
        String[] arrsize = sc.nextLine().split(" ");
        String[] arrtotal = sc.nextLine().split(" ");
        for(int i=0; i<N; i++){
            yuebing yb = new yuebing();
            yb.size = Integer.parseInt(arrsize[i]);
            yb.totalprice = Integer.parseInt(arrtotal[i]);
            yb.price = yb.totalprice*1.0/yb.size;
            list.add(yb);
        }
        if(N == 1){
            if(list.get(0).size <= D)
                System.out.printf("%.2f\n",(double)list.get(0).totalprice);
            else
                System.out.printf("%.2f\n", (double)(list.get(0).price*D));
        }
        else{
        CMP cmp = new CMP();
        Collections.sort(list, cmp);
        for(int i=0; i<N&&D>0; i++){
                if(list.get(i).size <= D){
                    sum += list.get(i).totalprice;
                    D = D - list.get(i).size;
                }
                else{
                    sum += list.get(i).price*D;
                    D = 0;
                }
        }
        System.out.printf("%.2f\n",sum);
        }
    }
    private static class yuebing{
        public int size;
        public int totalprice;
        public double price;
    }

    public static class CMP implements Comparator<yuebing>{
        @Override
        public int compare(yuebing yb1, yuebing yb2){
            if(yb1.price > yb2.price)
                return -1;
            else if(yb1.price < yb2.price)
                return 1;
            else
                return 0;
        }
    }
    
}

1011. 個位數統計

題目描述
給定一個k位整數N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),請編寫程式統計每種不同的個位數字出現的次數。例如:給定N = 100311,則有2個0,3個1,和1個3。

輸入描述:
每個輸入包含1個測試用例,即一個不超過1000位的正整數N。

輸出描述:
對N中每一種不同的個位數字,以D:M的格式在一行中輸出該位數字D及其在N中出現的次數M。要求按D的升序輸出。

輸入例子:
100311

輸出例子:
0:2
1:3
3:1

注:這個題目其實很簡單,我這裡使用了String,所以在執行效率上可能存在點問題,會有點低,如果利用整數除10分解效率還會更高,程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Scanner;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int[] num = new int[10];
        for(int i=0; i<str.length();i++){
            char ch = str.charAt(i);
            switch(ch){
                case '0': num[0]++;
                    break;
                case '1': num[1]++;
                    break;
                case '2': num[2]++;
                    break;
                case '3': num[3]++;
                    break;
                case '4': num[4]++;
                    break;
                case '5': num[5]++;
                    break;
                case '6': num[6]++;
                    break;
                case '7': num[7]++;
                    break;
                case '8': num[8]++;
                    break;
                case '9': num[9]++;
                    break;
            }
        }
        for(int i=0; i<10; i++){
            if(num[i] > 0)
                System.out.println(i+":"+num[i]);
        }
    }
}

1012.D進位制的A+B

題目描述
輸入兩個非負10進位制整數A和B(<=230-1),輸出A+B的D (1 < D <= 10)進位制數。

輸入描述:
輸入在一行中依次給出3個整數A、B和D。

輸出描述:
輸出A+B的D進位制數。

輸入例子:
123 456 8

輸出例子:
1103

注:這個題目只要按照進位制轉換的計算過程壓入棧就好了。程式碼如下

import java.util.Scanner;
import java.util.Stack;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int D = sc.nextInt();
        Stack<Integer> mystack = new Stack();
        int num = A + B;
        while(num>0){
            mystack.push(num%D);
            //System.out.println(num%D);
            num = num/D;
        }
        if(mystack.empty())
            System.out.println(0);
        else{
           // System.out.println(mystack.size());
            while(!mystack.empty())
                System.out.print(mystack.pop());
        }
    }
}
1013.組個最小數

題目描述
給定數字0-9各若干個。你可以以任意順序排列這些數字,但必須全部使用。目標是使得最後得到的數儘可能小(注意0不能做首位)。例如:
給定兩個0,兩個1,三個5,一個8,我們得到的最小的數就是10015558。

現給定數字,請編寫程式輸出能夠組成的最小的數。
輸入描述:
每個輸入包含1個測試用例。每個測試用例在一行中給出10個非負整數,順序表示我們擁有數字0、數字1、……數字9的個數。整數間用一個空
格分隔。10個數字的總個數不超過50,且至少擁有1個非0的數字。

輸出描述:
在一行中輸出能夠組成的最小的數。

輸入例子:
2 2 0 0 0 3 0 0 1 0

輸出例子:
10015558
注:利用一個10個元素的整形陣列表示每個數字的個數,然後第一位肯定需要非零的數,之後就按照從小到大輸出就好了。程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.util.Scanner;

/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String out = new String();
        int[] num = new int[10];
        for(int i=0; i<10; i++){
            num[i] = sc.nextInt();
        }
        for(int i=1; i<10; i++){
            if(num[i] > 0){
                out += i;
                num[i]--;
                break;
            }
        }
        for(int i=0; i<10; i++){
            while(num[i] > 0){
                out += i;
                num[i]--;
            }
        }
        System.out.println(out);
        
    }
}

1014.科學計數法

題目描述
科學計數法是科學家用來表示很大或很小的數字的一種方便的方法,其滿足正則表示式[+-][1-9]"."[0-9]+E[+-][0-9]+,即數字的整數部分
只有1位,小數部分至少有1位,該數字及其指數部分的正負號即使對正數也必定明確給出。

現以科學計數法的格式給出實數A,請編寫程式按普通數字表示法輸出A,並保證所有有效位都被保留。

輸入描述:
每個輸入包含1個測試用例,即一個以科學計數法表示的實數A。該數字的儲存長度不超過9999位元組,且其指數的絕對值不超過9999。
輸出描述:
對每個測試用例,在一行中按普通數字表示法輸出A,並保證所有有效位都被保留,包括末尾的0。

輸入例子:
+1.23400E-03

輸出例子:
0.00123400

注:本題主要分三類處理1、在有效數字前面添零,2、在有效數字後面添零,3、只需要移動小數點,不需要添零。程式碼如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.Scanner;
/**
 *
 * @author zjb
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String strnum = new String();
        int num = 0;
        int e =0;
        //String stre = new String();
        String  out = new String();
        int index = str.indexOf('E');
        String flag = str.substring(0,1);
        if(flag.equals("-")){
            strnum = str.substring(1,2)+str.substring(3, index);
        }
        else{
            strnum = str.substring(1,2)+str.substring(3, index);
            flag = "";
        }
        num = Integer.parseInt(strnum);
       // System.out.println(num);
        e = Integer.parseInt(str.substring(index+1));
        //System.out.println(e);
        //int e = Integer.parseInt(stre);
        if(e<0){
            out += "0.";
            for(int i=0; i<(-e)-1; i++){
                out += "0";
            }
            out += num;
        }
        else if(e<strnum.length()-3){
            out += strnum.substring(1, 2+e);
            out +=".";
            out += strnum.substring(2+e);
        }
        else{
            out += num;
            for(int i=0; i<e+1-strnum.length();i++)
                out +="0";
        }
        System.out.println(flag+out);
        //System.out.println(e);
        //System.out.println(Double.parseDouble(strnum)*e);
    }
}



相關推薦

PAT乙級java實現1-14

由於之前在PAT官網的練習場有時間限制,而且限制是針對c的基本上100ms到400ms,由於java需要虛擬機器才能執行,因此有些題實在不是因為演算法的原因才超時,牛客網的時間限制為1s,而且給我感覺後臺伺服器也比PAT官網跑的快,一樣的程式PAT官網要80ms,牛客網只要

PAT乙級練習題 1001採花生

連結:https://www.nowcoder.com/questionTerminal/83740e9b96074dd89edaf9bfad43cac3 來源:牛客網 魯賓遜先生有一隻寵物猴,名叫多多。這天,他們兩個正沿著鄉間小路散步,突然發現路邊的告示牌上貼著一張小小的

PAT 算法歷年 1003: 數素數 (20)

長度 ear code span 數字 print pan += and 1003:數素數 (20) 時間限制 1000 ms 內存限制 32768 KB 代碼長度限制 100 KB 判斷程序 Standard (來自 小小) 題目描述 令Pi表示第i個素數。現任給

浙大pat | 浙大pat PAT頂級(Top Level)練習題 1003 博弈論

You class are planning for a spring outing. N people are votingfor adestination out of K candidate places. The voting progress isbelow: Fi

浙大pat | 浙大pat PAT頂級(Top Level)練習題 1001

題目描述A string s is LUCKY if and only if the number of differentcharacters in s is a fibonacci number. Given a string consisting of only low

多校訓練第三場 C - Shuffle CardsSplay / rope

str spl shuf 鏈接 contest www size targe strong 鏈接: https://www.nowcoder.com/acm/contest/141/C 題意: 牛客網多校訓練第三場 C - Shuffle Cards(Splay /

提高組模擬賽第七場 T3 洞穴附bitset介紹

main \n std 個數 fin 輸出 1的個數 define 聲明 就是DP。 我們可以很簡單的想到要枚舉中間點,進行邊數的轉移。 但是因為邊長數據範圍很大,所以我們考慮log的倍增。 狀態設計為\(dp[i][j][k]\),為從節點\(i\)走\(2^k\)

PAT乙級試題整理——20分整理

牛客網上 共有真題六套,其中每套題有15分題一道,20分題目3道,25分題目1道,共計100分。考試時要求考生在180分鐘內完成,依照陳越姥姥的說法,要在30分鐘內拿下乙級20分題目,所以我們這篇主要想辦法怎麼儘量縮短自己的做題時間。我之前只學過Java和C#,沒有學過C語言,粗

PAT乙級試題整理——15分整理

牛客網上 共有真題六套,其中每套題有15分題一道,20分題目3道,25分題目1道,共計100分。考試時要求考生在180分鐘內完成。我之前只學過Java和C#,沒有學過C語言,粗淺學習了一下C語言基本語法,想借刷題這個機會好好體會一下面向過程的設計語言的精髓。這裡計劃: 1.先說題

——2017校招線上程式設計python&C++

牛客網——2017校招真題線上程式設計(python&C++)題目描述找出n個數裡最小的k個輸入描述:每個測試輸入包含空格分割的n+1個整數,最後一個整數為k值,n 不超過100。輸出描述:輸出

浙大pat | 浙大pat乙級1009

題目描述給定任一個各位數字不完全相同的4位正整數,如果我們先把4個數字按非遞增排序,再按非遞減排序,然後用第1個數字減第2個數字,將得到一個新的數字。一直重複這樣做,我們很快會停在有“數字黑洞”之稱的6174,這個神奇的數字也叫Kaprekar常數。例如,我們從6767開始,

【華為機試】找最高分(通過此熟悉Node輸入輸出)

length 輸出 ons ken [0 接下來 lin tput int 來源:牛客網 老師想知道從某某同學當中,分數最高的是多少,現在請你編程模擬老師的詢問。當然,老師有時候需要更新某位同學的成績. 輸入描述: 輸入包括多組測試數據。每組輸入第一行是兩個正整數N和M(0

java10.3

多少 print ray 返回 ++i string 保留 刪除元素 -s 1、定義有StringBuffer s1=new StringBuffer(10);s1.append(“1234”)則s1.length()和s1.capacity()分別是多少? length(

中南林業科技大學第十一屆程序設計大賽J 二分+線段樹

main build query 。。 eof its include pan ID https://www.nowcoder.com/acm/contest/124#question 題意 找第一個不小於K的數的下標,然後對它前一個數加一 解析 我們可以維護一個最大

發現的考研不錯,趕緊刷刷

break while class pop adl 升序 bsp put lap    題目: #下面這一段用一個txt來保存input的信息來模擬input.最後提交代碼時候刪除這一段即可. a9999=open(‘1.txt‘,‘r‘) def input():

算法 19 二叉平衡樹檢查 CC150

復雜 tro 返回 false ron code return nlog getheight 算法題 19 二叉平衡樹檢查 牛客網 CC150 實現一個函數,檢查二叉樹是否平衡,平衡的定義如下,對於樹中的任意一個結點,其兩顆子樹的高度差不超過1。 給定指向樹根結點的指針Tr

暑期ACM多校訓練營第二場I.car-規律思維

target 長度 情況下 con targe 標記 多校 class clu I.car 車只能從一邊走到另一邊,而且車和車不能相撞,車也不能走到坑裏。所以直接找規律,如果沒有坑,最多能放多少輛車。就會發現,關於對角線對稱的兩邊只能放一輛車,如果是奇數個的時候,中

暑期ACM多校訓練營第二場菜鳥補QAQ

warn 分享圖片 ini lin int 技術分享 ace main bre   G transform   題目大意: 數軸上有n個集裝箱,第i個集裝箱位於坐標x[i],有a[i]件貨物。現在要把集裝箱進行一些移動,求在所有貨物移動總距離不超過T的情況下,最多能把多少

暑期ACM多校訓練營第九場 A FWT

coder using lse -a 訓練 wiki png mes font 鏈接:https://www.nowcoder.com/acm/contest/147/A來源:牛客網 Niuniu has recently learned how to use Gauss

算法 22 折紙問題 ,今日頭條

-s question app 數組 sample 代碼 tro quest 每次 鏈接:https://www.nowcoder.com/questionTerminal/430180b66a7547e1963b69b1d0efbd3c來源:牛客網 請把紙條豎著放在桌?上