P1428 小鱼比可爱

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = sc.nextInt();
  6. int[] array = new int[n];
  7. for (int i = 0; i < n; i++) {
  8. array[i] = sc.nextInt();
  9. }
  10. System.out.print(0);
  11. for (int i = 1; i < n; i++) {
  12. System.out.print(" " + cal(array,i));
  13. }
  14. }
  15. public static int cal(int[] array,int index){
  16. int count = 0;
  17. for (int i = 0; i < index; i++) {
  18. if(array[i]<array[index]){
  19. count++;
  20. }
  21. }
  22. return count;
  23. }
  24. }

P1427 小鱼的数字游戏

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. ArrayList<Integer> list = new ArrayList<>();
  7. while(true){
  8. int n = sc.nextInt();
  9. if(n==0){
  10. break;
  11. }
  12. list.add(n);
  13. }
  14. System.out.print(list.get(list.size()-1));
  15. for (int i=list.size()-2; i >= 0; i--) {
  16. System.out.print(" " + list.get(i));
  17. }
  18. }
  19. }

P5727 【深基5.例3】冰雹猜想

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. ArrayList<Integer> list = new ArrayList<>();
  7. int n = sc.nextInt();
  8. list.add(n);
  9. while(n>1){
  10. if(n%2==0){
  11. n/=2;
  12. }else {
  13. n=n*3+1;
  14. }
  15. list.add(n);
  16. }
  17. for (int i=list.size()-1; i>=0; i--) {
  18. System.out.print(list.get(i) + " ");
  19. }
  20. }
  21. }

P1047 [NOIP2005 普及组] 校门外的树

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int l = sc.nextInt();
  6. int m = sc.nextInt();
  7. boolean[] trees = new boolean[l+1];
  8. for (int i = 0; i < m; i++) {
  9. int left = sc.nextInt();
  10. int right = sc.nextInt();
  11. for (int j = left; j <= right ; j++) {
  12. trees[j] = true;
  13. }
  14. }
  15. int count = 0;
  16. for(boolean b : trees){
  17. if(!b){
  18. count++;
  19. }
  20. }
  21. System.out.println(count);
  22. }
  23. }

P5728 【深基5.例5】旗鼓相当的对手

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. int count = 0;
  8. ArrayList<Student> list = new ArrayList<>();
  9. for (int i = 0; i < n; i++) {
  10. Student student = new Student();
  11. student.Chinese = sc.nextInt();
  12. student.Math = sc.nextInt();
  13. student.English = sc.nextInt();
  14. list.add(student);
  15. }
  16. for (int i = 0; i < list.size() - 1; i++) {
  17. for (int j = i + 1; j < list.size(); j++) {
  18. if(checkStudent(list.get(i),list.get(j))){
  19. count++;
  20. }
  21. }
  22. }
  23. System.out.println(count);
  24. }
  25. public static boolean checkStudent(Student student1,Student student2){
  26. boolean b1 = Math.abs(student1.Chinese - student2.Chinese) <= 5;
  27. boolean b2 = Math.abs(student1.Math - student2.Math) <= 5;
  28. boolean b3 = Math.abs(student1.English - student2.English) <= 5;
  29. boolean b4 = Math.abs((student1.Chinese + student1.Math + student1.English) - (student2.Chinese + student2.Math + student2.English)) <=10;
  30. return b1&&b2&&b3&&b4;
  31. }
  32. static class Student{
  33. int Chinese;
  34. int Math;
  35. int English;
  36. }
  37. }

P5729 【深基5.例7】工艺品制作

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int x = sc.nextInt();
  6. int y = sc.nextInt();
  7. int z = sc.nextInt();
  8. int q = sc.nextInt();
  9. int[][][] iceCube = new int[21][21][21];
  10. for (int i = 0; i < q; i++) {
  11. //输入坐标点
  12. int x1 = sc.nextInt();
  13. int y1 = sc.nextInt();
  14. int z1 = sc.nextInt();
  15. int x2 = sc.nextInt();
  16. int y2 = sc.nextInt();
  17. int z2 = sc.nextInt();
  18. //切割
  19. for (int j = x1; j <= x2; j++) {
  20. for (int k = y1; k <= y2; k++) {
  21. for (int l = z1; l <= z2; l++) {
  22. iceCube[j][k][l] = 1;
  23. }
  24. }
  25. }
  26. }
  27. //遍历,计数
  28. int count = 0;
  29. for (int i = 1; i <= x; i++) {
  30. for (int j = 1; j <= y; j++) {
  31. for (int k = 1; k <= z; k++) {
  32. if(iceCube[i][j][k] == 0){
  33. count++;
  34. }
  35. }
  36. }
  37. }
  38. System.out.println(count);
  39. }
  40. }

