洗盘子 注意stack的用法
.peek() .push() .isEmpty() .pop()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); Stack<Integer> record = new Stack <>(); while (sc.hasNextInt()){ int n = sc.nextInt(); while (n-- > 0 ){ record.push(sc.nextInt()); } int m = sc.nextInt(); while (m-- > 0 ){ int x = sc.nextInt(); if (x == 1 ){ if (!record.isEmpty()) record.pop(); } else if (x == 2 ){ record.push(sc.nextInt()); } } if (record.isEmpty()){ System.out.println("All the dishes have been washed." ); } else { System.out.println(record.peek()); } } } }
排队去奶茶 ArrayDeque .add(); .poll(); .peek(); .isEmpty();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); Deque<String> record = new ArrayDeque <>(); int n = sc.nextInt(); sc.nextLine(); String[] names = sc.nextLine().split(" " ); for (int i = 0 ; i < n; i++){ record.addLast(names[i]); } int m = sc.nextInt(); while (m-- > 0 ){ int step = sc.nextInt(); if (step == 1 ){ if (!record.isEmpty()) record.pollFirst(); } else { record.offer(sc.next()); } } if (record.isEmpty()) { System.out.println("There are no more people in the queue." ); } else { System.out.println(record.peek()); } } }
图形的面积 class整体用法
封装,尽量把class里面的属性封装成private,利用getter和setter函数读取和修正 继承,用extends继承父类,语法为子类extends父类 抽象类,当创建实例没有意义时,把class抽象掉,再把方法抽象掉(只能在抽象class里面)【只规定return type和方法名字】 多态,Shape建立后,可以有不同的子类,子类可以调用不同子类之下的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 import java.util.*;abstract class Shape { protected String type; public abstract double CalculateArea () ; public String getType () {return this .type;} } class Rectangle extends Shape { private int width; private int height; public Rectangle (int width, int height) { this .type = "Rectangle" ; this .width = width; this .height = height; } public double CalculateArea () { return (double ) (width * height); } } class Circle extends Shape { private int radius; public Circle (int radius) { this .type = "Circle" ; this .radius = radius; } public double CalculateArea () { return 3.14 * radius * radius; } } public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); while (sc.hasNext()){ String type = sc.next(); if (type.equals("rectangle" )){ Shape rec = new Rectangle (sc.nextInt(), sc.nextInt()); System.out.printf("Rectangle area: %.2f%n" , rec.CalculateArea()); } else if (type.equals("circle" )){ Shape cir = new Circle (sc.nextInt()); System.out.printf("Circle area: %.2f%n" , cir.CalculateArea()); } else { break ; } } } }
A + B 问题IV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); if (n != 0 ){ int sum = 0 ; for (int i = 0 ; i < n; i++){ sum += sc.nextInt(); } System.out.println(sum); } else { break ; } } } }
A + B 问题VII 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); while (sc.hasNextInt()){ int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a + b); System.out.println(); } } }
运营商活动 result 从 m开始
注意while loop 的判断条件,m和n的关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); while (sc.hasNextInt()){ int m = sc.nextInt(); int n = sc.nextInt(); if (m == 0 && n == 0 ){ break ; } else { System.out.println(getMaximumDays(m, n)); } } } public static int getMaximumDays (int m, int n) { int result = m; while (m >= n){ int step = m / n; result += step; m = m - (step * n) + step; } return result; } }
共同祖先 利用HashMap 溯源到同一个位置,数各自的数量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); Map<Integer, Integer> record = new HashMap <>(); while (n-- > 0 ){ int a = sc.nextInt(); int b = sc.nextInt(); record.put(a, b); } getResult(record); } } public static void getResult (Map<Integer, Integer> record) { int count_ming = 0 ; int count_yu = 0 ; int yu = 1 ; int ming = 2 ; while (record.containsKey(yu)){ count_yu++; yu = record.get(yu); } while (record.containsKey(ming)){ count_ming++; ming = record.get(ming); } if (count_yu > count_ming){ System.out.println("You are my elder" ); } else if (count_yu < count_ming){ System.out.println("You are my younger" ); } else { System.out.println("You are my brother" ); } } }