diff --git a/EXP/Exp06/README.md b/EXP/Exp06/README.md index 6f15081c387605dc8649a6973540a6fc5998bb57..f89e0ce8c0963d558315dc328284fefbc4ea77c3 100644 --- a/EXP/Exp06/README.md +++ b/EXP/Exp06/README.md @@ -1,11 +1,11 @@ ## 实验6作业代码 ### 题目详情 - A Bank Account Class - - [Account.java](./Exp/Exp06/Problem01/Account.java) - - [ManageAccounts](./Exp/Exp06/Problem01/ManageAccounts.java) + - [Account.java](./Problem01/Account.java) + - [ManageAccounts](./Problem01/ManageAccounts.java) - Representing Names - - [Name.java](./Exp/Exp06/Problem02/Name.java) - - [TestNamesName.java](./Exp/Exp06/Problem02/TestNames.java) + - [Name.java](./Problem02/Name.java) + - [TestNamesName.java](./Problem02/TestNames.java) ### 讨论和建议 如果大家在查看过程中发现错误或不足之处,欢迎大家在[Issues](https://gitee.com/Samler/java-work/issues)中发表自己的见解,也欢迎大家在[Issues](https://gitee.com/Samler/java-work/issues)中进行讨论。 diff --git a/PTA/ProblemSet02/Problem02/Main02.java b/PTA/ProblemSet02/Problem02/Main.java similarity index 88% rename from PTA/ProblemSet02/Problem02/Main02.java rename to PTA/ProblemSet02/Problem02/Main.java index 0a63f2a484df3f16c80a63c26bf5721c56170ec9..5a94f1ed2135fff938897be19455e1e5df58b14c 100644 --- a/PTA/ProblemSet02/Problem02/Main02.java +++ b/PTA/ProblemSet02/Problem02/Main.java @@ -2,7 +2,7 @@ package ProblemSet02.Problem02; import java.util.Scanner; -public class Main02 { +public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); diff --git a/PTA/ProblemSet03/Problem04/Main04.java b/PTA/ProblemSet03/Problem04/Main.java similarity index 94% rename from PTA/ProblemSet03/Problem04/Main04.java rename to PTA/ProblemSet03/Problem04/Main.java index 773152d6d570eff573ea06d21cbf7d7f34b2267c..85f64769de4311fefa46f90eddf61813c83b14f1 100644 --- a/PTA/ProblemSet03/Problem04/Main04.java +++ b/PTA/ProblemSet03/Problem04/Main.java @@ -4,7 +4,7 @@ import java.util.Scanner; import java.util.Arrays; import java.util.Comparator; -public class Main04 { +public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int id_len = Integer.parseInt(scan.nextLine()); diff --git a/PTA/ProblemSet05/Problem01/Main.java b/PTA/ProblemSet05/Problem01/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..f9cfd5f53ebbc2b25eefea31d8847416478abdc3 --- /dev/null +++ b/PTA/ProblemSet05/Problem01/Main.java @@ -0,0 +1,22 @@ +package ProblemSet05.Problem01; + +public class Main { + public static void main(String[] args) { + System.out.println("车牌号码是" + getCode()); + } + + public static String getCode() { + for(int i = 1; i <= 9; i++) { + for(int j = 1; j <= 9; j++) { + int code = i * 1100 + j * 11, + item = (int) Math.sqrt(code); + + if(Math.pow(item, 2) == code && i != j) { + return Integer.toString(code); + } + } + } + + return null; + } +} diff --git a/PTA/ProblemSet05/Problem02/Main.java b/PTA/ProblemSet05/Problem02/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..c49fdb7945c308c47fd47368dc44ab04703e3572 --- /dev/null +++ b/PTA/ProblemSet05/Problem02/Main.java @@ -0,0 +1,27 @@ +package ProblemSet05.Problem02; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + String str = scan.nextLine(); + scan.close(); + + System.out.println(isPalindrome(str, str.length(), 0) ? "yes" : "no"); + } + + public static boolean isPalindrome(String str, int length, int offset) { + if (length < 2) return false; + + int middleIndex = length / 2, + fixOffset = (length % 2 == 0) ? 0 : 1; + //fixOffset用于修正偶数情况下偏移量不对称问题 + + if(middleIndex + offset + fixOffset >= length) return true; + if(str.charAt(middleIndex + fixOffset + offset) == str.charAt(middleIndex - offset - 1)) + return isPalindrome(str, length, ++offset); + + return false; + } +} diff --git a/PTA/ProblemSet05/Problem03/Main.java b/PTA/ProblemSet05/Problem03/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..6567e641382bb4541f2e36a88f3073b5b826fd37 --- /dev/null +++ b/PTA/ProblemSet05/Problem03/Main.java @@ -0,0 +1,23 @@ +package ProblemSet05.Problem03; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int len = scan.nextInt(); + int[] hArray = new int[len]; + + for(int i = 0; i < len; i++) + hArray[i] = scan.nextInt(); + scan.close(); + + System.out.println(getMax(hArray, 0, hArray[0])); + } + + public static int getMax(int[] arr, int index, int max) { + if(index > arr.length - 1) return max; + + return getMax(arr, index + 1, arr[index] > max ? arr[index] : max); + } +} diff --git a/PTA/ProblemSet06/Problem01/Main.java b/PTA/ProblemSet06/Problem01/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..58ab523da37bc2084b4c4b35055909616e6158fa --- /dev/null +++ b/PTA/ProblemSet06/Problem01/Main.java @@ -0,0 +1,47 @@ +package ProblemSet06.Problem01; + +import java.util.Arrays; +import java.util.Scanner; + +class PersonSortable implements Comparable{ + private String name; + private int age; + + public PersonSortable(String name, int age) { + this.name = name; + this.age = age; + } + + public String toString() { + return name + "-" + age; + } + + public int compareTo(PersonSortable o) { + int nameCompareResult = this.name.compareTo(o.name); + if(nameCompareResult == 0) + return this.age - o.age; + + return nameCompareResult; + } +} + +public class Main { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int len = Integer.parseInt(scan.nextLine()); + PersonSortable[] pList = new PersonSortable[len]; + + for(int i = 0; i < len; i++) { + String[] pData = scan.nextLine().split(" "); + pList[i] = new PersonSortable(pData[0], Integer.parseInt(pData[1])); + } + scan.close(); + + Arrays.sort(pList); + for(PersonSortable p : pList) + System.out.println(p); + + System.out.println(Arrays.toString(PersonSortable.class.getInterfaces())); + } +} + diff --git a/PTA/ProblemSet06/Problem02/Main.java b/PTA/ProblemSet06/Problem02/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..b3fafe60aa60d6669e6373d3187f2b0cc0ffba9b --- /dev/null +++ b/PTA/ProblemSet06/Problem02/Main.java @@ -0,0 +1,76 @@ +package ProblemSet06.Problem02; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Scanner; + +class PersonSortable2 { + private String name; + private int age; + + public PersonSortable2(String name, int age) { + this.name = name; + this.age = age; + } + + public String toString() { + return name + "-" + age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} + +class NameComparator implements Comparator { + + public int compare(PersonSortable2 o1, PersonSortable2 o2) { + return o1.getName().compareTo(o2.getName()); + } + +} + +class AgeComparator implements Comparator { + + public int compare(PersonSortable2 o1, PersonSortable2 o2) { + return o1.getAge() - o2.getAge(); + } + +} + +public class Main { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int len = Integer.parseInt(scan.nextLine()); + PersonSortable2[] pList = new PersonSortable2[len]; + + for(int i = 0; i < len; i++) { + String[] pData = scan.nextLine().split(" "); + pList[i] = new PersonSortable2(pData[0], Integer.parseInt(pData[1])); + } + scan.close(); + + Arrays.sort(pList, new NameComparator()); + System.out.println("NameComparator:sort"); + for(PersonSortable2 p : pList) System.out.println(p); + + Arrays.sort(pList, new AgeComparator()); + System.out.println("AgeComparator:sort"); + for(PersonSortable2 p : pList) System.out.println(p); + + System.out.println(Arrays.toString(NameComparator.class.getInterfaces())); + System.out.println(Arrays.toString(AgeComparator.class.getInterfaces())); + } +} diff --git a/PTA/ProblemSet06/Problem03/Main.java b/PTA/ProblemSet06/Problem03/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..113b9fef9ce8291c02a6d9cc1e2584421faf6a81 --- /dev/null +++ b/PTA/ProblemSet06/Problem03/Main.java @@ -0,0 +1,37 @@ +package ProblemSet06.Problem03; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + int[] arr = new int[5]; + Scanner scan = new Scanner(System.in); + boolean continueListen = true; + + while(continueListen) { + String[] cmd = scan.nextLine().split(" "); + try { + switch(cmd[0]) { + case "arr": + arr[Integer.parseInt(cmd[1])] = 0; + break; + case "null": + throw new NullPointerException(); + case "cast": + @SuppressWarnings("unused") + Integer tmp = (Integer)((Object) new String("")); + break; + case "num": + Integer.parseInt(cmd[1]); + break; + default: + continueListen = false; + } + } catch (Exception e) { + System.out.println(e); + } + } + + scan.close(); + } +} diff --git a/PTA/ProblemSet06/Problem04/Main.java b/PTA/ProblemSet06/Problem04/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..46ca5c89b41239663fbd9dcf347de40243106440 --- /dev/null +++ b/PTA/ProblemSet06/Problem04/Main.java @@ -0,0 +1,24 @@ +package ProblemSet06.Problem04; + +import java.util.Scanner; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int n = scan.nextInt(); + int[] numArr = new int[n]; + + for(int i = 0; i < n; i++) { + try { + numArr[i] = Integer.parseInt(scan.next()); + } catch (Exception e) { + i--; + System.out.println(e); + } + } + scan.close(); + + System.out.println(Arrays.toString(numArr)); + } +} diff --git a/PTA/ProblemSet06/Problem05/Main.java b/PTA/ProblemSet06/Problem05/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..c3a394366642ee5c5bc50f4810825878beb693e2 --- /dev/null +++ b/PTA/ProblemSet06/Problem05/Main.java @@ -0,0 +1,49 @@ +package ProblemSet06.Problem05; + +import java.util.Scanner; + +class ArrayUtils { + public static double findMax(double[] arr, int begin, int end) throws IllegalArgumentException { + if(begin < 0) throw new IllegalArgumentException("begin:" + begin + " < 0"); + else if(begin >= end) throw new IllegalArgumentException("begin:" + begin + " >= end:" + end); + else if(end > arr.length) throw new IllegalArgumentException("end:" + end + " > arr.length"); + + double max = arr[begin]; + for(int i = begin; i < end; i++) { + if(arr[i] > max) max = arr[i]; + } + + return max; + } +} + +public class Main { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int n = scan.nextInt(); + double[] numArr = new double[n]; + boolean continueLinsten = true; + + for(int i = 0; i < n; i++) numArr[i] = scan.nextInt(); + + while(continueLinsten) { + try { + int n1 = scan.nextInt(), n2 = scan.nextInt(); + System.out.println(ArrayUtils.findMax(numArr, n1, n2)); + } + catch (IllegalArgumentException e) { + System.out.println(e); + } + catch (Exception e) { + continueLinsten = false; + } + } + scan.close(); + + try { + System.out.println(ArrayUtils.class.getDeclaredMethod("findMax", double[].class,int.class,int.class)); + } catch (Exception e1) { + + } + } +} diff --git a/PTA/ProblemSet06/Problem06/Main.java b/PTA/ProblemSet06/Problem06/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..6dd8c2998146fa770903906ac756164af23d175e --- /dev/null +++ b/PTA/ProblemSet06/Problem06/Main.java @@ -0,0 +1,91 @@ +package ProblemSet06.Problem06; + +import java.util.Scanner; +import java.util.NoSuchElementException; + +class IllegalNameException extends RuntimeException { + private static final long serialVersionUID = 3229322801946529935L; + + public IllegalNameException() { + super(); + } + + public IllegalNameException(String msg) { + super(msg); + } + +} + +class IllegalScoreException extends Exception{ + private static final long serialVersionUID = 1059038683602760667L; + + public IllegalScoreException() { + super(); + } + + public IllegalScoreException(String msg) { + super(msg); + } + +} + +class Student { + private String name; + private int score; + + public String getName() { + return name; + } + + public void setName(String name) { + if(Character.isDigit(name.charAt(0))) throw new IllegalNameException("the first char of name must not be digit, name=" + name); + + this.name = name; + } + + public int getScore() { + return score; + } + + public void setScore(int score) { + this.score = score; + } + + public String toString() { + return "Student [name=" + name + ", score=" + score + "]"; + } + + public int addScore(int score) throws IllegalScoreException { + int tmpScore = this.score + score; + if(tmpScore < 0 || tmpScore > 100) throw new IllegalScoreException("score out of range, score=" + tmpScore); + + this.score = tmpScore; + return this.score; + } + +} + +public class Main { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + while(true) { + String cmd = scan.nextLine(); + if ("new".equals(cmd)) { + try { + String[] sData = scan.nextLine().split(" "); + if(sData.length != 2) throw new NoSuchElementException(); + + Student stu = new Student(); + stu.setName(sData[0]); + stu.addScore(Integer.parseInt(sData[1])); + System.out.println(stu); + } catch (Exception e) { + System.out.println(e); + } + } else break; + } + scan.close(); + System.out.println("scanner closed"); + } +} diff --git a/PTA/README.md b/PTA/README.md index 0d75bf9c4722be8495c79b8f48aa9f4bcd1c43aa..a8762020deeb5410c176915b5700ef4487574249 100644 --- a/PTA/README.md +++ b/PTA/README.md @@ -17,14 +17,29 @@ - [7-3 jmu-Java-03面向对象基础-03-形状](./ProblemSet04/Problem03/Main.java) - [7-4 jmu-Java-03面向对象基础-05-覆盖](./ProblemSet04/Problem04/Main.java) - [7-5 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company](./ProblemSet04/Problem05/Main.java) +- 实验05 + - [7-1 消失的车](./ProblemSet05/Problem01/Main.java) + - [7-2 判断回文](./ProblemSet05/Problem02/Main.java) + - [7-3 我是升旗手](./ProblemSet05/Problem03/Main.java) +- 实验06 + - [7-1 jmu-Java-04面向对象进阶-01-接口-Comparable](./ProblemSet06/Problem01/Main.java) + - [7-2 jmu-Java-04面向对象进阶--02-接口-Comparator](./ProblemSet06/Problem02/Main.java) + - [7-3 jmu-Java-06异常-01-常见异常](./ProblemSet06/Problem03/Main.java) + - [7-4 jmu-Java-06异常-02-使用异常机制处理异常输入](./ProblemSet06/Problem04/Main.java) + - [7-5 jmu-Java-06异常-03-throw与throws](./ProblemSet06/Problem05/Main.java) + - [7-6 jmu-Java-06异常-04-自定义异常(综合)](./ProblemSet06/Problem06/Main.java) + - 天梯题集(贡献者:[@small_lin168](https://gitee.com/small_lin168)) + - [7-1 N个数求和](./Tianti/Problem01/Main.java) - [7-2 比较大小](./Tianti/Problem02/Main.java) - [7-3 计算指数](./Tianti/Problem03/Main.java) - [7-4 计算阶乘和](./Tianti/Problem04/Main.java) - [7-5 简单题](./Tianti/Problem05/Main.java) - [7-6 跟奥巴马一起画方块](./Tianti/Problem06/Main.java) - [7-7 查验身份证](./Tianti/Problem07/Main.java) + - [7-10 树的遍历](./Tianti/Problem08/Main.java) + - [7-12 最长对称子串](./Tianti/Problem09/Main.java) ### 讨论和建议 如果大家在查看过程中发现错误或不足之处,欢迎大家在[Issues](https://gitee.com/Samler/java-work/issues)中发表自己的见解,也欢迎大家在[Issues](https://gitee.com/Samler/java-work/issues)中进行讨论。 diff --git a/PTA/Tianti/Problem01/.keep b/PTA/Tianti/Problem01/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/PTA/Tianti/Problem01/Main.java b/PTA/Tianti/Problem01/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..41bbe8d57915e4763f0f1781252b438708e1fcf0 --- /dev/null +++ b/PTA/Tianti/Problem01/Main.java @@ -0,0 +1,67 @@ +package Tianti.Problem01; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + //输入完n之后将光标移到一下行 + sc.nextLine(); + int a, b; + + String str=sc.nextLine(); +// System.out.println("This is:"+str); + String[] split = str.split(" "); + String s = split[0]; + //获取分子 + a = Integer.parseInt(s.substring(0, s.indexOf('/'))); + //获取分母 + b = Integer.parseInt(s.substring(s.indexOf('/') + 1)); + int x, y, common; + + common = gcd(a, b); + a /= common; + b /= common; + + for (int i = 1; i < n; i++) { + s = split[i]; + x = Integer.parseInt(s.substring(0, s.indexOf('/'))); + y = Integer.parseInt(s.substring(s.indexOf('/') + 1)); + + common = y * b / gcd(y, b); + + a *= common / b; + x *= common / y; + + a += x; + b = common; + + common = gcd(a, b); + a /= common; + b /= common; + + } + + if(a / b != 0) { + if(a % b != 0) + System.out.println(a / b + " " + a % b + "/" + b); + else + System.out.println(a / b); + } else { + if(a % b != 0) + System.out.println(a % b + "/" + b); + else + System.out.println(0); + } + } + + private static int gcd(int a, int b) { + if(b==0){ + return a; + }else{ + return gcd(b,a%b); + } + } +} \ No newline at end of file diff --git a/PTA/Tianti/Problem08/.keep b/PTA/Tianti/Problem08/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/PTA/Tianti/Problem08/Main.java b/PTA/Tianti/Problem08/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..c6abf00c57b9f85059c9d6a4c68d2582fc014bd2 --- /dev/null +++ b/PTA/Tianti/Problem08/Main.java @@ -0,0 +1,42 @@ +package Tianti.Problem08; + +import java.util.*; + +public class Pta8 { + //存储后序遍历结点 + private static Vector post = new Vector<>(); + //存储中序遍历结点 + private static Vector in = new Vector<>(); + //有序/LinkedHashMap + private static TreeMap level = new TreeMap<>(); + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + for(int i = 0; i end)return; + int i = start; + while(i < end && in.get(i) != post.get(root)){ + i++; + } + level.put(index,post.get(root));//根 + pre(root - 1 - end + i, start, i - 1, 2 * index + 1);//左 + pre(root - 1, i + 1, end, 2 * index + 2);//右 + } +} \ No newline at end of file diff --git a/PTA/Tianti/Problem09/.keep b/PTA/Tianti/Problem09/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/PTA/Tianti/Problem09/Main.java b/PTA/Tianti/Problem09/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..ee6c6d2050c86c6435273804e90762b38e2858ca --- /dev/null +++ b/PTA/Tianti/Problem09/Main.java @@ -0,0 +1,36 @@ +package Tianti.Problem09; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Pta9 { + public static void main(String[] args) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String str = reader.readLine(); + //设置一个NN的二维数组 + int arr_i[][] = new int[str.length()][str.length()]; + int res_count =0; + for(int i=0;i=2){ + if(str.charAt(i)==str.charAt(j)&&arr_i[j+1][i-1]>0){ + arr_i[j][i] = arr_i[j+1][i-1]+2; + } + } + } + } + for(int i=0;i