90分,显示运行出错,我也不知道哪里出错了,使用的是递归
package com.example.administrator.test.ccf;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author kyp
* @time 2019/7/24
* @description
*/
public class _20171202 {
static int num = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//1-1000
int k = scanner.nextInt();//1-9
List<Integer> mLIst = new ArrayList<>();
for (int i = 1; i < n + 1; i++) {//保证数组n,其中0不要
mLIst.add(i);
}
if (n == 1) {
System.out.print(n);
} else {
retNum(mLIst, k, 1);
System.out.print(num);
}
}
public static void retNum(List<Integer> a, int k, int count) {
if (a.size() == 1) {
num = a.get(0);
} else {
List<Integer> mList = new ArrayList<>();
for (int i = 0; i < a.size(); i++) {
if (count % k == 0 || count % 10 == k) {
count++;
} else {
mList.add(a.get(i));
count++;
}
}
retNum(mList, k, count);
}
}
}
下面是大佬的解答,用的是队列,说到底还是基础不够扎实啊,会的话就很快了(https://blog.csdn.net/hunjue0915/article/details/80555920)
package com.example.administrator.test.ccf;
import java.util.ArrayDeque;
import java.util.Scanner;
/**
* @author kyp
* @time 2019/7/25
* @description
*/
public class _20170902 {
public static void main(String[] args) {
ArrayDeque queue = new ArrayDeque();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
for (int i = 0; i < n; i++) {
queue.offer(i + 1);
}
int count = 1;
while (queue.size() > 1) {
int top = (int) queue.poll();
if (count % k != 0 && count % 10 != k) {
queue.offer(top);
}
count++;
}
System.out.println(queue.peek());
}
}