首页 > 360笔试第二题
头像
浪波激泥
编辑于 2020-08-22 21:14
+ 关注

360笔试第二题

操作一:把第一个数放在最后
操作二:把12  34  翻转 21 43 依次类推

只能55%。实在不知道怎么优化了,
求AC大神指点😥
可能不能用链表
import java.util.Scanner;
public class Main {

    static class Node{
        int node;
        Node next;
        Node(int node){
          this.node=node;
        }
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();

        if(n==2){
            if(m%2==0){
                System.out.println("1 2");
            }
            else System.out.println("2 1");
            return;
        }

        Node  head=new Node(1);
        Node  end=head;
        for(int i=2;i<=n;i++){
            Node  n1=new Node(i);
            n1.next=null;
            end.next=n1;
            end=n1;
        }

        for(int i=0;i<m;i++){
           int a= sc.nextInt();
            if(a==1){
                Node  n1=head;
                head=head.next;
                end.next=n1;
                end=n1;
                end.next=null;
            }
            else{
                Node  n1=head;
                while(n1!=null){
                    int temp=n1.node;
                    n1.node=n1.next.node;
                    n1.next.node=temp;
                    n1=n1.next.next;
                }
            }
        }
        Node  n1=head;
        while(n1!=null){
            System.out.print(n1.node+" ");
            n1=n1.next;
        }
    }
}


全部评论

(9) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