题目:求 1∼202504121中,有多少个数可以通过改变其数字顺序后含有 2025。
例如,5220、21520 可以,而 205、225、2200、222555111222555111 则不行。
提示:要求的数就是含有至少 1 个 0、2 个 2、1 个 5 的数。 代码:
public class demo3 {
static void main(String[] args) {
int count = 0;
for (int i = 2025;i<=20250412;i++){
int number = i;
int a = 0;//记录2的个数
int b = 0;//记录0的个数
int d = 0;//记录5的个数
//转换为一个数字数组
int[] arr = String.valueOf(number).chars().map(c->c-'0').toArray();
for (int j = 0;j<arr.length;j++){
if (a > 2 && b > 1 && d > 1){
break;
}else if (arr[j] == 0){
b++;
}else if (arr[j] == 2){
a++;
}else if (arr[j] == 5){
d++;
}
}
if (a >= 2 && b >= 1 && d >= 1){
count++;
}
}
System.out.println(count);
}
}
整体来说是比较简单的,在统计的时候要注意不能重复统计。