王玉辰


私信TA

用户名:1652885487

访问量:8808

签 名:

DFS还是香啊

等  级
排  名 213
经  验 6197
参赛次数 18
文章发表 26
年  龄 0
在职情况 学生
学  校 新东方挖掘机汽修学院
专  业 计算机应用

  自我简介:

解题思路:
如果按照正常做法来做肯定会时间超限 所以这里我用的是数学公式求解

详细公式的递推思路可以去 https://cloud.tencent.com/developer/article/1164728看解法三

注意事项:

使用递归函数会占用计算机较多的内存,当递归层次太深时可能导致程序不能执行。因此,这里可以将程序直接编写为以下的递推形式:
参考代码:

//正常写法(会超时)
Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n=scanner.nextInt();
            int m=scanner.nextInt();
            List<Integer> list=new ArrayList<Integer>();
            for (int i = 1; i <= n; i++) {
                list.add(i);
            }
            int k=0;
            while (list.size()!=1) {
                for (int i = 0; i < m; i++) {
                    if (k>=list.size()) {
                        k=0;
                    }
                    if (i==m-1) {
                        list.remove(k);
                        break;
                    }
                    k++;
                }
            }
            System.out.println(list.get(0));


使用了数学公式求解

Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n=scanner.nextInt();
            int m=scanner.nextInt();
            int num=0;
            for (int i = 2; i <=n; i++) {
				num=(num+m)%i;
			}
            System.out.println(num+1);
        }


 

0.0分

8 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

此法甚妙。
2020-10-13 14:06:12
2020-10-08 15:59:47
大佬厉害厉害
2020-10-07 15:31:05
这个%号妙啊
2020-10-07 14:35:54
np
2020-10-07 14:16:33
  • «
  • 1
  • »