https://www.lanqiao.cn/problems/539/learning/
解法:暴力
import java.util.Scanner;
import java.util.Arrays;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
int n = scan.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = scan.nextInt();
}
int[] num = new int[n];
int index = 0;
for(int j = 0 ; j < n ; j++) {
boolean junge = false;
for(int k = 0; k < index; k++) {
if(num[k] == array[j]) {
junge = true;
break;
}
}
if(junge == false) {
num[index ] = array[j];
index++;
}
}
Arrays.sort(num);
System.out.println(index);
for(int m = n-index ; m< n ; m++) {
System.out.print(num[m] + " ");
}
scan.close();
}
}
注意:最后打印的时候要从n-index开始,因为排序后前面都是0;
https://www.lanqiao.cn/problems/2408/learning/
解法:暴力+枚举
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static int fun(int num) {
int sum = 0;
while(num != 0 ) {
sum += num % 10;
num /= 10;
}
return sum;
}
public static void main(String[] args) {
int[] day = {0, 31,28,31,30,31,30,31,31,30,31,30,31};
int count = 0;
for( int i = 1900 ; i <= 9999 ; i++ ) {
if((i % 4 == 0 && i % 100 != 0) || i% 400 == 0 ) {
day[2] = 29;
}else {
day[2] = 28;
}
for(int j = 1 ; j < 13 ; j++) {
for(int k = 1 ; k <= day[j] ; k++) {
if(fun(i) == fun(j) + fun(k)) {
count++;
}
}
}
}
System.out.println(count);
}
}