P2550 [AHOI2001]彩票摇奖

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int num = sc.nextInt();
  6. List<Integer> list = new ArrayList<>();
  7. for (int i = 0; i < 7; i++) {
  8. list.add(sc.nextInt());
  9. }
  10. int[] resultArray = new int[7];
  11. for (int i = 0; i < num; i++) {
  12. int counter = 0;
  13. for (int j = 0; j < 7; j++) {
  14. if (list.contains(sc.nextInt())) {
  15. counter++;
  16. }
  17. }
  18. if (counter != 0) {
  19. resultArray[7-counter]++;
  20. }
  21. }
  22. for (int i = 0; i < 7; i++) {
  23. System.out.print(resultArray[i] + " ");
  24. }
  25. }
  26. }

P2615 [NOIP2015 提高组] 神奇的幻方

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. int[][] a = new int[40][40];
  6. int n = sc.nextInt();
  7. //第一行中间为1
  8. int x = 1;
  9. int y = (n+1)/2;
  10. a[x][y] = 1;
  11. //构建幻方
  12. for (int i = 2; i <= n*n; i++) {
  13. if(x==1&&y!=n){
  14. a[n][y+1]=i;
  15. x=n;
  16. y++;
  17. }else if(x!=1&&y==n){
  18. a[x-1][1]=i;
  19. x--;
  20. y=1;
  21. }else if(x==1&&y==n){
  22. a[x+1][y]=i;
  23. x++;
  24. }else {
  25. if(a[x-1][y+1]==0){
  26. a[x-1][y+1]=i;
  27. x--;
  28. y++;
  29. }else{
  30. a[x+1][y]=i;
  31. x++;
  32. }
  33. }
  34. }
  35. //遍历输出
  36. for (int i = 1; i <= n; i++) {
  37. for (int j = 1; j <= n; j++) {
  38. System.out.print(a[i][j] + " ");
  39. }
  40. System.out.println();
  41. }
  42. }
  43. }

P5730 【深基5.例10】显示屏

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. //五行字符串拼接成结果
  6. StringBuilder line1 = new StringBuilder();
  7. StringBuilder line2 = new StringBuilder();
  8. StringBuilder line3 = new StringBuilder();
  9. StringBuilder line4 = new StringBuilder();
  10. StringBuilder line5 = new StringBuilder();
  11. //接收第一行,有几位数字。
  12. String s = sc.nextLine();
  13. char[] nums = sc.nextLine().toCharArray();
  14. for (char c : nums) {
  15. switch (c) {
  16. case '0':
  17. line1.append("XXX.");
  18. line2.append("X.X.");
  19. line3.append("X.X.");
  20. line4.append("X.X.");
  21. line5.append("XXX.");
  22. break;
  23. case '1':
  24. line1.append("..X.");
  25. line2.append("..X.");
  26. line3.append("..X.");
  27. line4.append("..X.");
  28. line5.append("..X.");
  29. break;
  30. case '2':
  31. line1.append("XXX.");
  32. line2.append("..X.");
  33. line3.append("XXX.");
  34. line4.append("X...");
  35. line5.append("XXX.");
  36. break;
  37. case '3':
  38. line1.append("XXX.");
  39. line2.append("..X.");
  40. line3.append("XXX.");
  41. line4.append("..X.");
  42. line5.append("XXX.");
  43. break;
  44. case '4':
  45. line1.append("X.X.");
  46. line2.append("X.X.");
  47. line3.append("XXX.");
  48. line4.append("..X.");
  49. line5.append("..X.");
  50. break;
  51. case '5':
  52. line1.append("XXX.");
  53. line2.append("X...");
  54. line3.append("XXX.");
  55. line4.append("..X.");
  56. line5.append("XXX.");
  57. break;
  58. case '6':
  59. line1.append("XXX.");
  60. line2.append("X...");
  61. line3.append("XXX.");
  62. line4.append("X.X.");
  63. line5.append("XXX.");
  64. break;
  65. case '7':
  66. line1.append("XXX.");
  67. line2.append("..X.");
  68. line3.append("..X.");
  69. line4.append("..X.");
  70. line5.append("..X.");
  71. break;
  72. case '8':
  73. line1.append("XXX.");
  74. line2.append("X.X.");
  75. line3.append("XXX.");
  76. line4.append("X.X.");
  77. line5.append("XXX.");
  78. break;
  79. default:
  80. line1.append("XXX.");
  81. line2.append("X.X.");
  82. line3.append("XXX.");
  83. line4.append("..X.");
  84. line5.append("XXX.");
  85. break;
  86. }
  87. }
  88. //打印时,抹去最后一列多余的那个 ’.‘
  89. int length = line1.length() - 1;
  90. System.out.println(line1.substring(0, length));
  91. System.out.println(line2.substring(0, length));
  92. System.out.println(line3.substring(0, length));
  93. System.out.println(line4.substring(0, length));
  94. System.out.println(line5.substring(0, length));
  95. }
  96. }

