PTA|团体程序设计天梯赛-练习集|JAVA版
题目地址
网上大部分的参考代码都是c版的,所以我打算做一个java版
目前是打算全做,如果有题目被卡了就先跳过了。
PS:代码开头标记的数字,是我自己认为的难度,仅供参考。
如果有帮助到你,可以点赞催更。
已更新
L1 : 1-68
L2 : 5-9
题目索引
- L1-001 Hello World (5分)
- L1-002 打印沙漏 (20分)
- L1-003 个位数统计 (15分)
- L1-004 计算摄氏温度 (5分)
- L1-005 考试座位号 (15分)
- L1-006 连续因子 (20分)
- L1-007 念数字 (10分)
- L1-008 求整数段和 (10分)
- L1-009 N个数求和 (20分)
- L1-010 比较大小 (10分)
- L1-011 A-B (20分)
- L1-012 计算指数 (5分)
- L1-013 计算阶乘和 (10分)
- L1-014 简单题 (5分)
- L1-015 跟奥巴马一起画方块 (15分)
- L1-016 查验身份证 (15分)
- L1-017 到底有多二 (15分)
- L1-018 大笨钟 (10分)
- L1-019 谁先倒 (15分)
- L1-020 帅到没朋友 (20分)(未AC)
- L1-021 重要的话说三遍 (5分)
- L1-022 奇偶分家 (10分)
- L1-023 输出GPLT (20分)
- L1-024 后天 (5分)
- L1-025 正整数A+B (15分)
- L1-026 I Love GPLT (5分)
- L1-027 出租 (20分)
- L1-028 判断素数 (10分)
- L1-029 是不是太胖了 (5分)
- L1-030 一帮一 (15分)
- L1-031 到底是不是太胖了 (10分)
- L1-032 Left-pad (20分)
- L1-033 出生年 (15分)
- L1-034 点赞 (20分)
- L1-035 情人节 (15分)
- L1-036 A乘以B (5分)
- L1-037 A除以B (10分)
- L1-038 新世界 (5 分)
- L1-039 古风排版 (20 分)
- L1-040 最佳情侣身高差 (10 分)
- L1-041 寻找250 (10 分)
- L1-042 日期格式化 (5 分)
- L1-043 阅览室 (20 分)
- L1-044 稳赢 (15 分)
- L1-045 宇宙无敌大招呼 (5 分)
- L1-046 整除光棍 (20 分)
- L1-047 装睡 (10 分)
- L1-048 矩阵A乘以B (15 分)
- L1-049 天梯赛座位分配 (20 分)
- L1-050 倒数第N个字符串 (15 分)
- L1-051 打折 (5 分)
- L1-052 2018我们要赢 (5 分)
- L1-053 电子汪 (10 分)
- L1-054 福到了 (15 分)
- L1-055 谁是赢家 (10 分)
- L1-056 猜数字 (20 分)
- L1-057 PTA使我精神焕发 (5 分)
- L1-058 6翻了 (15 分)
- L1-059 敲笨钟 (20 分)
- L1-060 心理阴影面积 (5 分)
- L1-061 新胖子公式 (10 分)
- L1-062 幸运彩票 (15 分)
- L1-063 吃鱼还是吃肉 (10 分)
- L1-064 估值一亿的AI核心代码(未AC)
- L1-065 嫑废话上代码
- L1-066 猫是液体
- L1-067 洛希极限
- L1-068 调和平均
- L2-003 月饼
- L2-005 集合相似度
- L2-006 树的遍历
- L2-007 家庭房产 (25分)(未AC)
- L2-008 最长对称子串
- L2-009 抢红包(未AC)
- L2-015 互评成绩
- 持续更新中。。。
L1-001 Hello World (5分)
//0
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
L1-002 打印沙漏 (20分)
import java.util.Scanner;
//3
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
int a = 1;
int x = 2;
int h = 1;// 行数
while (a <= n) {
x += 4;
a += x;
h++;
}
a -= x;
h--;// 3
int yu = n - a;//用公式先算好最终会余下多少
String[] str = new String[h];//用数组存每行的*号是因为,图像是对称的,打印下半部分可以直接用
for (int i = 0; i < h; i++) {
str[i] = "";//数组初始化,不然默认是null
}
for (int i = 0; i < h; i++) {// 行
for (int j = 0; j < (h - i) * 2 - 1; j++) {// 储存*
str[i] += s;//如果题目卡时间可以用StringBuilder的append,而不是用"+"连接
}
for (int k = 0; k < i; k++) {
System.out.print(" ");
} // 打印空格
System.out.println(str[i]);
}
for (int i = 1; i < h; i++) {
for (int k = h - 1 - i; k > 0; k--) {
System.out.print(" ");
} // 打印空格
System.out.println(str[h - i - 1]);
}
System.out.println(yu);
}
}
L1-003 个位数统计 (15分)
import java.util.Scanner;
//思路:将每个数字放入字符数组后遍历,字符对应的数字作为计数器数组的下标,并对应计数器+1.
// x`遍历结束输出时,按增序检测计数器是否为0,并输出答案
//3
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] chars = sc.nextLine().toCharArray();
int[] ints = new int[10];
for (int i = 0; i < chars.length; i++) {
ints[chars[i] - '0']++;//一个char类型的数字减去char类型的'0',就等于int型的数字
}
for (int i = 0; i < ints.length; i++) {
if (ints[i] > 0)//数组元素默认为0
System.out.println(i + ":" + ints[i]);
}
}
}
L1-004 计算摄氏温度 (5分)
import java.util.Scanner;
//1
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
double f = sc.nextDouble();
double c = 5 * (f - 32) /9;
System.out.println("Celsius = " + (int)c);
}
}
L1-005 考试座位号 (15分)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//4
//这题其实不难,但是有点卡时间,java想过就必须优化
//①用BufferedReader 代替 Scanner
//②接受数据时,拿试机号当作下标,便于后面搜索
public class Main1005 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
String[] s = new String[n + 1];
for (int i = 0; i < n; i++) {
String[] temp = br.readLine().split(" ");
int t1 = Integer.valueOf(temp[1]);
s[t1] = temp[0] + " " + temp[2];
}
int n1 = Integer.valueOf(br.readLine());
String[] temp = br.readLine().split(" ");
for (int i = 0; i < n1; i++) {
System.out.println(s[Integer.valueOf(temp[i])]);
}
}
}
L1-006 连续因子 (20分)
import java.util.Scanner;
//4
//这题没卡时间,所以我用的比较暴力的办法,先把所有连乘的答案全找出来并存在数组里,最后遍历一下找最长的答案
//如果输入是个质数则只输出1和该数
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean flag = true;// 如果字符串里加了数字就改为false
int k = 2, x = 0;
int N = (int) Math.sqrt(n);
String str[] = new String[N];// 存,每个数字作为起始因子对应的答案
while (x < N) {// 循环到N-1 为止
int a = n, tk = k;
boolean b = true;// 处理答案中多一个*号的问题
str[x] = "";
while (a % tk == 0) {// 整除则继续
a /= tk;
if (b) {
flag = false;
b = false;
str[x] += tk;
tk++;
continue;
}
str[x] += "*" + tk;
tk++;
}
k++;
x++;
}
int maxi = 0;
for (int i = 1; i < str.length; i++) {
if (str[i].equals(""))
continue;
if (str[maxi].equals("")) {
maxi = i;
continue;
}
maxi = str[maxi].split("\\*").length >= str[i].split("\\*").length ? maxi : i;// 找最长答案对应的下标
}
if (flag) {// 排除质数的情况
System.out.println("1");
System.out.println(n);
} else {
System.out.println(str[maxi].split("\\*").length);
System.out.println(str[maxi]);
}
}
}
L1-007 念数字 (10分)
import java.util.Scanner;
//2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
String[] arr = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};//数表
for(int i = 0; i < s.length()-1;i++) {
if(s.charAt(i) >= '0' && s.charAt(i) <= '9') {//判断是否为数字
System.out.print(arr[s.charAt(i) - '0'] + " ");
}else
System.out.print("fu ");
}
System.out.println(arr[s.charAt(s.length() - 1) - '0']);//处理多余的空格问题
}
}
L1-008 求整数段和 (10分)
import java.util.Scanner;
//2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = 0;
int sum = 0;
for (; a <= b; a++) {
sum += a;
System.out.printf("%5d", a);
c++;
if (c % 5 == 0)//控制换行
System.out.println();
}
if (c % 5 != 0)//如果上面已经换过行了,则这里不用换
System.out.println();
System.out.println("Sum = " + sum);
}
}
L1-009 N个数求和 (20分)
import java.io.*;
import java.util.StringTokenizer;
// 4
public class Main {
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) {
int n = in.nextInt();
long fz = 0;
long fm = 1;
for (int i = 0; i < n; i++) {
String[] split = in.next().split("/");
long tfz = Long.valueOf(split[0]);
long tfm = Long.valueOf(split[1]);
long nfz = fz * tfm + tfz * fm;
long nfm = fm * tfm;
long gg = nfz == 0 ? 1 : gcd(Math.abs(nfz), Math.abs(nfm));// 分子为0不需要约分
fz = nfz / gg;
fm = nfm / gg;
}
if (fz == 0) {
System.out.println(0);
return;
}
if (fz < 0) {
fz *= -1;
System.out.print("-");
}
if (fz >= fm) {
System.out.print(fz / fm);
fz -= fz / fm * fm;
if (fz > 0) {
System.out.println(" " + fz + "/" + fm);
}
} else {
System.out.println(fz + "/" + fm);
}
}
private static long gcd(long nfz, long nfm) {
long a = Math.min(nfz, nfm);
long b = Math.max(nfz, nfm);
if (b % a == 0) {
return a;
}
return gcd(a, b % a);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
L1-010 比较大小 (10分)
import java.util.Arrays;
import java.util.Scanner;
//2
//方法一:用Arrays作弊233
//方法二:用三元(目)运算符一次性输出
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] res = sc.nextLine().split(" ");
int[] ints = new int[3];
for(int i = 0; i < 3;i++)
ints[i] = Integer.valueOf(res[i]);//一定要转换成int数组再排序,直接用String排序会出错
Arrays.sort(ints);
System.out.println(ints[0] + "->" + ints[1] + "->" + ints[2]);
// int a = sc.nextInt();
// int b = sc.nextInt();
// int c = sc.nextInt();
// System.out.println( ( a < b ? a < c ? a : c < b ? c : b : b < c ? b : c ) +
// "->" + ( a > b ? a > c ? b > c ? b : c : a : b > c ? a > c ? a : c : b ) +
// "->" + ( a > b ? a > c ? a : c : b > c ? b : c ) );
}
}
L1-011 A-B (20分)
// 3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));//相当于原来的Scanner sc = new Scanner(System.in);
char[] s = bf.readLine().toCharArray();//bf.readLine() 相当于 sc.nextLine()
char[] cs = new char[10000];
int a = bf.read(cs);// 将字符串填入cs中,并且返回char数组长度
int[] flag = new int[126];
for (int i = 0; i < a; i++) {
flag[cs[i]] = 1;
}
for (int i = 0; i < s.length; i++) {
if (flag[s[i]] == 0)
System.out.print(s[i]);
}
}
}
L1-012 计算指数 (5分)
import java.util.Scanner;
//1
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x = 1;
for (int i = 0; i < n; i++) {
x *= 2;
}
System.out.println("2^" + n + " = " + x);
}
}
L1-013 计算阶乘和 (10分)
import java.util.Scanner;
//2
public class Main1013 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += f(i);
}
System.out.println(sum);
}
public static int f(int a) {
if (a == 1)
return 1;
return a * f(a - 1);
}
}
L1-014 简单题 (5分)
//0
public class Main {
public static void main(String[] args) {
System.out.println("This is a simple problem.");
}
}
L1-015 跟奥巴马一起画方块 (15分)
import java.util.Scanner;
//1
public class Main1015 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
String s = sc.next();
for (int i = 0; i < (a + 1) / 2; i++) {
for (int j = 0; j < a; j++) {
System.out.print(s);
}
System.out.println();
}
}
}
L1-016 查验身份证 (15分)
import java.util.Scanner;
//4
public class Main1016 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] M = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
int[] ints = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
int n = sc.nextInt();
sc.nextLine();
String[] str = new String[n];
int c = 0;
boolean flag = true;
for (int i = 0; i < n; i++) {
str[i] = sc.nextLine();
}
loop: for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < 17; j++) {
if (str[i].charAt(j) < '0' || str[i].charAt(j) > '9') {
System.out.println(str[i]);
flag = false;
continue loop;
}
sum += (str[i].charAt(j) - '0') * ints[j];
}
if (M[sum % 11] != str[i].charAt(17)) {
System.out.println(str[i]);
flag = false;
}
}
if (flag)
System.out.println("All passed");
}
}
L1-017 到底有多二 (15分)
import java.util.Scanner;
//3
public class Main1017 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
boolean f = false;
boolean o = false;
double c = 0.0;
double res;
if (a.charAt(0) == '-')
f = true;
if ((a.charAt(a.length() - 1) - '0') % 2 == 0) {
o = true;
}
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == '2')
c++;
}
if (f)
res = c / (a.length() - 1) * 1.5;
else
res = c / (a.length());
if (o)
res *= 2;
res *= 100;
System.out.printf("%.2f", res);
System.out.println("%");
}
}
L1-018 大笨钟 (10分)
import java.util.Scanner;
//2
public class Main1018 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] str = s.split(":");
int h = Integer.valueOf(str[0]);
int m = Integer.valueOf(str[1]);
if (h <= 12)
System.out.println("Only " + s + ". Too early to Dang.");
else {
h -= 12;
if (m == 0)
for (int i = 0; i < h; i++) {
System.out.print("Dang");
}
else
for (int i = 0; i < h + 1; i++) {
System.out.print("Dang");
}
}
}
}
L1-019 谁先倒 (15分)
import java.util.Scanner;
//2
public class Main1019 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int n = sc.nextInt();
int A = a;
int B = b;
for (int i = 0; i < n; i++) {
int ah = sc.nextInt();
int as = sc.nextInt();
int bh = sc.nextInt();
int bs = sc.nextInt();
if (ah + bh == as)
if (ah + bh == bs)
continue;
else {
a--;
}
if (ah + bh == bs)
b--;
if (a < 0) {
System.out.println("A");
System.out.println(B - b);
break;
}
if (b < 0) {
System.out.println("B");
System.out.println(A - a);
break;
}
}
}
}
L1-020 帅到没朋友 (20分)(未AC)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//3
//第三个测试点是当k=1时,后面跟的人不应该属于有朋友
//第四个测试点的人标号标号开头为0。如(00001),如果转换成int 会变成1 所以容易错
public class Main1020 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
String[] arr = new String[100000];
boolean flag = false;
for (int i = 0; i < n; i++) {
String[] temp = br.readLine().split(" ");
if (temp[0].equals("1"))
continue;
for (int j = 1; j < temp.length; j++) {
arr[Integer.valueOf(temp[j])] = "1";
}
}
br.readLine();// m数据没用上,空读扔掉
String[] temp = br.readLine().split(" ");
/*for (String t : temp) {
int tt = Integer.valueOf(t);
if (arr[tt] == null) {
arr[tt] = "1";// 打印过后改为1,以避免重复出现
if (b)
System.out.print(" ");
System.out.print(t);
b = true;// 处理空格问题,打印第一个不加空格,后面加数据就得要在前面加空格
flag = false;// 如果打印过,则出现了没朋友的人,最后一句就不用打印
}
}*/
StringBuilder sb = new StringBuilder();
for (String t : temp) {
int tt = Integer.valueOf(t);
if (arr[tt] == null) {
arr[tt] = "1";// 打印过后改为1,以避免重复出现
if (flag)
sb.append(" ");
sb.append(t);
flag = true;// 如果打印过,则出现了没朋友的人,最后一句就不用打印,顺便处理空格问题
}
}
if (!flag)
System.out.println("No one is handsome");
else
System.out.println(sb.toString());
}
}
L1-021 重要的话说三遍 (5分)
import java.util.Scanner;
//0
public class Main1021 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("I'm gonna WIN!");
System.out.println("I'm gonna WIN!");
System.out.println("I'm gonna WIN!");
}
}
L1-022 奇偶分家 (10分)
import java.util.Scanner;
//1
public class Main1022 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int j = 0;
int o = 0;
for (int i = 0; i < n; i++)
if (sc.nextInt() % 2 == 0)
o++;
else
j++;
System.out.println(j + " " + o);
}
}
L1-023 输出GPLT (20分)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//2
public class Main1023 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] chars = br.readLine().toCharArray();
int g, p, l, t;
g = p = l = t = 0;
for (char c : chars) {
if (c == 'g' | c == 'G') {
g++;
continue;
}
if (c == 'p' | c == 'P') {
p++;
continue;
}
if (c == 't' | c == 'T') {
t++;
continue;
}
if (c == 'l' | c == 'L') {
l++;
continue;
}
}
while (g > 0 | p > 0 | t > 0 | l > 0) {
if (g > 0) {
System.out.print("G");
g--;
}
if (p > 0) {
System.out.print("P");
p--;
}
if (l > 0) {
System.out.print("L");
l--;
}
if (t > 0) {
System.out.print("T");
t--;
}
}
}
}
L1-024 后天 (5分)
import java.util.Scanner;
//1
public class Main1024 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h = (sc.nextInt() + 2) % 7;
System.out.println(h == 0 ? 7 : h);
}
}
L1-025 正整数A+B (15分)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String num[]=sc.nextLine().split(" ");
int a=0,b=0;
int flaga=1;
int flagb=1;
if(num.length>2){
flagb=0;
}
try{
a=Integer.parseInt(num[0]);
if(a<1||a>1000){
flaga=0;
}
}catch(Exception e){
flaga=0;
}
try{
b=Integer.parseInt(num[num.length-1]);
if(b<1||b>1000){
flagb=0;
}
}catch(Exception e){
flagb=0;
}
if(flaga==0){
System.out.print("? + ");
}else{
System.out.print(a +" + ");
}
if(flagb==0){
System.out.print("? = ");
}else{
System.out.print(b +" = ");
}
if(flaga==0||flagb==0){
System.out.print("?");
}else{
System.out.print(a+b);
}
}
}
L1-026 I Love GPLT (5分)
public class Main1026 {
public static void main(String[] args) {
System.out.println("I");
System.out.println(" ");
System.out.println("L");
System.out.println("o");
System.out.println("v");
System.out.println("e");
System.out.println(" ");
System.out.println("G");
System.out.println("P");
System.out.println("L");
System.out.println("T");
}
}
L1-027 出租 (20分)
import java.util.Scanner;
public class Main1027 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] chars = sc.nextLine().toCharArray();
int[] t = new int[10];
for(int i = 0;i < chars.length;i++) {
if(t[chars[i] - '0'] == 0) {
t[chars[i] - '0'] = 1;
}
}
String arr = "";
for(int i = 9;i >= 0;i--) {
if(t[i] == 1) {
arr += i;
}
}
System.out.print("int[] arr = new int[]{");
for(int i = 0;i < arr.length();i++) {
if(i == arr.length()-1) {
System.out.println(arr.charAt(i) + "};");
break;
}
System.out.print(arr.charAt(i) + ",");
}
System.out.print("int[] index = new int[]{");
for(int i = 0;i < 11;i++) {
if(i == 10) {
System.out.println(arr.indexOf(chars[i]) + "};");
break;
}
System.out.print(arr.indexOf(chars[i]) + "," );
}
sc.close();
}
}
L1-028 判断素数 (10分)
import java.util.Scanner;
// 2
public class Main1028 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
if (isSS(temp))
System.out.println("Yes");
else
System.out.println("No");
}
}
public static boolean isSS(int number){
if( number <= 1)
return false;
int max = (int)Math.sqrt(number) + 1;
for (int i = 2; i < max; i++) {
if(number % i == 0)
return false;
}
return true;
}
}
L1-029 是不是太胖了 (5分)
import java.util.Scanner;
//1
public class Main1029 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.printf("%.1f", (a - 100) * 0.9 * 2);
}
}
L1-030 一帮一 (15分)
import java.util.Scanner;
//3
public class Main1030 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[][] res = new String[n / 2][2];
int c = 0;
for (int i = 0; i < n; i++) {
String flge = sc.next();
String name = sc.next();
if (c < n / 2) {//前一半的人成绩好当队长,队伍第二的位置存队长性别
res[c][0] = name;
res[c++][1] = flge;
} else {
for (int j = n / 2 - 1; j >= 0; j--) {//队员从队伍倒着遍历,是异性就入队
if((res[j][1].equals("0") || res[j][1].equals("1")) && !res[j][1].equals(flge)){
res[j][1] = name;
break;
}
}
}
}
for (int i = 0; i < n / 2; i++) {
System.out.println(res[i][0] + " " + res[i][1]);
}
}
}
L1-031 到底是不是太胖了 (10分)
import java.util.Scanner;
//1
public class Main1031 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int high = sc.nextInt();
int weight = sc.nextInt();
double standard = (high - 100)*0.9 * 2;//计算标准市斤体重
if(Math.abs(weight - standard) < standard *0.1)//未考虑相等情况
System.out.println("You are wan mei!");//打印中式英语
else if(weight > standard)
System.out.println("You are tai pang le!");
else
System.out.println("You are tai shou le!");
}
}
}
L1-032 Left-pad (20分)
import java.util.Scanner;
//2
public class Main1032 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] line1 = sc.nextLine().split(" ");//这里用 行读取保证第二行字符串可以顺利使用nextLine
int n = Integer.parseInt(line1[0]);
char ch = line1[1].charAt(0);
String oldStr = sc.nextLine();
if (oldStr.length() == n)
System.out.println(oldStr);
if(oldStr.length() < n ){
for(int i = 0;i < n-oldStr.length();i++)
System.out.print(ch);
System.out.println(oldStr);
}
if(oldStr.length() > n){
System.out.println(oldStr.substring(oldStr.length()-n));
}
}
}
L1-033 出生年 (15分)
import java.util.HashMap;
import java.util.Scanner;
//3
public class Main1033 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int n = sc.nextInt();
int c = 0;
while (true) {//每年遍历,满足条件就跳出
String temp = "";
int len = (year + "").length();
for (int i = len; i < 4; i++) {//年份补0
temp += "0";
}
temp += year;
HashMap<Character, Integer> hm = new HashMap<>();
for (int i = 0; i < 4; i++) {
if (!hm.containsKey(temp.charAt(i)))//如果没有这个字符就添加该字符
hm.put(temp.charAt(i), 1);//添加了一次就多一个不同的数(第二个参数1是随便取得,无意义)
}
if (hm.size() == n) {
System.out.println(c + " " + temp);
break;
}
c++;
year++;
}
}
}
L1-034 点赞 (20分)
import java.io.*;
//3 (卡java时间)
public class Main1034 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException {//用nextInt代替 sc.nextInt
in.nextToken();
return (int)in.nval;
}
public static void main(String[] args) throws IOException {
// Scanner sc = new Scanner(System.in);
int n = nextInt();
int[] ints = new int[1001];//编号作为下标,每个元素记录数量
for (int i = 0; i < n; i++) {
int k = nextInt();
for (int j = 0; j < k; j++) {
int index = nextInt();
ints[index]++;
}
}
int max = 0;
int res = 0;
for (int i = 1; i < ints.length; i++) {
if(ints[i] >= max){//这里用大于等于,确保最大值相同时取较大者
res = i;
max = ints[i];
}
}
System.out.println(res + " " + max);
}
}
L1-035 情人节 (15分)
import java.util.Scanner;
//1
public class Main1035 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c = 0;
String s1 = "";
String s2 = "";
while (true){
String temp = sc.next();
if(temp.equals("."))
break;
c++;
if(c == 2)
s1 = temp;
if(c == 14)
s2 = temp;
}
if( c < 2)
System.out.println("Momo... No one is for you ...");
else if(c < 12)
System.out.println(s1 + " is the only one for you...");
else
System.out.println(s1 + " and " + s2 + " are inviting you to dinner...");
}
}
L1-036 A乘以B (5分)
import java.util.Scanner;
//0
public class Main1036 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt() * sc.nextInt());
}
}
L1-037 A除以B (10分)
import java.util.Scanner;
//1
public class Main1037 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
if(b == 0)
System.out.printf("%d/0=Error\n",a);
else if(b < 0)
System.out.printf("%d/(%d)=%.2f",a,b,(a*1.0/b));
else
System.out.printf("%d/%d=%.2f",a,b,(a*1.0/b));
}
}
L1-038 新世界 (5 分)
//0
public class Main {
public static void main(String[] args) {
System.out.print("Hello World\n" +
"Hello New World\n");
}
}
L1-039 古风排版 (20 分)
import java.util.Scanner;
//3
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String[] res = new String[n];
for (int i = 0; i < res.length; i++) {
res[i] = "";
}
char[] arr = sc.nextLine().toCharArray();
for (int i = arr.length-1; i >= 0; i--) {
//倒序循环插入对应行
res[i%n] += arr[i];
}
//计算每一行的列数
int w = arr.length / n;
w += arr.length%n == 0?0:1;
for (int i = 0; i < res.length; i++) {
//插入变量w,构造格式化字符串,默认右对齐
System.out.printf("%" + w + "s\n",res[i]);
}
}
}
L1-040 最佳情侣身高差 (10 分)
//1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String str = sc.next();
if(str.equals("M"))
System.out.printf("%.2f\n",sc.nextDouble()/1.09);
else
System.out.printf("%.2f\n",sc.nextDouble()*1.09);
}
}
}
L1-041 寻找250 (10 分)
import java.util.Scanner;
//1
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c = 0;
//每次循环前增加次数,找到后就跳出循环
while (++c>0 && sc.nextInt() != 250){}
System.out.println(c);
}
}
L1-042 日期格式化 (5 分)
import java.util.Scanner;
//1
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] s = sc.nextLine().split("-");
System.out.println(s[2] + "-" + s[0] + "-" + s[1]);
}
}
L1-043 阅览室 (20 分)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//3
public class Main {
public static void main(String[] args) throws IOException {
//Scanner会超时
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
int[] books;
int[] flag;
for (int i = 0; i < n; i++) {
books = new int[1001];
flag = new int[1001];//最好是多设一个flag数组,若想用books等于0代替flag,会遇上开始时间正好是0的情况(应该是测试点3,4)
int c = 0;
int sumtime = 0;
while (true) {
String[] temp = br.readLine().split(" ");
int num = Integer.valueOf(temp[0]);
String command = temp[1];
int time = toMin(temp[2]);
if (num == 0) break; //一天结束
if (command.equals("S")) {//只要是S就覆盖原来的数据(SSE取第二个S)
books[num] = time;
flag[num] = 1;
} else {
if (flag[num] == 1) {//排除只有E没有S的情况
c++;
sumtime += time - books[num];
books[num] = 0;//排除有SEE的情况
flag[num] = 0;
}
}
}
//三元运算防止除零异常,+0.5后转型为了四舍五入
System.out.printf("%d %d\n", c, c == 0 ? 0 : (int) (sumtime * 1.0 / c + 0.5));
}
}
//将字符串转成分钟数
public static int toMin(String str) {
String[] arr = str.split(":");
return Integer.valueOf(arr[0]) * 60 + Integer.valueOf(arr[1]);
}
}
L1-044 稳赢 (15 分)
import java.io.*;
//3
public class Main {
//快速输入
//提交多次 偶尔能不超时
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int k = nextInt() + 1;
String str;
int i = 0;
while (!(str = next()).equals("End")) {
if (++i % k == 0) {
System.out.println(str);//平局
} else {
switch (str) {
case "ChuiZi":
System.out.println("Bu");
break;
case "Bu":
System.out.println("JianDao");
break;
default:
System.out.println("ChuiZi");
}
}
}
}
}
L1-045 宇宙无敌大招呼 (5 分)
import java.util.Scanner;
//0
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Hello " + sc.next());
}
}
L1-046 整除光棍 (20 分)
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger n = BigInteger.valueOf(sc.nextInt());
StringBuilder num = new StringBuilder("1");
BigInteger numB = new BigInteger(num.toString());
int c = 1;
while (!numB.mod(n).toString().equals("0")){
num.append("1");
numB = new BigInteger(num.toString());
c++;
}
System.out.println(numB.divide(n).toString() + " " + c);
}
}
L1-047 装睡 (10 分)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
String[] arr = sc.nextLine().split(" ");
if(Integer.valueOf(arr[1]) > 20 |Integer.valueOf(arr[1]) < 15 |Integer.valueOf(arr[2]) > 70 |Integer.valueOf(arr[2]) < 50)
System.out.println(arr[0]);
}
}
}
L1-048 矩阵A乘以B (15 分)
import java.io.*;
//2
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws IOException {
int r1 = nextInt();
int c1 = nextInt();
int[][] arr1 = new int[r1][c1];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
arr1[i][j] = nextInt();
}
}
int r2 = nextInt();
int c2 = nextInt();
int[][] arr2 = new int[r2][c2];
int[][] res = new int[r1][c2];
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
arr2[i][j] = nextInt();
}
}
if(c1 != r2){
out.printf("Error: %d != %d",c1,r2);
}else {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
res[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
out.println(r1 + " " + c2);
for (int i = 0; i < r1; i++) {
out.print(res[i][0]);
for (int j = 1; j < c2; j++) {
out.print(" " + res[i][j]);
}
out.println();
}
}
out.flush();
}
}
L1-049 天梯赛座位分配 (20 分)
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
//4
public class Main {
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer>[] arr = new ArrayList[n];
for (int i = 0; i < n; i++) {
arr[i] = new ArrayList<>();
}
int[] max = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
int t = sc.nextInt() * 10;
sum += t;
max[i] = t;
}
int SchoolIndex = 0;
int id = 1;
int flag = -1;
for (int i = 0; i < sum; i++) {
if (flag == SchoolIndex) {
id++;
}
arr[SchoolIndex].add(id++);
flag = SchoolIndex;
SchoolIndex = (SchoolIndex + 1) % n;
while (i != sum - 1 && arr[SchoolIndex].size() >= max[SchoolIndex]) {
SchoolIndex = (SchoolIndex + 1) % n;
}
}
int c = 0;
for (int i = 0; i < n; i++) {
out.println("#" + (i + 1));
for (int j = 0; j < max[i]; j++) {
if (j % 10 != 9)
out.print(arr[i].get(j) + " ");
else
out.println(arr[i].get(j));
}
}
out.flush();
}
}
L1-050 倒数第N个字符串 (15 分)
import java.util.Scanner;
// 3
// 26进制转换
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int n = sc.nextInt();
int max = (int) Math.pow(26, l);
int count = max - n;
char[] list = new char[26];
for (int i = 0; i < list.length; i++) {
list[i] = (char) ('a' + i);
}
for (int i = 0; i < l; i++) {
int t = (int)Math.pow(26,l-i-1);
System.out.print(list[count / t % 26]);
}
}
}
L1-051 打折 (5 分)
import java.util.Scanner;
//0
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.printf("%.2f", sc.nextInt() / 10.0 * sc.nextInt());
}
}
L1-052 2018我们要赢 (5 分)
import java.util.Scanner;
//0
public class Main {
public static void main(String[] args) {
System.out.print("2018\n" +
"wo3 men2 yao4 ying2 !\n");
}
}
L1-053 电子汪 (10 分)
import java.util.Scanner;
//1
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
for (int i = 0; i < a + b; i++) {
System.out.print("Wang!");
}
}
}
L1-054 福到了 (15 分)
import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher;
//3
// 题意 “颠倒” 不明确,输出需要竖直方向和水平方向都颠倒
// 而“bu yong dao le” 的判断条件只有竖直方向
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String[] temp = br.readLine().split(" ");
String str = temp[0];
int n = Integer.valueOf(temp[1]);
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
char[] s = br.readLine().toCharArray();
int l = 0,r= s.length-1;
while (l < r){
char tmp = s[l];
s[l] = s[r];
s[r] = tmp;
l++;
r--;
}
arr[i] = String.valueOf(s).replaceAll("@", Matcher.quoteReplacement(str));
}
int l = 0, r = n - 1;
boolean flag = true;
while (l <= r) {
if (arr[l].equals(arr[r])) {
flag = true;
} else {
flag = false;
break;
}
l++;
r--;
}
if (flag) {
out.println("bu yong dao le");
}
for (int i = n - 1; i >= 0; i--) {
out.println(arr[i]);
}
out.flush();
}
}
L1-055 谁是赢家 (10 分)
import java.util.Scanner;
// 2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int ap = 0;
int bp = 0;
for (int i = 0; i < 3; i++) {
if (sc.nextInt() == 0) {
ap++;
} else
bp++;
}
if (ap == 3) System.out.println("The winner is a: " + a + " + 3");
else if (bp == 3) System.out.println("The winner is b: " + b + " + 3");
else if(a > b)System.out.println("The winner is a: " + a + " + " + ap);
else System.out.println("The winner is b: " + a + " + " + bp);
}
}
L1-056 猜数字 (20 分)
import java.io.*;
//2
//注意是和平均数的一半比较
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws IOException {
int n = nextInt();
String[] names = new String[n];
int[] ints = new int[n];
String[] temp;
int sum = 0;
for (int i = 0; i < n; i++) {
names[i] = next();
int t = nextInt();
sum += t;
ints[i] = t;
}
double v = sum * 1.0 / n / 2;
double min = 101;
int mini = -1;
for (int i = 0; i < n; i++) {
if (Math.abs(ints[i] - v) < min) {
min = Math.abs(ints[i] - v);
mini = i;
}
}
System.out.println((int) v + " " + names[mini]);
}
}
L1-057 PTA使我精神焕发 (5 分)
//0
public class Main {
public static void main(String[] args) {
System.out.println("PTA shi3 wo3 jing1 shen2 huan4 fa1 !");
}
}
L1-058 6翻了 (15 分)
import java.io.*;
//3
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
System.out.println(p(str));
}
private static String p(String s) {
if (s.contains("6666")) {
int i = s.indexOf("6666");
int l = i;
int c = 0;
while(i < s.length() && s.charAt(i) == '6' ){
c++;
i++;
}
if(c > 9){
s = s.replaceFirst(s.substring(l,l+c), "27");
}else
s = s.replaceFirst(s.substring(l,l+c),"9");
}else return s;
return p(s);
}
}
L1-059 敲笨钟 (20 分)
import java.io.*;
public class Main {
// 3
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
for (int i = 0; i < n; i++) {
String[] temp = br.readLine().split(",");
String front = temp[0];
//考虑上半句的字数情况
if(temp[0].length() >= 3)
front = temp[0].substring(temp[0].length()-3);
String next = temp[1].substring(temp[1].length()-4,temp[1].length()-1);
if(front.equals("ong") && next.equals("ong")){
out.print(temp[0]+",");
String[] t2 = temp[1].split(" ");
for (int j = 1; j < t2.length-3; j++) {
out.print(" " + t2[j]);
}
out.println(" qiao ben zhong.");
}else out.println("Skipped");
}
out.flush();
}
}
L1-060 心理阴影面积 (5 分)
import java.util.Scanner;
//2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(5000 - 50 * (100 - sc.nextInt() + sc.nextInt()));
}
}
L1-061 新胖子公式 (10 分)
import java.util.Scanner;
// 1
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double h = sc.nextDouble();
double w = sc.nextDouble();
double res = h / w/w;
if (res > 25){
System.out.printf("%.1f\n",res);
System.out.println("PANG");
}else {
System.out.printf("%.1f\n",res);
System.out.println("Hai Xing");
}
}
}
L1-062 幸运彩票 (15 分)
import java.util.Scanner;
//2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
char[] x = sc.next().toCharArray();
int sum1 = getSum(x, 0, 2);
int sum2 = getSum(x, 3, 5);
if (sum1 == sum2) System.out.println("You are lucky!");
else System.out.println("Wish you good luck.");
}
}
private static int getSum(char[] x, int l, int r) {
int sum = 0;
for (int j = l; j <= r; j++) {
sum += x[j] - '0';
}
return sum;
}
}
L1-063 吃鱼还是吃肉 (10 分)
import java.util.Scanner;
//L1-063 吃鱼还是吃肉 (10 分)
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int h = sc.nextInt();
int w = sc.nextInt();
solve(x, h, w);
}
}
// 如果太矮了,输出:duo chi yu!(多吃鱼);
// 如果太瘦了,输出:duo chi rou!(多吃肉);
// 如果正标准,输出:wan mei!(完美);
// 如果太高了,输出:ni li hai!(你厉害);
// 如果太胖了,输出:shao chi rou!(少吃肉)。
private static void solve(int x, int h, int w) {
int hs = 130;
int ws = 27;
if (x == 0) {
hs = 129;
ws = 25;
}
// 女宝宝的标准身高为 129 厘米、标准体重为 25 公斤。
if (h < hs) {
System.out.print("duo chi yu! ");
} else if (h > hs) {
System.out.print("ni li hai! ");
} else {
System.out.print("wan mei! ");
}
if (w < ws) {
System.out.println("duo chi rou!");
} else if (w > ws) {
System.out.println("shao chi rou!");
} else {
System.out.println("wan mei!");
}
}
}
L1-064 估值一亿的AI核心代码(未AC)
import java.util.ArrayList;
import java.util.Scanner;
//4
//L1-064 估值一亿的AI核心代码 (20 分)
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
String str = sc.nextLine();
solve(str);
}
}
private static void solve(String str) {
// 无论用户说什么,首先把对方说的话在一行中原样打印出来;
System.out.println(str);
// 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
String AIStr = remove_blank(str);
// 把原文中所有大写英文字母变成小写,除了 I;
AIStr = toLow(AIStr);
// 把原文中所有独立的 I 和 me 换成 you;
// 暂时设置为*you* 防止can I 变成 can you ,被后续程序误解
AIStr = AIStr.replaceAll("\\bI\\b", "* you *");
AIStr = AIStr.replaceAll("\\bme\\b", "* you *");
// 把原文中所有独立的 can you、could you 对应地换成 I can、I could—— 这里“独立”是指被空格或标点符号分隔开的单词;
AIStr = AIStr.replaceAll("\\bcan you\\b", "I can");
AIStr = AIStr.replaceAll("\\bcould you\\b", "I could");
// 把 *you* 变回 you
AIStr = AIStr.replaceAll("\\* you \\*", "you");
// 把原文中所有的问号 ? 换成惊叹号 !;
AIStr = AIStr.replaceAll("\\?", "!");
// 在一行中输出替换后的句子作为 AI 的回答。
System.out.printf("AI: %s\n", AIStr);
}
private static String toLow(String aiStr) {
char[] arr = aiStr.toCharArray();
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (c >= 'A' && c <= 'Z' && c != 'I') {
arr[i] = (char) (c - 'A' + 'a');
}
}
return new String(arr);
}
private static String remove_blank(String str) {
ArrayList<String> list = new ArrayList<>();
String[] arr = str.split(" ");
for (int i = 0; i < arr.length; i++) {
if (arr[i].length() != 0) {
list.add(arr[i]);
}
}
StringBuilder sb = new StringBuilder();
sb.append(list.get(0));
for (int i = 1; i < list.size(); i++) {
sb.append(" ");
sb.append(list.get(i));
}
//删除标点前的空格
for (int i = 0; i < sb.length(); i++) {
char c = sb.charAt(i);
if (c >= '0' && c <= '9') continue;
if (c >= 'a' && c <= 'z') continue;
if (c >= 'A' && c <= 'Z') continue;
if (c == ' ') continue;
if (i > 0 && sb.charAt(i - 1) == ' ') {
sb.deleteCharAt(i - 1);
}
}
return sb.toString();
}
}
L1-065 嫑废话上代码
// 0
public class Main {
public static void main(String[] args) {
System.out.println("Talk is cheap. Show me the code.");
}
}
L1-066 猫是液体
import java.util.Scanner;
// 1
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt()*sc.nextInt()*sc.nextInt());
}
}
L1-067 洛希极限
import java.util.Scanner;
// 2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double p = sc.nextDouble();
int w = sc.nextInt();
double r = sc.nextDouble();
if(w == 0) p*= 2.455;
else p *= 1.26;
System.out.printf("%.2f %s",p,(p < r?"^_^":"T_T"));
}
}
L1-068 调和平均
import java.util.Scanner;
/**
* @Author: Weizhi
* @Date: create in 2022/3/16 20:23
* @Description:
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double sum = 0;
for (int i = 0; i < n; i++) {
sum += 1.0/sc.nextDouble();
}
System.out.printf("%.2f",n/sum);
}
}
L2-003 月饼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
// Cake类必须定义为静态内部类,不然会超时,有时即使是静态内部类也得多提交几次才不超时
static class Cake implements Comparable {
double price;
double count;
@Override
public int compareTo(Object o) {
Cake c = (Cake) o;
if (price > c.price) return -1;
return 1;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split(" ");
int n = Integer.valueOf(temp[0]);
double max = Double.valueOf(temp[1]);
Cake[] arr = new Cake[n];
temp = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = new Cake();
arr[i].count = Double.valueOf(temp[i]);
}
temp = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i].price = Double.valueOf(temp[i]) * 1.0 / arr[i].count;
}
Arrays.sort(arr);
double sum = 0;
for (int i = 0; i < n; i++) {
if (max == 0) break;
if (max >= arr[i].count) {
sum += arr[i].price * arr[i].count;
max -= arr[i].count;
} else {
sum += arr[i].price * max;
break;
}
}
System.out.printf("%.2f", sum);
}
}
L2-005 集合相似度
import java.io.*;
import java.util.HashSet;
// 3 运气好的时候能满分过
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static void main(String[] args) throws IOException {
int n = nextInt();
HashSet<Integer>[] arr = new HashSet[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = new HashSet<>();
int k = nextInt();
for (int j = 0; j < k; j++) {
arr[i].add(nextInt());
}
}
int k = nextInt();
for (int i = 0; i < k; i++) {
int x = nextInt()-1;
int y = nextInt()-1;
int con = 0;
int sum = arr[x].size();
for (Integer item : arr[y]) {
if(arr[x].contains(item)){ // 交集
con++;
}else{ //并集
sum++;
}
}
out.printf("%.2f%%\n",100.0*con/sum);
}
out.flush();
}
}
L2-006 树的遍历
import java.io.*;
import java.util.LinkedList;
// 3
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static int[] hou; // 2 3 1 5 7 6 4
static int[] zhong; // 1 2 3 4 5 6 7
/*
4
1 6
3 5 7
2
*/
static class Node {
Node l, r;
int v;
public Node(int v) {
this.v = v;
}
}
static LinkedList<Node> q = new LinkedList<Node>();
public static void main(String[] args) throws IOException {
int n = nextInt();
hou = new int[n];
zhong = new int[n];
for (int i = 0; i < n; i++) {
hou[i] = nextInt();
}
for (int i = 0; i < n; i++) {
zhong[i] = nextInt();
}
Node node = f(0, n - 1, 0, n - 1);
System.out.print(node.v);
if(node.l!=null){q.add(node.l);}
if(node.r!=null){q.add(node.r);}
show();
}
private static void show() {
while(!q.isEmpty()){
Node node = q.poll();
System.out.print(" " + node.v);
if(node.l!=null){q.add(node.l);}
if(node.r!=null){q.add(node.r);}
}
}
private static Node f(int lz, int rz, int lh, int rh) {
if (lz > rz) {
return null;
}
Node node = new Node(hou[rh]);
int mi = 0;
for (int i = lz; i <= rz; i++) {
if (hou[rh] == zhong[i]) {
mi = i;
break;
}
}
node.l = f(lz, mi - 1, lh, lh + (mi - lz - 1));
node.r = f(mi + 1, rz, lh + (mi - lz), rh - 1);
return node;
}
}
L2-007 家庭房产 (25分)(未AC)
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
//4
public class Main2007 {
static Person[] arr;
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt() throws IOException {
in.nextToken();
return (int)in.nval;
}
static double nextDouble() throws IOException {
in.nextToken();
return in.nval;
}
public static int find(int id) {
if (arr[id].next == id)
return id;
return find(arr[id].next);
}
public static final void union(int id1, int id2) {
int f1 = find(id1);
int f2 = find(id2);
if (f1 == f2)
return;
if (f1 < f2) {
arr[f2].next = f1;
//大家庭合并,总人数、房产、面积合并
arr[f1].count += arr[f2].count;
arr[f1].house += arr[f2].house;
arr[f1].area += arr[f2].area;
} else {
arr[f1].next = f2;
arr[f2].count += arr[f1].count;
arr[f2].house += arr[f1].house;
arr[f2].area += arr[f1].area;
}
}
public static void main(String[] args) throws IOException {
// Scanner sc = new Scanner(System.in);
int n = nextInt();
arr = new Person[10000];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Person(i);
}
for (int i = 0; i < n; i++) {
int self = nextInt();
arr[self].flag = 1;
int father = nextInt();
int mather = nextInt();
if (father != -1) {
union(self, father); //家庭加入父亲
arr[father].flag = 1;
}
if (mather != -1) {
union(self, mather); //家庭加入母亲
arr[mather].flag = 1;
}
int k = nextInt();
for (int j = 0; j < k; j++) {
int kid = nextInt();
arr[kid].flag = 1;
union(self, kid);//家庭加入所有孩子
}
arr[find(self)].house += nextInt();
arr[find(self)].area += nextDouble();
}
Arrays.sort(arr);//快排
int c = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i].next != arr[i].id)
break;
c++;
}
out.println(c);
for (int i = 0; i < c; i++) {
out.print(arr[i]);
}
out.flush();//刷新所有内容
}
}
class Person implements Comparable {
int id;
int count = 1;//人数,默认自己算一个人
int house;//房产数
double area;
int next;
int flag;// flag为1 的说明真有此人
Person() {
}
Person(int i) {
id = i;
next = i;
}
@Override
public int compareTo(Object o) {//自定义比较
Person p = (Person) o;
// if(id == next && p.id != p.next)
// return 1;
if (area / count > p.area / p.count) {
// System.out.println(area / count);
return -1;
} else if (area / count < p.area / p.count) {
return 1;
} else {
if (id < p.id)
return -1;
else return 1;
}
}
@Override
public String toString() {
if (id != next)
return "";
DecimalFormat df = new DecimalFormat("0000"); // 字符串长度<=4时带前导零,>4时照常显示
String Sid = df.format(id);
String Shouse = String.format("%.3f", house * 1.0 / count);
String Sarea = String.format("%.3f", area * 1.0 / count);
return Sid + " " + count + " " + Shouse + " " + Sarea + "\n";
}
}
L2-008 最长对称子串
import java.io.*;
//3
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] arr = br.readLine().toCharArray();
// 偶数
int max1 = 0;
for (int i = 0; i < arr.length - 1; i++) {
int l = i;
int r = i + 1;
while (l >= 0 && r < arr.length && arr[l] == arr[r]){l--;r++;}
if(arr[++l]==arr[--r] &&(r-l+1)>max1){
max1 = r-l+1;
}
}
// 奇数
int max2 = 1;
for (int i = 1; i < arr.length-1; i++) {
int l = i-1;
int r = i+1;
while (l >= 0 && r < arr.length && arr[l] == arr[r]){l--;r++;}
if(arr[++l]==arr[--r] &&(r-l+1)>max1){
max2 = r-l+1;
}
}
System.out.println(Math.max(max1,max2));
}
}
L2-009 抢红包(未AC)
import java.io.*;
import java.util.Arrays;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws IOException {
int n = nextInt();
P[] p = new P[n];
for(int i = 0;i < n;i++){
p[i] = new P();
p[i].id = i;
}
for(int i = 0;i < n;i++){
int k = nextInt();
for(int j = 0;j < k;j++){
int id = nextInt()-1;
int money = nextInt();
p[i].money -= money;
p[id].money += money;
p[id].count++;
}
}
Arrays.parallelSort(p);
for(int i = 0;i < n;i++){
out.println(p[i].id+1 + " " + String.format("%.2f",p[i].money * 1.0/100));
out.flush();
}
}
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt() throws IOException {
in.nextToken();
return (int)in.nval;
}
static class P implements Comparable{
int money = 0;
int count = 0;
int id = 0;
boolean con(P p){
if(money > p.money)
return true;
else if(money == p.money)
if(count > p.count)
return true;
else if(count == p.count)
if(id < p.id)
return true;
return false;
}
@Override
public int compareTo(Object o) {
P p = (P)o;
if(money > p.money)
return -1;
else if (money < p.money)
return 1;
if(count > p.count)
return -1;
else if(count < p.count)
return 1;
if(id < p.id)
return -1;
return 1;
}
}
}
L2-015 互评成绩
import java.io.*;
import java.util.Arrays;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws IOException {
int n = nextInt();
int k = nextInt();
int m = nextInt();
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
int min = 101;
int max = -1;
int sum = 0;
for (int j = 0; j < k; j++) {
int t = nextInt();
sum += t;
if (t > max) max = t;
if (t < min) min = t;
}
arr[i] = (sum-max-min) * 1.0 / (k-2);
}
Arrays.sort(arr);
for (int i = n-m; i <= n-2; i++) {
System.out.printf("%.3f ",arr[i]);
}
System.out.printf("%.3f\n",arr[n-1]);
}
}

2262

被折叠的 条评论
为什么被折叠?



