【Java】PAT乙级真题全记录(二)21到40题


其实PAT的坑在刷题过程中就会发现,还是有迹可循的。常见的坑点如:输入样例的 数据类型范围限制特殊输入的情况,比如0;输出数据的 格式;“不超过”,“不低于”等字样表示的 边界值情况等。最难识别的还是题目的 逻辑盲点,总之题要好好分析再想解决问题的思路,先理解好题目才能不走弯路!

废话完了开正题(21到40题,没标注就是测试点全通过)

1021 个位数统计

import java.util.Scanner;
public class Main {
   

	public static void main(String[] args) {
   
		Scanner sc = new Scanner(System.in);
		String num = sc.nextLine();

		int[] count = new int[10];
		for(int i = 0; i < num.length(); i++) {
   
			switch(num.charAt(i)) {
   
			case '0':	count[0]++;	continue;
			case '1':	count[1]++;	continue;
			case '2':	count[2]++;	continue;
			case '3':	count[3]++;	continue;
			case '4':	count[4]++;	continue;
			case '5':	count[5]++;	continue;
			case '6':	count[6]++;	continue;
			case '7':	count[7]++;	continue;
			case '8':	count[8]++;	continue;
			case '9':	count[9]++;	continue;
			}
		}
		
		for(int i = 0; i < 10; i++) {
   
			if(count[i]!=0) {
   
				System.out.println(i+":"+count[i]);
			}
		}
	}
}

1022 D进制的A+B

注意结果为0的情况哦!

import java.util.Scanner;
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();
		int dec = A + B;
		
		StringBuilder numD = new StringBuilder("");
		if(dec == 0){
   
			numD.append("0");
		}else{
   
            while(dec > 0) {
   
				numD.append(dec % D);
				dec = dec / D;
		    }
        }
		System.out.println(numD.reverse().toString());
	}
}

1023 组个最小数

唯一要注意的就是寻找开头数字的条件:数字不为0,计数不为0,并且是计数不为0的数字中最小的数字。剩下的按照有小到大排列即可。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
   
	public static void main(String[] args) throws IOException {
   
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] temp = br.readLine().trim().split("\\s+");
		int[] count = new int[10];

		int firstDigit = 0;
		boolean minNotZero = true;
		for (int i = 0; i < 10; i++) {
   
			count[i] = Integer.valueOf(temp[i]);
			if (i != 0 && minNotZero == true && count[i] != 0) {
    // 找到非0的最小数字并将对应的计数减1
				firstDigit = i;
				count[i]--;
				minNotZero = false;
			}
		}

		StringBuilder str = new StringBuilder(String.valueOf(firstDigit));
		for (int i = 0; i < 10; i++) {
   
			for (int j = 0; j < count[i]; j++)
				str.append(String.valueOf(i));
		}

		System.out.println(str);
	}
}

1024 科学计数法

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
   
	public static void main(String[] args) throws IOException {
   
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] input = br.readLine().trim().split("E|\\.");
		char sign1 = input[0].charAt(0);	// sign1是整数部分的正负号
		char sign2 = input[2].charAt(0);	// sign2是指数部分的正负号

		StringBuilder result = new StringBuilder(input[0].charAt(1)+input[1]); 	// result最终得到输出结果,首先加入小数点左侧的一位数字[1-9],再加入小数点右侧的数字串
		String[] temp = input[2].split("\\+|\\-"); 
		int exp = Integer.parseInt(temp[1]); // exp存储指数部分的数字

		if (sign2 == '-') {
   
			for (int i = 0; i < exp; i++)
				result.insert(0, '0');
			result.insert(1, '.');
		} else {
   
			int pointMove = exp - input[1].length();
			if (pointMove >= 0) {
   	// pointMove大于等于0则小数部分为0,即结果为整数,增加0即可
				for (int i = 0; i < pointMove; i++)
					result.append('0');
			}else {
   	                // pointMove小于0则挪动小数点,并插入小数点,即结果为浮点数
				result.insert((exp + 1), '.');
			}
		}

		if (sign1 == '-')
			result.insert(0, '-');
		System.out.println(result);
	}
}

1025 反转链表(测试点5运行超时)

本题最大坑点在有可能无法完全遍历链表,如果出现了-1就要提前结束,用-1之前的进行反转排序并输出。另外输出格式要格外注意。测试点5用Java过不了。

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedReader;
public class Main {
   
	public static void main(String[] args) throws Exception {
   
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] str = br.readLine().trim().split("\\s+");

		int firstNode = Integer.valueOf(str[0]);
		int n = Integer.parseInt(str[1]);
		int k = Integer.parseInt(str[2]);

		Node[] nodes = new Node[100000];
		while (n-- > 0) {
   
			String[] temp = br.readLine().trim().split("\\s+");
			nodes[Integer.valueOf(temp[0])] = new Node(temp[0], Integer.valueOf(temp[1]), Integer.valueOf(temp[2]));
		}

		int address = firstNode;
		ArrayList<Integer> record = new ArrayList<>();
		while (address != -1) {
   							// -1之前的记录下来
			record.add(address);
			address = nodes[address].nextNode;
		}
		
		int count = 0;
		while (record.size() >= (count + 1) * k)		// 每k个反转一次,分别为0到k-1,k到2k-1……
			reverse(record, count++ * k, count * k - 1);

		for (int i = 0; i < record.size()-1; i++)
			System.out.println(nodes[record.get(i)].address+" "+nodes[record.get(i)].value + " "+nodes[record.get(i+1)].address);
		System.out.println(nodes[record.get(record.size()-1)].address+" "+nodes[record.get(record.size()-1)].value + " -1");
	}

	public static void reverse(ArrayList<Integer> record, int start, int end) {
   
		while (start < end) {
   
			int temp = record.get(start);				// swap
			record.set(start++, record.get(end));
			record.set(end--, temp);
		}
	}
}

class Node {
   
	String address; 									// 便于输出
	int value;
	int nextNode;

	public Node() {
   }

	public Node(String address, int value, int nextNode) {
   
		this.address = address;
		this.value = value;
		this.nextNode = nextNode;
	}
}

1026 程序运行时间

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
   
	public static void main(String[] args) throws IOException {
   
		// 输入在一行中顺序给出 2 个整数 C1 和 C2。注意两次获得的时钟打点数肯定不相同,即 C1 < C2
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		final int CLK_TCK = 100;
		String[] str = br
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值