PAT乙级真题(上)

(1001) A + B 和 C

在这里插入图片描述

Java 实现

import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int num = 0;
    		long a = 0, b = 0, c = 0;
    		boolean flag = true;
    		num = sc.nextInt();
    		for(int i = 1; i <= num; i++) {
    			a = sc.nextLong();
    			b = sc.nextLong();
    			c = sc.nextLong();
    			if(a + b > c) {
    				flag = true;
    			}
    			else {
    				flag = false;
    			}
    			System.out.println("Case #" + i + ": " + flag);
    		}
    		sc.close();
    	}
    }

(1002) 数字分类

在这里插入图片描述

Java实现

import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = 0;
		n = sc.nextInt();
		int a1 = 0;
		int a2 = 0;
		int a3 = 0;
		float a4 = 0f;
		int a5 = 0;
		
		int[] arr = new int[n];
		
		int flag = 1;
		int count = 0;
		int max = 0;
		
		for(int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
			switch(arr[i] % 5) {
			case 0:
				if(arr[i] % 2 == 0) {
					a1 += arr[i];
				}
				break;
			case 1:
				a2 += flag * arr[i];
				flag = 0 - flag;
				break;
			case 2:
				a3++;
				break;
			case 3:
				a4 += arr[i];
				count++;
				break;
			case 4:
				if(arr[i] > max) {
					max = arr[i];
				}
				break;
			}
		}
		a4 = a4 / count;
		a5 = max;
		
		String stra4 = "N";
		
		
		if(a1 == 0) {
			System.out.print("N ");
		}
		else {
			System.out.print(a1 + " ");
		}
		if(a2 == 0) {
			System.out.print("N ");
		}
		else {
			System.out.print(a2 + " ");
		}
		if(a3 == 0) {
			System.out.print("N ");
		}
		else {
			System.out.print(a3 + " ");
		}
		if(a4 > 0) {
			stra4 = new DecimalFormat("0.0").format(a4);
			System.out.print(stra4 + " ");
		}
		else {
			System.out.print("N ");
		}
		if(a5 == 0) {
			System.out.print("N");
		}
		else {
			System.out.print(a5);
		}
		sc.close();
	}
}

(1003) 数素数

在这里插入图片描述
Java实现

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Main main = new Main();
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		boolean isFirst = true;
		int count = 0;//素数的计数
		int num = 0;//结果的计数
		
		for (int i = 2; i < Integer.MAX_VALUE; i++) {
			if (main.isPrime(i) == true) {
				count++;
				if (count >= m && count <= n) {
					num++;
					if (isFirst == true) {
						isFirst = false;
						System.out.print(i);
					} else {
						System.out.print(" " + i);
					}
					if (num % 10 == 0) {
						System.out.print("\r\n");
						isFirst = true;
					}
				}
				else if(count >= n)
				{
					break;
				}
			}
		}
		sc.close();
	}
	
	boolean isPrime(int n) {
		boolean flag = true;
		if(n < 2) {
			flag = false;
		}
		for(int i = 2; i <= Math.sqrt(n); i++) {
			if(n % i == 0) {
				flag = false;
				break;
			}
		}
		return flag;
	}
}

(1004) 福尔摩斯的约会

在这里插入图片描述
Java实现

