CCF(Java)之系列游戏(_20171202)

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());

    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值