自己报的名,含着泪也要考完试TUT
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 < 所 求 分 数 的 分 子 < n 2 ∗ k m 2 \frac{n1*k}{m1} < 所求分数的分子 <\frac{n2*k}{m2} m1n1∗k<所求分数的分子<m2n2∗k
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