P1554 梦中的统计

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int[] count = new int[10];
  6. int M = sc.nextInt();
  7. int N= sc.nextInt();
  8. for (int i = M; i <= N ; i++) {
  9. int num = i;
  10. while(num>0){
  11. count[num%10]++;
  12. num /= 10;
  13. }
  14. }
  15. for (int i : count) {
  16. System.out.print(i + " ");
  17. }
  18. }
  19. }

P2141 [NOIP2014 普及组] 珠心算测验

  1. import java.util.HashSet;
  2. import java.util.Scanner;
  3. import java.util.Set;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int n = sc.nextInt();
  8. int[] a = new int[n];
  9. for (int i = 0; i < n; i++) {
  10. a[i] = sc.nextInt();
  11. }
  12. //开始搜索
  13. Set<Integer> set = new HashSet<>();//Set集合自动去重,避免相同一组计算两次
  14. for (int i = 0; i < n-1; i++) {//左边的数
  15. for (int j = i+1; j < n; j++) {//右边的数
  16. for (int k = 0; k < n; k++) {//遍历数组
  17. if(k!=i && k!=j && a[i]+a[j]==a[k]){
  18. set.add(k);
  19. }
  20. }
  21. }
  22. }
  23. System.out.println(set.size());
  24. }
  25. }

P1614 爱与愁的心痛

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = sc.nextInt();
  6. int m = sc.nextInt();
  7. int[] array = new int[n];
  8. for (int i = 0; i < n; i++) {
  9. array[i] = sc.nextInt();
  10. }
  11. int min = 0;
  12. for (int i = 0; i < n-m+1; i++) {
  13. int sum = 0;
  14. for (int j = 1; j <= m; j++) {
  15. sum += array[i+j-1];
  16. }
  17. if(i==0 || sum<min){
  18. min = sum;
  19. }
  20. }
  21. System.out.println(min);
  22. }
  23. }

P2911 [USACO08OCT]Bovine Bones G

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int[] table = new int[100];//a+b+c最大值为80,定义一个稍微大一点的数组
  6. int a = sc.nextInt();
  7. int b = sc.nextInt();
  8. int c = sc.nextInt();
  9. for (int i = 1; i <= a; i++) {
  10. for (int j = 1; j <= b; j++) {
  11. for (int k = 1; k <= c; k++) {
  12. table[i+j+k]++;
  13. }
  14. }
  15. }
  16. int max = 0;
  17. int result = 0;
  18. for(int i = 3; i <= a+b+c; i++){
  19. if(table[i] > max){
  20. max = table[i];
  21. result = i;
  22. }
  23. }
  24. System.out.println(result);
  25. }
  26. }

P1161 开灯

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] lights = new int[10000000];
        Arrays.fill(lights,-1);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            double a = sc.nextDouble();
            int t = sc.nextInt();
            for (int j = 1; j <= t; j++) {
                int position = (int) Math.floor(a*j);
                lights[position] *= -1;
            }
        }
        for (int i = 0; i < lights.length; i++) {
            if(lights[i] == 1){
                System.out.println(i);
                return;
            }
        }
    }
}

