题
- 1、求和
- 2、天数
- 3、最大缝隙
1、求和
https://www.lanqiao.cn/problems/1442/learning/
解法:字符串方法的应用
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {Scanner scan = new Scanner(System.in);//在此输入您的代码...int sum = 0;for(int i = 1; i<=2019;i++) {String str = String.valueOf(i);if(str.contains("2") || str.contains("0") || str.contains("1") || str.contains("9")) {sum +=i;}}System.out.println(sum);scan.close();}
}
2、天数
https://www.lanqiao.cn/problems/542/learning/
解法:暴力
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[] day = {0,31 ,28 , 31, 30,31,30,31,31,30,31,30,31};System.out.println(day[m]);scan.close();}
}
3、最大缝隙
https://www.lanqiao.cn/problems/543/learning/
解法:暴力
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[] num = new int[n];for(int i = 0; i< n ; i++) {num[i] = scan.nextInt();}int gap = 0;for(int j = 1 ; j <n;j++) {if(num[j] - num[j-1] > gap) {gap = num[j] - num[j-1];}}System.out.println(gap);scan.close();}
}