【第三天】蓝桥杯备战

  • 1、成绩分析
  • 2、合法日期
  • 3、时间加法
  • 4、扫雷
  • 5、大写
  • 6、标题统计

1、成绩分析

https://www.lanqiao.cn/problems/497/learning/
【第三天】蓝桥杯备战_第1张图片
【第三天】蓝桥杯备战_第2张图片
解法:暴力

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Test3 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n = scan.nextInt();
        int i = 0;
        int[] score = new int[n];
        while(i < n) {
            score[i] = scan.nextInt();
            i++;
        }
        scan.close();
        int maxScore = score[0];
        int minScore = score[0];
        int sum = 0;
        for(int j = 0 ; j< n ; j++) {
            if(score[j] > maxScore) {
                maxScore = score[j];
            }
            if(score[j] < minScore) {
                minScore = score[j];
            }
            sum += score[j];
        }
        System.out.println(maxScore);
        System.out.println(minScore);
        double ave = (double)sum / n;
        System.out.printf("%.2f",ave);//是printf
    }
}

注意:最后得到的平均值要保留两位小数,是double型,所以sum要强转为double型

2、合法日期

https://www.lanqiao.cn/problems/541/learning/
【第三天】蓝桥杯备战_第3张图片
【第三天】蓝桥杯备战_第4张图片
解法:暴力

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int m = scan.nextInt();
        int d = scan.nextInt();
        boolean ret = judge(m , d);
        if(ret == true) {
          System.out.println("yes");
        }else {
          System.out.println("no");
        }
        scan.close();
    }
    public static boolean judge(int m,int d) {
      int[] data = {0,31 , 28 , 31 , 30, 31 , 30,31,31,30,31,30,31};
      if(m > 12 || d > 31) {
        return false;
      }
      if(data[m] < d) {
        return false;
      }
      return true;
    }
}

3、时间加法

https://www.lanqiao.cn/problems/548/learning/
【第三天】蓝桥杯备战_第5张图片
【第三天】蓝桥杯备战_第6张图片
解法:暴力

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int a = scan.nextInt();
        int b = scan.nextInt();
        int t = scan.nextInt();
        b = b + t;
        while(b >= 60) {
          b = b - 60;
          a = a + 1;
        }
        System.out.println(a);
        System.out.println(b);
        scan.close();
    }
}

4、扫雷

https://www.lanqiao.cn/problems/549/learning/
【第三天】蓝桥杯备战_第7张图片
【第三天】蓝桥杯备战_第8张图片
解法:暴力

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[][] array = new int[n+2][m+2];
        int[][] result = new int[n+2][m+2];
        for(int i = 1 ; i <= n ; i++) {
          for(int j = 1 ;j <= m ; j++) {
            array[i][j] = scan.nextInt();
          }
        }
        for(int i = 1 ; i<= n ; i++) {
          for(int j = 1 ; j <= m ; j++) {
            if(array[i][j] == 1) {
              System.out.print("9" + " ");
            }else {
               result[i][j] = array[i-1][j-1] + array[i-1][j] + array[i-1][j+1] +array[i][j-1] + array[i][j+1]
                            +array[i+1][j-1] + array[i+1][j] + array[i+1][j+1];
            System.out.print(result[i][j] + " ");
            }
           
          }
          System.out.println();
        }
        scan.close();
    }
}

5、大写

https://www.lanqiao.cn/problems/1590/learning/
【第三天】蓝桥杯备战_第9张图片
【第三天】蓝桥杯备战_第10张图片

解法:运用字符串方法

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        String str = scan.nextLine();
        System.out.println(str.toUpperCase());
        scan.close();
    }
}

6、标题统计

https://www.lanqiao.cn/problems/325/learning/
【第三天】蓝桥杯备战_第11张图片
【第三天】蓝桥杯备战_第12张图片
解法:暴力

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        String  str = scan.nextLine();
        int count = 0 ;
        for(int i = 0 ; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(ch != ' ' && ch != '\n') {
                count++;
            }
        }
        System.out.println(count);
        scan.close();
    }
}

你可能感兴趣的:(蓝桥杯备战刷题,蓝桥杯,职场和发展,java,算法)