商汤报数
第一题思路 二三题刚不动
package algorithm.problems;
import java.util.Scanner;
import org.junit.Test;
/**
- /
class P{
public int num = 0;
public int state = 1;
public P() {
}
public P(int num, int state) {this.num = num; this.state = state;
}
}
public class YSF {
public int[] ysf_1() {int[] idx = new int[2]; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); P[] arr = new P[n]; for(int i = 0; i < n; ++i) { arr[i] = new P(); (arr[i]).num = i + 1; } int CNT_1 = n;//剩多少个1 int cnt = 0; int reset = 0; for(int j = 0; j < n; ++j) { if(reset == 1) cnt = 0;//重置 reset = 0; if(arr[j].state == 1) {//表示在队列中 ++cnt; if(cnt == 1) {//如果报1,就置0 arr[j].state = 0; --CNT_1; }else if(cnt == k){ reset = 1; } if(CNT_1 == 2) {//如果最后剩余2个人了 break;//结束操作 } } if(j == n - 1) j = -1; //下一次循环,可以用HashMap写,把报数为1的删除,可以减小复杂度,不贴了 } int t = 0; for(int r = 0; r < n; ++r) { if(arr[r].state == 1) { idx[t] = arr[r].num; ++t; if(t == 2) break; } } sc.close(); return idx;
}
@Test
public void test() {YSF Ysf = new YSF(); int[] idx = Ysf.ysf_1(); System.out.print(idx[0] + " " + idx[1]);
}
}