Java 实现PAT乙级(Basic Level)1001-1010 解题报告(一)

最近有想在OJ上刷一点点题,从简单的开始,感觉有的程序有点繁琐,想法不够清晰,如有更好的解法,希望大神们多多提提意见。

       题目列表:

1001:A+B和C 

1002:数字分类

1003:数素数

1004:福尔摩斯的约会

1005:德才论

1006:部分A+B

1007:A除以B

1008:锤子剪刀布

1009:数字黑洞

1010:月饼

程序如下:

1001:A+B和C 

import java.util.Scanner;
public class
Main{
public static void main(String[] agrs) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String[][] array = new String[t][];
for (int i = 0; i < t; i++) {
array[i] = new String[3];
array[i][0] = in.next();
array[i][1] = in.next();
array[i][2] = in.next();
}
in.close();
for (int i = 0; i < t; i++) {
System.out.print("Case " + "#" + (i + 1) + ": ");
long a = Long.parseLong(array[i][0]);
long b = Long.parseLong(array[i][1]);
long c = Long.parseLong(array[i][2]);
if (a + b > c) {
System.out.println("true");
}
else {
System.out.println("false");
}
}


}
}



1002:数字分类

import java.util.Scanner;


public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String[] array = new String[N];
for (int i = 0; i < N; i++) {
array[i] = in.next();
}
in.close();
int a1 = 0, n1 = 0;
int a2 = 0, n2 = 0;
int a3 = 0, n3 = 0;
double a4 = 0;
int n4 = 0;
int a5 = 0, n5 = 0;


for (int i = 0; i < N; i++) {
int num = Integer.parseInt(array[i]);
int mod = num % 5;
switch (mod) {
case 0:


if (num % 2 == 0) {
a1 = a1 + num;
n1++;
}
break;
case 1:
n2++;
if (n2 % 2 == 1) {
a2 = a2 + num;
} else {
a2 = a2 - num;
}
break;
case 2:
n3++;
a3 = a3 + 1;
break;
case 3:
n4++;
a4 = a4 + num;
break;
case 4:
n5++;
if (num > a5) {
a5 = num;
}
break;
}
}
// a1
if (n1 == 0) {
System.out.print("N ");
} else {
System.out.print(a1 + " ");
}
// a2
if (n2 == 0) {
System.out.print("N ");
} else {
System.out.print(a2 + " ");
}
// a3
if (n3 == 0) {
System.out.print("N ");
} else {
System.out.print(a3 + " ");
}
// a4
if (n4 == 0) {
System.out.print("N ");
} else {
System.out.printf("%.1f ", (double) a4 / (double) n4);
}
// a5
if (n5 == 0) {
System.out.print("N");
} else {
System.out.print(a5);
}


}


}



1003:数素数



import java.util.Scanner;


public class Main {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int begin = in.nextInt();
int end = in.nextInt();
in.close();
int[] array = new int[end];
int index = 0;
int i = 2;
while (index < end) {
if (Isprime(i)) {
array[index] = i;
index++;
}
i++;
}
int ten = 0;
for (int j = (begin - 1); j < end; j++) {
System.out.print(array[j]);
ten++;
if (ten % 10 == 0) {
System.out.println();
continue;
}
if (j != end - 1) {
System.out.print(" ");
}
}
}


public static boolean Isprime(int n) {
if (n == 1) {
return false;
} else if (n == 2) {
return true;
} else if (n % 2 == 0) {
return false;
} else {

for (int i = 3; i <= (int)Math.sqrt(n); i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}


}
}



1004:福尔摩斯的约会

import java.util.Scanner;


public class pat1004 {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String[] str = new String[4];
for (int i = 0; i < str.length; i++) {
str[i] = in.nextLine();
}
in.close();
getDayHour(str);
getMinute(str);
}


public static void getDayHour(String[] str) {
String[] week = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
int flag = 0;
for (int i = 0; i < str[0].length(); i++) {
char c = str[0].charAt(i);
if (c == str[1].charAt(i)) {
if (flag == 0 && c >= 'A' && c <= 'Z') {
System.out.print(week[c - 'A'] + " ");
flag++;
continue;
}
if (flag == 1) {
if (Character.isDigit(c)) {
System.out.print("0" + c + ":");
break;
}
if (c >= 'A' && c <= 'N') {
System.out.print((c - 'A' + 10) + ":");
break;
}
}
}
}
}


public static void getMinute(String[] str) {
for (int i = 0; i < str[2].length(); i++) {
char c = str[2].charAt(i);
if (c == str[3].charAt(i) && Character.isLetter(c)) {
System.out.print(i >= 10 ? i : "0" + i);
break;
}
}
}
}



1005:德才论

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


