商汤报数

第一题思路 二三题刚不动
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]);
    }
    }
#笔试题目##商汤科技#
全部评论
一二AC 第三题70% 四题凉凉 选择题全是概率 机器学习 神经网络等数学题 基本不会
点赞 回复 分享
发布于 2019-08-19 22:56

相关推荐

昨天 13:14
已编辑
中山大学 算法工程师
牛客849740264号:友友,要不要看看我们这里的算法岗?现在是急招岗,最后3天截至,欢迎来试一试呀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务