java打卡day 21 ①洗牌(数组及数学关系)②MP3光标位置(数组+穷举)

目录

①洗牌

②MP3光标位置

①洗牌

刷题链接:洗牌_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=M3K6https://www.nowcoder.com/practice/5a0a2c7e431e4fbbbb1ff32ac6e8dfa0?tpId=85&&tqId=29848&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking

import java.util.*;
public class Main{
     public static void playCard(int[]cards,int n,int k){
         //一共洗k次牌
         for(int i=0;i<k;i++){
             int [] newcards=new int[cards.length];
             //找到数学关系洗牌
             for(int j=0;j<n;++j){
                 //前n张牌的对应关系
                 newcards[2*j]=cards[j];
                 //第n张牌至第2n张牌的对应关系
                 newcards[2*j+1]=cards[j+n];
                 
             }
             //将每一轮洗好的牌赋值给下一次要洗的牌数组
             cards=newcards;
         }
         //输出
         for(int i=0;i<cards.length-1;++i){
             System.out.print(cards[i]+" ");
         }
         System.out.println(cards[cards.length-1]);
     }
    public static  void main(String [] args){
        Scanner sc=new Scanner(System.in);
        int groups=sc.nextInt();
        for(int i=0;i<groups;++i){
            int n=sc.nextInt();
            int k=sc.nextInt();
            int [] cards=new int[2*n];
            for(int j=0; j<2*n; ++j){
                cards[j]=sc.nextInt();
            }
            playCard(cards,n,k);
        }
    }
}

②MP3光标位置

刷题链接:MP3光标位置_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=M3K6https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15?tpId=37&&tqId=21287&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking

import java.util.*;
public class Main {
    public static void mouseMove(String numstr, String orderStr) {
        //歌曲的数量为n
        int n = Integer.parseInt(numstr);
        //将指令存入数组当中
        char[]order = orderStr.toCharArray();
        //定义鼠标所在的位置,初始为1代表鼠标在列表第一位
        int mouse = 1;
        //定义歌曲列表所在的起始位置
        int first = 1;
        //当歌曲总数小于等于4时,
        if (n <= 4) {
            //for循环遍历指令
            for (int i = 0; i < order.length; i++) {
                //光标在第一首歌曲上时,按Up键光标挪到最后一首歌曲
                if (mouse == 1 && order[i] == 'U') {
                    mouse = n;
                    //光标在最后一首歌曲时,按Down键光标挪到第一首歌曲
                } else if (mouse == n && order[i] == 'D') {
                    mouse = 1;
                    //其它情况up则挪动至上一首,D则下一首
                } else {
                    if (order[i] == 'U') {
                        mouse--;
                    } else {
                        mouse++;
                    }
                }
            }
            //输出
            for (int i = 1; i < n; ++i) {
                System.out.print(i + " ");
            }
            //为了换行单独将最后歌曲换行打印
            System.out.println(n);
            System.out.println(mouse);
        }
        //当歌曲的数量大于4
        else {
            for (int i = 0; i < order.length; ++i) {
                //屏幕显示的是第一页(即显示第1 – 4首)时,光标在第一首歌曲上
                //这时first=1;mouse=1;
                if (first == 1 && mouse == 1 && order[i] == 'U') {
                    first = n - 3;
                    mouse = n;
                }
                //屏幕显示最后一页时,光标在最后一首歌曲上,用户按Down键
                else if (first == n - 3 && mouse == n && order[i] == 'D') {
                    first = 1;
                    mouse = 1;
                }
                //一般翻页:屏幕显示的不是第一页时,光标在当前屏幕显示的第一首歌曲时
                else if (first != 1 && mouse == first && order[i] == 'U') {
                    --mouse;
                    --first;
                   // 光标在当前屏幕的最后一首歌时的Down键
                    //****************
                    //这里的逻辑需要重点把握,如果if语句里面是
                    //if(first != 1 && mouse == first+3 && order[i] == 'D')
                    //如果歌曲数目不是4的整数倍,则无法进入上面的第二个逻辑;在该逻辑中
                    //则first+3发生越界,无法执行
                    //改为·n-3则表示页面不在含有四首歌曲的最后一页
                } else if (first != n-3 && mouse == first+3 && order[i] == 'D') {
                    ++mouse;
                    ++first;
                } else if (order[i] == 'U') {
                    --mouse;
                } else if (order[i] == 'D') {
                    ++mouse;
                }
            }
            //输出打印
            for (int i = first; i < first + 3; ++i) {
                System.out.print(i + " ");
            }
            System.out.println(first + 3);
            System.out.println(mouse);
        }
    }
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String a = sc.nextLine();
            String b = sc.nextLine();
            mouseMove(a, b);
        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值