ACM java 输入输出

程序框架

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 判断接下来是否还有输入
		while (sc.hasNext()) {
			int n1 = sc.nextInt(); // 读一个数字
			Long n2 = sc.nextLong(); // Long 型
			sc.nextLine(); // 读取掉换行符
			String s = sc.nextLine(); // 读整行,包括回车符
			int m = Integer.valueOf(sc.nextLine()); // 将本行读取的String 转换为数字
			String[] ss = sc.nextLine().split(" "); // 将本行读取的String 根据空格分割成 String 数组
		}
		System.out.println();
	}
}

构建数据结构

数组

  • 输入 [1,2,3,4]
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String imput = sc.nextLine();
		String[] parts = s.replace("[").replace("]").split(",");
		int[] ans = new int[parts.length];
		for (int i = 0; i < parts.length; i++) {
			ans[i] = Integer.valueOf(parts[i]);
		}
		// System.out.println(Arrays.toString(ans));
	}
}

链表

  • 输入 [1,2,3,4]
import java.util.*;

public class Main {
	static class ListNode {
		int val;
		ListNode next;
		ListNode(int val) {this.val = val;}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] arr  = str.replace("[").replace("]").split(",");
		ListNode head = StringToListNode(arr);
		printListNode(head);
	}
	private static ListNode StringToListNode(String[] arr) {
		// 尾插法创建链表
		ListNode dummyHead = new ListNode(-1);
		ListNode cur = dummyHead;
		for (int i = 0; i < arr.length; i++) {
			cur.next = new ListNode(Integer.valueOf(arr[i]));
			cur = cur.next;
		}
		return dummyHead.next;
	}
	private static void printListNode(ListNode head) {
		ListNode cur = head;
		while (cur != null) {
			System.out.print(cur.val + " ");
			cur = cur.next;
		}
	}
}

二叉树

  • 输入 [10,5,-3,3,2,null,11,3,-2,null,1]
  • 层序输入
  • 数组预处理方式创建二叉树
import java.util.*;

public class Main {
	static class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode (int val) {this.val = val;}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();
		TreeNode root = stringToTreeNode(input);
		printTree(root);
	}
	
	public static TreeNode stringToTreeNode(String input) {
		String[] arr = input.replace("[","")
                        .replace("]","")
                        ,replace(" ", "")
                        .split(",");
		TreeNode root = null;
		
		// 将输入的数组转换为二叉树数组,如果有 null ,就在数组中存  null
		List<TreeNode> list = new ArrayList<>();
		for (int i = 0; i < arr.length; i++) {
			TreeNode node = null;
			if ("null".equals(arr[i])) {
				list.add(node);
			} else {
				node = new TreeNode(Integer.valueOf(arr[i]));
				list.add(node);
			}
			if (i == 0) root = node; // 根节点
		}
		
		// 构造二叉树
		for (int i = 0; 2 * i + 2 < arr.length; i++) {
			TreeNode node = list.get(i);
			if (node != null) {
				node.left = list.get(2 * i + 1);
				node.right = list.get(2 * i + 2); 
			} 
		}
		return root;
	}
	
	public static void printListNode(TreeNode root) {
		if (root == null) return;
		ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
		LinkedList<TreeNode> que = new LinkedList<>();
		que.offer(root);
		while (!que.isEmpty()) {
			ArrayList<Integer> list = new ArrayList<>();
			int size = que.size();
			for (int i = 0; i < size; i++) {
				TreeNode node= que.poll();
				list.add(node.val);
				if (node.left != null) {
					que.offer(node.left);
				}
				if (node.right != null){
                    que.offer(node.right);
                }
			}
			ans.add(list);
		}
		for (int i = 0; i < ans.size(); i++) {
			for (int j = 0; j < ans.get(i).size(); j++) {
				System.out.print(ans.get(i).get(j) + " ");	
			}	
		}
	}
}

快读

  • BufferedReader
  • PrintWriter
import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter wr = new PrintWriter(new OutputStreamWriter(System.out));
		String str = br.readLine(); // 一次读一行
		String[] arr = str.split(" ");
		int a1 = Integer.valueOf(arr[0]);
        int a2 = Integer.valueOf(arr[1]);

		wr.println("..."); 
		// wr.printf("%.2f", num);
        wr.flush();
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值