PTA|团体程序设计天梯赛-练习集|JAVA版
题目地址
网上大部分的参考代码都是c版的,所以我打算做一个java版
目前是打算全做,如果有题目被卡了就先跳过了。
PS:代码开头标记的数字,是我自己认为的难度,仅供参考
//0
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
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);
}
}
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]);
}
}
}
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);
}
}
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])]);
}
}
}
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]);
}
}
}
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']);//处理多余的空格问题
}
}
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);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//4
//时间限制400ms,其他测试点都80ms过的,测试点3还是报超时了,不晓得为啥
//我感觉已经很优化了,如果有大佬java过了,希望可以指点一下
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
boolean b = true;
long[][] ints = new long[n][2];
long sum = 0;
long fm = 1, fz, zs;
String[] in = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
String[] temp = in[i].split("/");
ints[i][0] = Long.valueOf(temp[0]);
ints[i][1] = Long.valueOf(temp[1]);
if(fm % ints[i][1] != 0) {//防止分母全乘导致超出long的范围
fm *= ints[i][1];
}
}
for (int i = 0; i < n; i++) {
sum += ints[i][0] * fm / ints[i][1];//防止分母全乘导致超出long的范围,的分子和的处理
}
zs = sum / fm;
fz = sum % fm;
if (sum < 0) {
b = false;
zs = Math.abs(zs);
fz = Math.abs(fz);
}
for (int i = 2; i <= fz; i++) {
if (fz % i == 0 && fm % i == 0) {
fz /= i;
fm /= i;
}
}
if (b)//判断各种特殊情况,基本上一种情况一个测试点
if (zs != 0 & fz != 0) {
System.out.println(zs + " " + fz + "/" + fm);
} else if (zs != 0 & fz == 0)
System.out.println(zs);
else if (zs == 0 & fz != 0)
System.out.println(fz + "/" + fm);
else
System.out.println(0);
else if (zs > 0 & fz != 0) {
System.out.println( "-" +zs + " " + fz + "/" + fm);
} else if (zs > 0 & fz == 0)
System.out.println("-" + zs);
else
System.out.println("-" + fz + "/" + fm);
}
}
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 ) );
}
}