1、卡片

  1. public class Main {
  2. public static void main(String[] args) {
  3. // HashMap 用于存储卡片的个数 (用数组也可以)
  4. Map<Integer, Integer> hashMap = new HashMap<>();
  5. hashMap.put(0, 2021);
  6. hashMap.put(1, 2021);
  7. hashMap.put(2, 2021);
  8. hashMap.put(3, 2021);
  9. hashMap.put(4, 2021);
  10. hashMap.put(5, 2021);
  11. hashMap.put(6, 2021);
  12. hashMap.put(7, 2021);
  13. hashMap.put(8, 2021);
  14. hashMap.put(9, 2021);
  15. Integer number = 1;
  16. while(true) {
  17. // 将数字转成字符串
  18. String str = number.toString();
  19. Integer strLength = str.length();
  20. // 遍历字符串的每一位并判断卡片是否足够
  21. for(int i = 0; i < strLength; i++) {
  22. char ch = str.charAt(i);
  23. String s = String.valueOf(ch);
  24. Integer size = hashMap.get(Integer.parseInt(s));
  25. if(size <= 0) {
  26. System.out.println(number - 1);
  27. return;
  28. } else {
  29. size--;
  30. hashMap.put(Integer.parseInt(s), size);
  31. }
  32. }
  33. number++;
  34. }
  35. }
  36. }

2、直线

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. int x = 20;
  5. int y = 21;
  6. // 利用 HashSet 的去重功能
  7. Set<String> result = new HashSet<>();
  8. // 用 ArrayList 存储所有的点
  9. List<Point> list = new ArrayList<>();
  10. for(int i = 0; i < x; i++) {
  11. for(int j = 0; j < y; j++) {
  12. Point point = new Point(i, j);
  13. list.add(point);
  14. }
  15. }
  16. for(int i = 0; i < list.size(); i++) {
  17. for(int j = i + 1; j < list.size(); j++) {
  18. Point point1 = list.get(i);
  19. Point point2 = list.get(j);
  20. // 斜率不存在时
  21. if(point1.x == point2.x) {
  22. result.add("x=" + point1.x);
  23. } else {
  24. double k =getK(point1, point2);
  25. double zero = getZero(point1, k);
  26. result.add(k + "+" + zero);
  27. }
  28. }
  29. }
  30. System.out.println(result.size());
  31. Iterator it = result.iterator();
  32. while(it.hasNext()) {
  33. System.out.println(it.next());
  34. }
  35. }
  36. // 求出直线的斜率 k
  37. public static double getK(Point point1, Point point2) {
  38. return (point1.y * 1.00 - point2.y) / (point1.x * 1.00 - point2.x);
  39. }
  40. // 根据斜率 k, 和直线上的一点, 求得 x = 0 时 y 的值
  41. // 即 y = kx + b 里的 b 值
  42. public static double getZero(Point point, double k) {
  43. return point.y - k * point.x;
  44. }
  45. }
  46. class Point {
  47. int x;
  48. int y;
  49. public Point(int x, int y) {
  50. this.x = x;
  51. this.y = y;
  52. }
  53. }

3、货物摆放

4、路径

5、ASC

  1. public class Main {
  2. public static void main(String[] args) {
  3. System.out.println((int)'L');
  4. }
  5. }

6、括号序列

7、时间显示

8、杨辉三角形

9、双向排序

10、最少砝码