import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		String s1 = sc.next();
		String s2 = sc.next();
		String s3 = sc.next();
		String s4 = sc.next();
		
		char[] chars1 = s1.toCharArray();
		char[] chars2 = s2.toCharArray();
		char[] chars3 = s3.toCharArray();
		char[] chars4 = s4.toCharArray();
		
		int length1 = s1.length() < s2.length() ? s1.length() : s2.length();
		int length2 = s3.length() < s4.length() ? s3.length() : s4.length();
		
		StringBuilder sb = new StringBuilder();
		int temp = 0;
		
		for(int i = 0; i < length1; i++) {
			if(chars1[i] == chars2[i] && chars1[i] >= 'A' && chars1[i] <= 'G') {
				String weekday = calcWeekday(chars1[i]);
				sb.append(weekday);
				sb.append(" ");
				temp = i;
				break;
			}
		}
		for(int i = temp + 1; i < length1; i++) {
			if(chars1[i] == chars2[i] && 
					((chars1[i] >= '0' && chars1[i] <= '9') || (chars1[i] >= 'A' && chars1[i] <= 'N'))) {
				String hour = calcHour(chars1[i]);
				sb.append(hour);
				sb.append(":");
				break;
			}
		}
		for(int i = 0; i < length2; i++) {
			if(chars3[i] == chars4[i] && 
					((chars3[i] >= 'a' && chars3[i] <= 'z') || (chars3[i] >= 'A' && chars3[i] <= 'Z'))) {
				if(i < 10) {
					sb.append("0");
				}
				sb.append(i);
				break;
			}
		}
		System.out.println(sb.toString());
		sc.close();
	}
	
	public static String calcWeekday(char c) {
		String[] week = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
		int index = (c - 'A' + 1) % 7;
		return week[index];
	}
	
	public static String calcHour(char c) {
		StringBuilder sb = new StringBuilder();
		if(c >= '0' && c <= '9') {
			sb.append("0");
			sb.append(c);
		}
		else if(c >= 'A' && c <= 'N') {
			int hour = c - 'A' + 10;
			sb.append(hour);
		}
		return sb.toString();
	}
}

(1005) 德才论

在这里插入图片描述
Java实现

import java.util.Scanner;
import java.util.TreeSet;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int L = sc.nextInt();
		int H = sc.nextInt();
		
		TreeSet<Student> both = new TreeSet<Student>();
		TreeSet<Student> dc1 = new TreeSet<Student>();
		TreeSet<Student> dc2 = new TreeSet<Student>();
		TreeSet<Student> dc3 = new TreeSet<Student>();
		
		while(N-- != 0) {
			Student stu = new Student(sc.nextInt(), sc.nextInt(), sc.nextInt());
			if(stu.d < L || stu.c < L) {
				continue;
			}
			if(stu.d >= H && stu.c >= H) {
				both.add(stu);
			}
			else if(stu.d >= H) {
				dc1.add(stu);
			}
			else if(stu.d >= stu.c) {
				dc2.add(stu);
			}
			else {
				dc3.add(stu);
			}
		}
		
		System.out.println(both.size() + dc1.size() + dc2.size() + dc3.size());
		print(both);
		print(dc1);
		print(dc2);
		print(dc3);
		sc.close();
	}

	static class Student implements Comparable<Student>{
		int id, d, c, sum;
		public Student(int id, int d, int c) {
			this.id = id;
			this.d = d;
			this.c = c;
			this.sum = d + c;
		}
		@Override
		public int compareTo(Student o) {
			if(this.sum != o.sum) {
				return o.sum - this.sum;	//倒序
			}
			if(this.d != o.d){
				return o.d - this.d;	//倒序
			}
			return this.id - o.id;	//正序
		}
	}
	
	static void print(TreeSet<Student> set) {
		for(Student s: set) {
			System.out.println(s.id + " " + s.d + " " + s.c);
		}
	}
}

(1006) 部分A + B

在这里插入图片描述

import java.util.Scanner;

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 sum = calcNum(A, Da) + calcNum(B, Db);
		System.out.println(sum);
		sc.close();
	}
	
	static int calcNum(int a, int da) {
		int sum = 0;
		int count = 0;
		while(a > 0) {
			if(a % 10 == da) {
				count++;
			}
			a /= 10;
		}
		while(count-- > 0) {
			sum += da * Math.pow(10, count);
		}
		return sum;
	}
}

(1007) A除以B

在这里插入图片描述
Java实现

import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		String A = sc.next();
		int B = sc.nextInt();
		
		StringBuilder Q = new StringBuilder();
		int R = 0;
		//第一个数字特殊处理,判断是否够整除
		int num = A.charAt(0) - '0';
		if(num >= B) {
			Q.append(num / B);
			R = num % B;
		}
		else {
			R = num % B;
		}
		//其它数字正常
		for(int i = 1; i < A.length(); i++) {
			num = R * 10 + (A.charAt(i) - '0');
			Q.append(num / B);
			R = num % B;
		}
		if(A.length() == 1) {
			Q.append("0");
		}
		System.out.println(Q.toString() + " " + R);
		sc.close();
	}
}