public class Main {


@SuppressWarnings("unchecked")
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int L = in.nextInt();
int H = in.nextInt();
ArrayList<Student> list = new ArrayList<Student>();
Student s = null;
int count = 0; // 输出个数
for (int i = 0; i < N; i++) {
s = new Student();
s.setID(in.nextInt());
s.setDe(in.nextInt());
s.setCai(in.nextInt());
if (s.getDe() >= L && s.getCai() >= L) {
list.add(s);
count++;
}
}
in.close();
ArrayList<Student> L1 = new ArrayList<Student>();
ArrayList<Student> L2 = new ArrayList<Student>();
ArrayList<Student> L3 = new ArrayList<Student>();
ArrayList<Student> L4 = new ArrayList<Student>();


for (int i = 0; i < list.size(); i++) {
Student a = list.get(i);
if (a.getDe() >= H && a.getCai() >= H) {
L1.add(a);
continue;
} else if (a.getDe() >= H && a.getCai() >= L && a.getCai() <= H) {
L2.add(a);
continue;
} else if (a.getDe() >= L && a.getDe() < H && a.getCai() >= L
&& a.getCai() < H && a.getDe() >= a.getCai()) {
L3.add(a);
continue;
} else if(a.getDe() >= L && a.getCai() >= L
&& a.getDe() < a.getCai()) {
L4.add(a);
continue;
}
}
Collections.sort(L1, new SortByRule());
Collections.sort(L2, new SortByRule());
Collections.sort(L3, new SortByRule());
Collections.sort(L4, new SortByRule());
System.out.println(count);
for (int i = 0; i < L1.size(); i++) {
System.out.println(L1.get(i).getID() + " " + L1.get(i).getDe()
+ " " + L1.get(i).getCai());
}
for (int i = 0; i < L2.size(); i++) {
System.out.println(L2.get(i).getID() + " " + L2.get(i).getDe()
+ " " + L2.get(i).getCai());
}
for (int i = 0; i < L3.size(); i++) {
System.out.println(L3.get(i).getID() + " " + L3.get(i).getDe()
+ " " + L3.get(i).getCai());
}
for (int i = 0; i < L4.size(); i++) {
System.out.println(L4.get(i).getID() + " " + L4.get(i).getDe()
+ " " + L4.get(i).getCai());
}
}
}


class SortByRule implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.getSum() != s2.getSum()) {
return s2.getSum() - s1.getSum();
} else if (s1.getDe() != s2.getDe()) {
return s2.getDe() - s1.getDe();
} else {
return s1.getID() - s2.getID();
}
}
}


class Student {
int id, de, cai;


public int getID() {
return id;
}


public void setID(int id) {
this.id = id;
}


public int getDe() {
return de;
}


public void setDe(int de) {
this.de = de;
}


public int getCai() {
return cai;
}


public void setCai(int cai) {
this.cai = cai;
}


public int getSum() {
return this.getDe() + this.getCai();
}
}



1006:部分A+B



import java.util.Scanner;


public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
in.close();
String[] str = s.split(" ");
int a = 0, b = 0;
Long PA = Long.parseLong(str[1]);
Long PB = Long.parseLong(str[3]);
Long sumA =  0L, sumB = 0L;
for (int i = 0; i < str[0].length(); i++) {
if (str[1].charAt(0) == str[0].charAt(i)) {
sumA = (long) (sumA + PA * Math.pow(10, a));
a++;
}
}
for (int i = 0; i < str[2].length(); i++) {
if (str[3].charAt(0) == str[2].charAt(i)) {
sumB = (long) (sumB + PB * Math.pow(10, b));
b++;
}
}
System.out.println(sumA+sumB);
}
}



1007:A除以B



import java.util.Scanner;


public class pat1007 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String A = in.next();
int B = in.nextInt();
in.close();
StringBuffer Q = new StringBuffer();
int R = 0, div = 0, temp = 0, count = 0;
for (int i = 0; i < A.length() - 1; i++) {
if (count == 0) {
temp = Integer.parseInt(A.charAt(i) + "") * 10
+ Integer.parseInt(A.charAt(i + 1) + "");
count++;
} else {
temp = R * 10 + Integer.parseInt(A.charAt(i + 1) + "");
}
div = temp / B;
Q.append(String.valueOf(div));
R = temp % B;
}
System.out.print(Q.toString() + " " + R);
}
}




1008:锤子剪刀布



import java.util.Scanner;


