【Java】PAT乙级真题全记录(三)61到80题

自己报的名,含着泪也要考完试TUT1061 判断题1062 最简分数1063 计算谱半径1064 朋友数1065 单身狗(测试点3、4运行超时)1061 判断题水题import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.StreamT...
摘要由CSDN通过智能技术生成

1061 判断题

水题

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Practice1061 {
   

	static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

	static int nextInt() throws IOException {
   
		st.nextToken();
		return (int) st.nval;
	}

	public static void main(String[] args) throws IOException {
   
		int n = nextInt(); // 学生人数,<=100
		int m = nextInt(); // 判断题数量,<=100

		int[] point = new int[m]; // 每道题的分值
		for (int i = 0; i < m; i++)
			point[i] = nextInt();

		int[] key = new int[m]; // 每道题的正确答案
		for (int i = 0; i < m; i++)
			key[i] = nextInt();

		int[] grade = new int[n]; // 每个学生的分数
		for (int i = 0; i < n; i++) {
   
			for (int j = 0; j < m; j++) {
   
				grade[i] += nextInt() == key[j] ? point[j] : 0;
			}
			System.out.println(grade[i]);
		}
	}
}

1062 最简分数

经变换可得到: n 1 ∗ k m 1 &lt; 所 求 分 数 的 分 子 &lt; n 2 ∗ k m 2 \frac{n1*k}{m1} &lt; 所求分数的分子 &lt;\frac{n2*k}{m2} m1n1k<<m2n2k

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
   
	public static void main(String[] args) throws IOException {
   
		// 题目保证给出的所有整数都不超过 1000,因此以下涉及整数均在Int范围内
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] temp = br.readLine().split("\\s+|\\/");
		int n1 = Integer.parseInt(temp[0]);
		int m1 = Integer.parseInt(temp[1]);
		int n2 = Integer.parseInt(temp[2]);
		int m2 = Integer.parseInt(temp[3]);
		int k = Integer.parseInt(temp[4]);

		int low = (int) (Math.min((double) n1 / m1, (double) n2 / m2) * k);
		int high1 = (double) n1 / m1 > (double) n2 / m2 ? n1 : n2;	// 更大数字的分子
		int high2 = (double) n1 / m1 > (double) n2 / m2 ? m1 : m2;	// 更大数字的分母

		boolean isFirst = true;
		for (int i = low + 1; i * high2 < high1 * k; i++)
			if (gcd(k, i) == 1) {
   
				if (isFirst) {
   
					System.out.print(i + "/" + k);
					isFirst = false;
				} else {
   
					System.out.print(" " + i + "/" + k);
				}
			}
	}

	public static int gcd(int a, int b) {
   
		return b == 0 ? a : gcd(b, a % b);
	}
}

1063 计算谱半径

水题

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.StreamTokenizer;
import java.io.IOException;

public class Main {
   

	static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

	static int nextInt() throws IOException {
   
		st.nextToken();
		return (int) st.nval;
	}

	public static void main(String[] args) throws IOException {
   
		int n = nextInt();
		int max = 0;
		while(n-->0) {
   
			int temp = (int) Math.pow(nextInt(), 2)+(int) Math.pow(nextInt(), 2);
			if(temp > max)
				max = temp;
		}
		
		System.out.printf("%.2f",Math.sqrt(max));
	}

}

1064 朋友数

水题

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Collections;

public class Main {
   
	static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

	static int nextInt() throws IOException {
   
		st.nextToken();
		return (int) st.nval;
	}

	public static void main(String[] args) throws IOException {
   
		int n = nextInt();
		ArrayList<Integer> arr = new ArrayList<>();		// 每个数的朋友数
		while (n-- > 0) {
   
			int temp = nextInt();
			int total = 0;								// 计算当前数temp的朋友数
			while (temp > 0) {
   
				total += temp % 10;
				temp /= 10;
			}
			if (!arr.contains(total))					// 如果不包含此朋友数就加入
				arr.add(total);
		}
		Collections.sort(arr);							// 升序排列

		int last = arr.size() - 1;
		System.out.println(last + 1);
		for (int i = 0; i < last; i++)
			System.out.print(arr.get(i) + " ");
		System.out.print(arr.get(last));
	}

}

1065 单身狗(测试点3、4运行超时)

测试点1的错误原因:当出席者均为夫妇时,结果为0,不输出ID
如果使用ArrayList,移除后一定要将进行循环的i–

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main {
   

	public static void main(String[] args) throws IOException {
   
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());
		int[] couple = new int[100000];
		Arrays.fill(couple, -1);
		while (n-- > 0) {
   
			String[] temp = br.readLine().split("\\s+");
			int cp1 = Integer.parseInt(temp[0]);
			int cp2 = Integer.parseInt(temp[1]);
			couple[cp1] = cp2;								// 互存对方的ID表示为夫妇
			couple[cp2] = cp1;
		}

		ArrayList<Integer> guest = new ArrayList<>();
		int m = Integer.parseInt(br.readLine().trim());
		String[] attendance = br.readLine().split("\\s+");
		while (--m >= 0)
			guest.add(Integer.parseInt(attendance[m]));		// 出席者列入ArrayList

		for (int i = 0; i < guest.size(); i++) {
   
			Integer g = guest.get(i);
			Integer gcp = couple[g];
			if (guest.contains(gcp)) {
   				// 如果有cp且出席就将两者移除
				guest.remove(g);
				guest.remove(gcp);
                i--;								// 注意要将i--,因为移除后序号仍不变
			}
		}

		Collections.sort(guest);					// 升序排序
		int last = guest.size() - 1;				// 最后一位“单身狗”
		System.out.println(last + 1);
		if (last > -1) {
   
			for (int i = 0; i < last; i++)
				System.out.printf("%05d ", guest.get(i));		// 按格式输出
			System.out.printf("%05d", guest.get(last));
		}
	}

}

1066 图像过滤(测试点3运行超时)

水题

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main {
   
	static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值