搜狐笔试第一题精简版,ac

输入不写了,主要看代码逻辑

public class Main {

    public static void main(String[] args) {
        int n = 30;
        int m = 4;
        int[] v = new int[]{2,1,3,1};

        int i = 0;
        int j = 0;
        int k = 1;
        int q = 0;
        int[] res = new int[200];
        res[0] = v[0];
        while(i<n){
            if(k<res[i]){
                res[q+1] = res[q];
                k++;
                q++;
            }else{
                k = 1;
                j++;
                i++;
                j%=m;
                res[q+1]=v[j];
                q++;
            }
        }
        for(int p = 0;p<n;p++){
            System.out.println(res[p]);
        }
    }
}
全部评论
能给说说思路吗 谢谢啦
点赞 回复 分享
发布于 2017-08-28 21:20
很棒的思路,谢谢!
点赞 回复 分享
发布于 2017-08-28 22:41
//生成Kolakoski序列 import java.util.Scanner; public class Kolakoski { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] num = new int[m]; for (int i = 0; i < m; i++) { num[i] = sc.nextInt(); } sc.close(); int[] arrSequ = new int[n]; int[] arrLen = new int[n]; arrLen[0] = num[0]; int i = 0, k = 0, s = 0, sumLen = 0; outer: while (true) { for (i = 0; i < arrLen.length; i++) { for (int j = 0; j < arrLen[i]; j++) { arrSequ[s++] = num[k % 4]; if (s >= n) break outer; } sumLen = sumLen + arrLen[i]; ++k; for (int j = sumLen - arrLen[i]; j < sumLen; j++) arrLen[j] = arrSequ[j]; } } for (int j = 0; j < n; j++) { System.out.print(arrSequ[j]+" "); } } }
点赞 回复 分享
发布于 2017-08-28 22:53

相关推荐

字节 飞书绩效团队 (n+2) * 15 + 1k * 12 + 1w
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务