P5731 【深基5.习6】蛇形方阵

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n == 1)
            System.out.println("  1");
        if(n == 2){
            System.out.println("  1  2");
            System.out.println("  4  3");
        }
        if(n == 3) {
            System.out.println("  1  2  3");
            System.out.println("  8  9  4");
            System.out.println("  7  6  5");
        }
        if(n == 4) {
            System.out.println("  1  2  3  4");
            System.out.println(" 12 13 14  5");
            System.out.println(" 11 15 16  6");
            System.out.println(" 10  9  8  7");
        }
        if(n == 5) {
            System.out.println("  1  2  3  4  5");
            System.out.println(" 16 17 18 19  6");
            System.out.println(" 15 24 25 20  7");
            System.out.println(" 14 23 22 21  8");
            System.out.println(" 13 12 11 10  9");
        }
        if(n == 6) {
            System.out.println("  1  2  3  4  5  6");
            System.out.println(" 20 21 22 23 24  7");
            System.out.println(" 19 32 33 34 25  8");
            System.out.println(" 18 31 36 35 26  9");
            System.out.println(" 17 30 29 28 27 10");
            System.out.println(" 16 15 14 13 12 11");
        }
        if(n == 7) {
            System.out.println("  1  2  3  4  5  6  7");
            System.out.println(" 24 25 26 27 28 29  8");
            System.out.println(" 23 40 41 42 43 30  9");
            System.out.println(" 22 39 48 49 44 31 10");
            System.out.println(" 21 38 47 46 45 32 11");
            System.out.println(" 20 37 36 35 34 33 12");
            System.out.println(" 19 18 17 16 15 14 13");
        }
        if(n == 8) {
            System.out.println("  1  2  3  4  5  6  7  8");
            System.out.println(" 28 29 30 31 32 33 34  9");
            System.out.println(" 27 48 49 50 51 52 35 10");
            System.out.println(" 26 47 60 61 62 53 36 11");
            System.out.println(" 25 46 59 64 63 54 37 12");
            System.out.println(" 24 45 58 57 56 55 38 13");
            System.out.println(" 23 44 43 42 41 40 39 14");
            System.out.println(" 22 21 20 19 18 17 16 15");
        }
        if(n == 9) {
            System.out.println("  1  2  3  4  5  6  7  8  9");
            System.out.println(" 32 33 34 35 36 37 38 39 10");
            System.out.println(" 31 56 57 58 59 60 61 40 11");
            System.out.println(" 30 55 72 73 74 75 62 41 12");
            System.out.println(" 29 54 71 80 81 76 63 42 13");
            System.out.println(" 28 53 70 79 78 77 64 43 14");
            System.out.println(" 27 52 69 68 67 66 65 44 15");
            System.out.println(" 26 51 50 49 48 47 46 45 16");
            System.out.println(" 25 24 23 22 21 20 19 18 17");
        }
    }
}

P5732 【深基5.习7】杨辉三角

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] array = new int[n][n];
        //将第一列初始化为1
        for (int i = 0; i < array.length; i++) {
            array[i][0] = 1;
        }
        //从第二行第二列开始计算
        for (int i = 1; i < array.length; i++) {
            for (int j = 1; j < n; j++) {
                array[i][j] = array[i-1][j] + array[i-1][j-1];
            }
        }
        for (int[] ints : array) {
            for (int anInt : ints) {
                if(anInt!=0){
                    System.out.print(anInt + " ");
                }
            }
            System.out.println();
        }
    }
}

P1789 【Mc生存】插火把

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//区域边长
        int m = sc.nextInt();//火把
        int k = sc.nextInt();//萤石
        int[][] area = new int[101][101];
        for(int[] ints : area){//全部标记为0(暗)
            Arrays.fill(ints,0);
        }
        for (int i = 0; i < m; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            fire(x-1,y-1,area,n);
        }
        for (int i = 0; i < k; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            stone(x-1,y-1,area,n);
        }
        int count = 0;
        for(int i=0; i < n; i++){
            for (int j = 0; j < n; j++) {
                if(area[i][j] == 0){
                    count++;
                }
                //System.out.print(area[i][j]);
            }
            //System.out.println();
        }
        System.out.println(count);
    }

    public static void stone(int x, int y,int[][] area,int n){
        for (int i = x-2; i <= x+2; i++) {
            for (int j = y-2; j <= y+2; j++) {
                if(i<0||j<0||i>n-1||j>n-1){
                    continue;
                }
                area[i][j] = 1;
            }
        }
    }

    public static void fire(int x,int y,int[][] area,int n){
        for (int i = x-2; i <= x+2; i++) {
            for (int j = y-2; j <= y+2; j++) {
                if(i<0||j<0||i>n||j>n){
                    continue;
                }
                if((j==y-2||j==y+2) && i!=x){
                    continue;
                }
                if((j==y-1||j==y+1) && (i==x-2 || i==x+2)) {
                    continue;
                }
                area[i][j] = 1;
            }
        }
    }
}