(1008) 锤子剪刀布

在这里插入图片描述
Java实现

import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String bcj = "BCJ";
		int[] a = new int[3];
		int[] b = new int[3];
		for(int i = 0; i < n; i++) {
			String s1 = sc.next();
			String s2 = sc.next();
			int rst = judge(s1, s2);
			if(rst == 1) {
				a[bcj.indexOf(s1.charAt(0))]++;
			}
			else if(rst == -1) {
				b[bcj.indexOf(s2.charAt(0))]++;
			}
		}
		int suma = a[0] + a[1] + a[2];
		int sumb = b[0] + b[1] + b[2];
		int other = n - suma - sumb;
		System.out.println(suma + " " + other + " " + sumb);
		System.out.println(sumb + " " + other + " " + suma);
		System.out.println(most(a) + " " + most(b));
		sc.close();
	}
	
	static int judge(String s1, String s2) {
		int rst = 0;
		if(s1.equals("C")) {
			if(s2.equals("J")) {
				rst = 1;
			}
			else if(s2.equals("B")) {
				rst = -1;
			}
		}
		else if(s1.equals("J")) {
			if(s2.equals("B")) {
				rst = 1;
			}
			else if(s2.equals("C")) {
				rst = -1;
			}
		}
		else if(s1.equals("B")) {
			if(s2.equals("C")) {
				rst = 1;
			}
			else if(s2.equals("J")) {
				rst = -1;
			}
		}
		return rst;
	}
	
	static char most(int[] a) {
		int m = a[0];
		int indexOfMax = 0;
		for(int i = 1; i < a.length; i++) {
			if(a[i] > m) {
				m = a[i];
				indexOfMax = i;
			}
		}
		String s = "BCJ";
		return s.charAt(indexOfMax);
	}
}

(1009) 数字黑洞

在这里插入图片描述
Java实现

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		do{
			n = calcNum(n);
			if(n == 0) {
				break;
			}
		}
		while(n != 6174);
		sc.close();
	}
	
	static int calcNum(int n) {
		int[] a = new int[4];
		for(int i = 3; i > -1; i--) {
			a[i] = n % 10;
			n = n / 10;
		}
		Arrays.sort(a);
		int n1 = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
		int n2 = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
		int rst = n1 - n2;
		String s1 = String.format("%04d", n1);
		String s2 = String.format("%04d", n2);
		String s3 = String.format("%04d", rst);
		System.out.println(s1 + " - " + s2 + " = " + s3);
		return rst;
	}
}

(1010) 月饼

在这里插入图片描述
Java实现

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

public class Main {

public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n= sc.nextInt();
		int d = sc.nextInt();
		
		double[] a = new double[n];
		double[] b = new double[n];
		List<Cake> cakes = new ArrayList<Cake>();
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextDouble();
		}
		for(int i = 0; i < n; i++) {
			b[i] = sc.nextDouble();
		}
		for(int i = 0; i < n; i++) {
			Cake c = new Cake(a[i], b[i]);
			cakes.add(c);
		}
		
		Collections.sort(cakes, new Comparator<Cake>() {
			@Override
			public int compare(Cake c1, Cake c2) {
				if(c1.unit > c2.unit) {
					return -1;
				}
				else if(c1.unit < c2.unit) {
					return 1;
				}
				else {
					return 0;
				}
			}
		});
		
		double r = d;
		double max = 0;
		for(Cake c : cakes) {
			if(r > c.num) {
				r = r - c.num;
				max += c.total;
			}
			else {
				max += r / c.num * c.total;
				break;
			}
		}
		System.out.printf("%.2f", max);
		sc.close();
	}
	
	static class Cake{
		public double num;
		public double total;
		public double unit;
		
		public Cake(double num, double total) {
			this.num = num;
			this.total = total;
			this.unit = total / num;
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值