浙大PAT甲级(1074)java题解

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @program: 浙大PAT甲级1074 java题解(满分25 此题22分 一条测试用例超时)
 * @description:
 * @author: 国宇航
 * @create: 2019-12-31 15:13
 **/
public class onezerosevenfour {
    static class LNode{
        int data;
        int next;
        int address;
        LNode child;
        public LNode(int address,int data,int next){
            this.address = address;
            this.data =data;
            this.next = next;
        }
    }
    //不带头结点反转函数
    public  static LNode reverse(LNode head){
        if(head==null){
            return null;
        }
        if(head.child==null){
            return head;
        }
        LNode pre= null;
        LNode next = null;

        while (head!=null){
            next = head.child;
            head.child = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String b[] = bufferedReader.readLine().split(" ");
        int start=Integer.parseInt(b[0]);
        int sum =Integer.parseInt(b[1]);
        int M = Integer.parseInt(b[2]);
//        System.out.println(sum);
        ArrayList arrayList = new ArrayList();
//        int start = 00000;
        //00000 3 3
//        LNode node1 = new LNode(00000,4,99999);
//        LNode node2 = new LNode(99999,5,68237);
//        LNode node3 = new LNode(68237,6,-1);
        for(int i = 0 ;i arrayList1 = new ArrayList();
        //每次反转之后的链表都加入到arrayList1当中
        //反转所有
        if(count/M<1){
            LNode reverse = reverse(first);
            for (LNode node = reverse; node != null; node = node.child) {
                if (node.child != null) {
                    node.next = node.child.address;
                }
                if (node.child == null) {
                    node.next = -1;
                }
                String address = String.format("%05d",node.address);
                String next = String.format("%05d",node.next);
                if (next.equalsIgnoreCase("-0001") ) {
                    next = "-1";
                }
                System.out.println(address + " " + node.data + " " + next);
            }
            //否则分段反转
        }else {
            for (int i=0;i

浙大PAT甲级(1074)java题解_第1张图片
不知道超时的测试用例是哪个 用java实在是太慢了 而且不适合考试
用C++ vector和取地址等操作可以一步到位 代码行数40以内

你可能感兴趣的:(浙大PAT甲级,java题解)