P1319 压缩技术

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder result = new StringBuilder();
        int sum = 0;    //记录有多少个(0/1),达到N*N个结束循环
        int N = sc.nextInt();
        for (int i = 0; sum < N*N; i++) {
            int num = sc.nextInt();
            for (int j = 0; j < num; j++) {
                if(i%2 == 0){
                    result.append("0");
                }else {
                    result.append("1");
                }
            }
            sum += num;
        }
        for (int i = 0; i < N; i++) {
            System.out.println(result.substring(i*N,(i+1)*N));
        }
    }
}

P1320 压缩技术(续集版)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        String line = sc.nextLine();
        sb.append(line);
        int N = line.length();
        //拼接成一行字符串
        for (int i = 1; i < N; i++) {
            sb.append(sc.nextLine());
        }
        String result = sb.toString();
        System.out.print(N + " ");
        //遍历字符串计算结果
        int countZero = 0;
        int countOne = 0;
        int len = result.length();
        for (int i = 0; i < len;) {
            //题目中规定了第一个数是0的个数,所以先查0
            while(i < len && result.charAt(i) == '0'){
                countZero++;
                i++;
            }
            System.out.print(countZero + " ");
            countZero = 0;
            //查完0可能已经遍历完了字符串,要检查一下
            if(i < len){
                while(i < len && result.charAt(i) == '1'){
                    countOne++;
                    i++;
                }
                System.out.print(countOne + " ");
                countOne = 0;
            }
        }
    }
}

P1205 [USACO1.2] 方块转换 Transformations

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        char[][] chs1 = new char[n][n];
        char[][] chs2 = new char[n][n];
        for (int i = 0; i < n; i++) {
            char[] tempChars = sc.nextLine().toCharArray();
            System.arraycopy(tempChars, 0, chs1[i], 0, n);
        }
        for (int i = 0; i < n; i++) {
            char[] tempChars = sc.nextLine().toCharArray();
            System.arraycopy(tempChars, 0, chs2[i], 0, n);
        }
        if (wk1(n,chs1,chs2)) {
            System.out.println(1);
        } else if (wk2(n,chs1,chs2)) {
            System.out.println(2);
        } else if (wk3(n,chs1,chs2)) {
            System.out.println(3);
        } else if (wk4(n,chs1,chs2)) {
            System.out.println(4);
        } else if (wk5(n,chs1,chs2)) {
            System.out.println(5);
        } else if (wk6(chs1,chs2)) {
            System.out.println(6);
        } else {
            System.out.println(7);
        }
    }

    private static boolean wk1(int n, char[][] chars1, char[][] chars2) {
        char[][] result = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                result[j][n -i-1] = chars1[i][j];
            }
        }
        return Arrays.deepEquals(result, chars2);
    }

    private static boolean wk2(int n, char[][] chars1, char[][] chars2) {
        char[][] result = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                result[n-i-1][n-j-1] = chars1[i][j];
            }
        }
        return Arrays.deepEquals(result, chars2);
    }

    private static boolean wk3(int n, char[][] chars1, char[][] chars2) {
        char[][] result = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                result[n-j-1][i] = chars1[i][j];
            }
        }
        return Arrays.deepEquals(result, chars2);
    }

    private static boolean wk4(int n, char[][] chars1, char[][] chars2) {
        char[][] result = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                result[i][n -j-1] = chars1[i][j];
            }
        }
        return Arrays.deepEquals(result, chars2);
    }

    private static boolean wk5(int n, char[][] chars1, char[][] chars2) {
        char[][] result = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                result[i][n -j-1] = chars1[i][j];
            }
        }
        return wk1(n,result,chars2) || wk2(n,result,chars2) || wk3(n,result,chars2);
    }

    private static boolean wk6(char[][] chars1, char[][] chars2) {
        return Arrays.deepEquals(chars1, chars2);
    }
}