最近在春招实习笔试,发现大多公司都采用ACM模式,故对ACM格式下的输入输出做一个汇总。
ACM(OJ)模式下对于各种输入输出情况的总结(JAVA)_小朱小朱绝不服输的博客-CSDN博客_acm模式java输入输出
类名必须为Main
//阻塞式的
- next()--String,i
- inextInt()--int,
- nextDouble()--double
意思就是如果遇到空格,Tab,Enter,会跳过,直到获得相应类型相应的值!!!!!
//nextLine()
也是以回车符为结束,并且只是以回车符结束,并且会读取回车符。读入整行
数字:
1. 多组空格分割的两个整数 (无行数,组数限制)
2 5
13 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
}
}
2. 多组空格分割的两个整数 (无行数,组数限制)
4
1 3 5 6
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
3. 第一行为组数 (行数限制)
2 //组数
1 5
10 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
4.第一行组数接第一个个数接空格分开的整数 (行数限制,每行有个数限制)
2 //2组
4 1 2 3 4 //4个元素
5 1 2 3 4 5 //5个元素
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for (int i = 0; i < num; i++){
int n = in.nextInt();
while(n-->0){
//
}
}
}
}
5.不定长度的数组
public static void main(String[] args) throws IOException {
// 创建一个BufferedReader对象
Scanner sc=new Scanner(System.in);
// 读取第一行数据
String line = sc.nextLine();
// 将字符串根据空格进行分隔
String[] strings = line.trim().split(" ");
int[] arr=new int[strings.length];
// 分别将其中的每个数值读出
for(int i=0;i<strings.length;i++) {
arr[i]= Integer.parseInt(strings[i]);
}
}
5.先是一个数字代表数组长度,第二行是以逗号分隔的数组
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
String line = sc.next().toString();
// 将字符串根据空格进行分隔
String[] strings = line.trim().split(",");
int[] arr=new int[strings.length];
// 分别将其中的每个数值读出
for(int i=0;i<strings.length;i++) {
arr[i]= Integer.parseInt(strings[i]);
}
int[] ints = reverse(arr);
for (int i : ints) {
System.out.print(i);
}
}
字符串:
1.第一行个数第二行字符串
5
c d a bb e
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
while (in.hasNext()) {
String[] s = in.nextLine().split(" ");
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
}
}
}
}
2.第一行两个数 第二行字符串
6 4
AbcDEf
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
3.第一行是行数,后面是字符串
3
qwe
asd
zxc
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] arr = new String[n];
sc.nextLine();
for(int i=0;i<n;i++){
arr[i]=sc.nextLine();
}
for (String s : arr) {
System.out.println(s);
}
}
链表:
链表的输入输出
public static class ListNode {
// 结点的值
int val;
// 下一个结点
ListNode next;
// 节点的构造函数(无参)
public ListNode() {
}
// 节点的构造函数(有一个参数)
public ListNode(int val) {
this.val = val;
}
// 节点的构造函数(有两个参数)
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[] split = sc.nextLine().split(" ");
int[] arr = new int[split.length];
for(int i=0;i<arr.length;i++){
arr[i]= Integer.parseInt(split[i]);
}
//将数组包装成链表
ListNode head=help(arr);
//输出
while (node!=null){
System.out.print(node.val);
if(node.next!=null){
System.out.print("->");
}
node=node.next;
}
}
public static ListNode help(int[] arr){
ListNode dumpy=new ListNode(-1);
ListNode cur=dumpy;
for(int i=0;i<arr.length;i++){
ListNode node = new ListNode(arr[i]);
cur.next=node;
cur=cur.next;
if(i==arr.length-1){
cur.next=null;
}
}
return dumpy.next;
}