public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String[][] str = new String[N][2];
for (int i = 0; i < N; i++) {
str[i][0] = in.next();
str[i][1] = in.next();
}
int[][] count = new int[3][3];
for (int i = 0; i < N; i++) {
String jia = str[i][0], yi = str[i][1];
if (jia.equals("C")) {
if (yi.equals("J")) {
count[0][0]++;// 统计甲胜
count[1][0]++;// 甲C胜
} else if (yi.equals("C")) {
count[0][1]++;// 统计甲平
} else {
count[0][2]++;// 统计甲负
count[2][2]++;// 统计乙B胜
}
} else if (jia.equals("J")) {
if (yi.equals("B")) {
count[0][0]++;// 统计甲胜
count[1][1]++;// 甲J胜
} else if (yi.equals("J")) {
count[0][1]++;// 统计甲平
} else {
count[0][2]++;// 统计甲负
count[2][0]++;// 统计乙c胜
}
} else {
if (yi.equals("C")) {
count[0][0]++;// 统计甲胜
count[1][2]++;// 统计甲B胜
} else if (yi.equals("B")) {
count[0][1]++;// 统计甲平
} else {
count[0][2]++;// 统计甲负
count[2][1]++;// 统计乙J胜
}
}
}
System.out.println(count[0][0] + " " + count[0][1] + " " + count[0][2]);
System.out.println(count[0][2] + " " + count[0][1] + " " + count[0][0]);


int c1 = 0, j1 = 0, b1 = 0;
for (int i = 1; i < 3; i++) {
c1 = count[i][0];
j1 = count[i][1];
b1 = count[i][2];
if (b1 >= c1 && b1 >= j1) {
System.out.print("B");
} else if (c1 >= j1 && c1 > b1) {
System.out.print("C");
} else {
System.out.print("J");
}
if (i == 1) {
System.out.print(" ");
}
}
}
}





1009:数字黑洞

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num ;
        num = input.nextInt();
        if (num <= 9) {
            num = num*1000;
        }else if (num <= 99) {
            num = num*100;
        }else if (num <= 999) {
            num = num*10;
        }
         
         
        if ( num % 1111 == 0 ) {
            System.out.print(num+" - "+num+" = 0000");
        }else {
            begin(num);
        }
         
    }
     
    public static void begin(int num){
        int k;
        String Max = "",Min = "";
        int[] a1 =new int[4];
        a1[0] = num % 10;;
        a1[1] = (num / 10) % 10;
        a1[2] = (num / 100) % 10;
        a1[3] = (num / 1000) % 10;
        sort(a1);
        for (int i = 0; i < a1.length; i++) {
            Min += a1[i];
        }
        for (int i = a1.length-1 ; i >= 0; i--) {
            Max += a1[i];
        }  
        k = Integer.valueOf(Max) - Integer.valueOf(Min);
        if(k == 6174){
            System.out.print(Max+" - "+Min+" = "+k);
            return;
        }else {
            System.out.println(Max+" - "+Min+" = "+k);
   //这步方法就是递归,方法内部调用自己这个方法,当满足k==6174时候就会跳出递归        //递归方法一定要注意条件,要不容易死循环
            begin(k);
        }
    }
     
    public static void sort(int[] a){
        int n;
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i+1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    n = a[i];
                    a[i] = a[j];
                    a[j] = n;
                }
            }
        }
         
    }
}







1010:月饼





import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Vector;


public class Main{


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double need = in.nextDouble();
double[] numArr = new double[n];
double [] totalArr = new double[n];
for (int i = 0; i < numArr.length; i++) {
numArr[i] = in.nextDouble();
}
for (int i = 0; i < totalArr.length; i++) {
totalArr[i] = in.nextDouble();
}
Vector<cake> v = new Vector<cake>();


for (int i = 0; i < n; i++) {
cake temp = new cake();
temp.setNum(numArr[i]);
temp.setTotalPrice(totalArr[i]);
v.add(temp);
}
in.close();
Collections.sort(v, new sortByPrice());
double profit = 0;
double nowNum = 0;
DecimalFormat df = new DecimalFormat("###.00");
for (int j = 0; j < n; j++) {
if ((nowNum+v.get(j).getNum()) >= need) {
if (profit == 0) {
profit = v.get(j).getTotalPrice()/ v.get(j).getNum() * need;
break;
} else {
profit = profit + v.get(j).getTotalPrice()/ v.get(j).getNum() * (need-nowNum);
break;
}
} else {
nowNum = nowNum+v.get(j).getNum();
profit = profit + v.get(j).getTotalPrice();
}
}
System.out.println(df.format(profit));


}
}


class sortByPrice implements Comparator {
public int compare(Object O1, Object O2) {
cake s1 = (cake) O1;
cake s2 = (cake) O2;
if (s1.getPrice() - s2.getPrice() > 0) {
return -1;
} else {
return 1;
}
}
}


class cake {
double num;
double totalPrice;


public void setNum(double num) {
this.num = num;
}


public double getNum() {
return num;
}


public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}


public double getTotalPrice() {
return totalPrice;
}


public double getPrice() {
return this.totalPrice / this.num;
}
}




如有转载,请注明作者和地址http://blog.csdn.net/tn_ms/article/details/47863941,谢谢了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值