7-2 试试手气

题目如下:

sz.png

我们知道一个骰子有 6 个面,分别刻了 1 到 6 个点。下面给你 6 个骰子的初始状态,即它们朝上一面的点数,让你一把抓起摇出另一套结果。假设你摇骰子的手段特别精妙,每次摇出的结果都满足以下两个条件:

  • 1、每个骰子摇出的点数都跟它之前任何一次出现的点数不同;
  • 2、在满足条件 1 的前提下,每次都能让每个骰子得到可能得到的最大点数。

那么你应该可以预知自己第 n 次(1≤n≤5)摇出的结果。

输入格式:

输入第一行给出 6 个骰子的初始点数,即 [1,6] 之间的整数,数字间以空格分隔;第二行给出摇的次数 n(1≤n≤5)。

输出格式:

在一行中顺序列出第 n 次摇出的每个骰子的点数。数字间必须以 1 个空格分隔,行首位不得有多余空格。

———————————————————————————————————————————

以下为我的解题思路:

分析题目,得知我们每个骰子每次摇出的点数不同,且除了初始状态外每次为可能摇出的最大值,由此可知,我们需要避免在摇点数时与之前摇出的相同,最好的办法就是将已经摇出的点数移出可能摇得到的点数的集合,这样不论怎么选都不会得到已经出现过的点数;其次,我们还要每次都得到可能得最大点数,若将可能得到的点数按顺序排序,每次移除已经摇出的点数,并不会影响其余可能点数的大小顺序,因此,若从小到大排序,那么末尾的点数一定是未摇出过的最大点数,对此,我将对每个骰子使用链表实现以上功能,具体代码如下:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        LinkedList<Integer>A1=new LinkedList<Integer>();
        for(int i=1;i<=6;i++){
            A1.add(i);//初始骰子点数
        }
        LinkedList<Integer>A2=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A3=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A4=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A5=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A6=(LinkedList<Integer>) A1.clone();//初始化各骰子可能出现点数
        int []B=new int[6];
        Scanner scan=new Scanner(System.in);
        for(int i=0;i<6;i++){
            B[i]=scan.nextInt();//初始骰子点数
        }
        int N=scan.nextInt();
        for(int i=0;i<N;i++){
            A1.remove(B[0]);//使用boolean remove函数移去可能摇出点数中上一次摇出的点数
            B[0]=A1.getLast();

            A2.remove(B[1]);
            B[1]=A2.getLast();


            A3.remove(B[2]);
            B[2]=A3.getLast();


            A4.remove(B[3]);
            B[3]=A4.getLast();
 

            A5.remove(B[4]);
            B[4]=A5.getLast();
  

            A6.remove(B[5]);
            B[5]=A6.getLast();
        }
        for(int i=0;i<6;i++){
            System.out.print(B[i]);
             if(i!=5)
                System.out.print(" ");
        }
        System.out.println();
    }
}

 PTA运行结果:

在IDEA中运行代码,发现报错如下:

 对代码进行逐步调试,发现remove(B[I])删除的是Index=B[i]的数据,而不是数值等于B[i]的数据

在检查函数参数后发现,List的泛型为Integer,那么要删除的数据类型也得是Integer才行,int型会调用的是删除某个结点的数据的remove,而我们需要的是删除某个Integer数据的节点,因此,我们需要调用Integer包装器构造一个Integer变量,其值等于B[i];

debug后代码如下:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        LinkedList<Integer>A1=new LinkedList<Integer>();
        for(int i=1;i<=6;i++){
            A1.add(i);//初始骰子点数
        }
        LinkedList<Integer>A2=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A3=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A4=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A5=(LinkedList<Integer>) A1.clone();
        LinkedList<Integer>A6=(LinkedList<Integer>) A1.clone();//初始化各骰子可能出现点数
        int []B=new int[6];
        Scanner scan=new Scanner(System.in);
        for(int i=0;i<6;i++){
            B[i]=scan.nextInt();//初始骰子点数
        }
        int N=scan.nextInt();
        for(int i=0;i<N;i++){
            Integer a=new Integer(B[0]);
            A1.remove(a);//使用boolean remove函数移去可能摇出点数中上一次摇出的点数
            B[0]=A1.getLast();
            a=new Integer(B[1]);//将a指向新骰子

            A2.remove(a);
            B[1]=A2.getLast();
            a=new Integer(B[2]);

            A3.remove(a);
            B[2]=A3.getLast();
            a=new Integer(B[3]);

            A4.remove(a);
            B[3]=A4.getLast();
            a=new Integer(B[4]);

            A5.remove(a);
            B[4]=A5.getLast();
            a=new Integer(B[5]);

            A6.remove(a);
            B[5]=A6.getLast();
        }
        for(int i=0;i<6;i++){
            System.out.print(B[i]);
             if(i!=5)
                System.out.print(" ");
        }
        System.out.println();
    }
}

PTA运行结果:

PTA打分:

总结:

链表中remove函数中的参数为要与链表类型相同才能移除具体元素,否则只能移去下标为参数的元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值