Q1:数的分解
题目:

思路:
我们要求2019分解成三个数,首先想到可以用三个for循环来暴力得出答案,其实用两个for循环就可以了,因为知道了前两个数,第三个数用2019减去前两个数就好了。我们还要判断这三个数是否含有2和4.
代码:
public class Question1 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i < 2019 / 2; i++) {
for (int j = i + 1; j <= 2019; j++) {
int k = 2019 - i - j;
if (check(i) && check(j) && check(k) && j < k) {
count++;
}
}
}
System.out.println(count);
}
public static boolean check(int n) {
while (n > 0) {
if (n % 10 == 4 || n % 10 == 2) return false;
n /= 10;
}
return true;
}
}
Q2:猜生日
题目:

思路:
我们已经知道了月份,现在只需要求出年份和具体日子了,我们可以用两个for循环,第一个循环年份从1900开始到2012结束,然后循环日子,注意:1-9只有一个数字需要在前面多加一个0,然后把年月日用字符串连起来,最后解析成int,然后判断是否能被2012 , 3 , 1 2整除,最后就出来一个结果符合要求。
代码:
public class Question2 {
public static void main(String[] args) {
String str1, str2, str = "";
for (int i = 1900; i < 2012; i++) {
for (int j = 1; j <= 31; j++) {
if (j < 10) {
str = i + "060" + j;
} else {
str = i + "06" + j;
}
int n = Integer.parseInt(str);
if (n % 2012 == 0 && n % 3 == 0 && n % 12 == 0) {
System.out.println(n);
}
}
}
}
}
Q3:成绩统计
题目:

思路 :
直接统计多少大于60,多少大于85人数即可,最后除总人数,注意在处理四舍五入的时候要+0.5,因为int直接是去掉小数点后面的数。
代码:
import java.util.Scanner;
public class Question3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int jg = 0;
int yx = 0;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (x >= 60) jg++;
if (x >= 85) yx++;
}
int jgl = (int)(jg*1.0/n*100.0+0.5);
int yxl = (int)(yx*1.0/n*100.0+0.5);
System.out.println(jgl+"%");
System.out.println(yxl+"%");
}
}
Q4: