1.想法:先将数位之和求出,再比大小排序
2.遇到的问题
首先在求数位和的时候有点忘了,当耽误了点时间
还有在排序的时候,只想到了用冒泡来排序
3.测试结果就是超时了,因为采取的双重for循环
4.解题收获,像数组集合里面自带的一些方法往往比较高效,用起来也比较方便,要多熟悉熟悉
5.
public static void main(String[] args) {
int m, n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++) {
array[i] = i + 1;
}
//用集合的排序方法排序
Arrays.sort(array, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Sum(o1) != Sum(o2) ? Sum(o1) - Sum(o2) : o1 - o2;
}
});
System.out.println(array[m - 1]);
}
private static int Sum(Integer i) {
int sum=0;
while (i!=0){
sum+=i%10;
i=i/10;
}
return sum;
}
6.错误原因效率太低,耗时长
public static void main(String[] args) {
int m, n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i]=i;
}
for (int j = 1; j < arr.length; j++){
for (int i = 1; i+1 < arr.length-j; i++) {
if (sum(arr[i])>sum(arr[i+1])){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}else if (sum(arr[i])==sum(arr[i+1])){
if (arr[i]>arr[i+1]){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
}
System.out.println(arr[m]);
}
private static int sum(int i) {
int sum=0;
while (i!=0){
sum+=i%10;
i=i/10;
}
return sum;
}