ACM模式各种输入输出情况汇总(JAVA)

最近在春招实习笔试,发现大多公司都采用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;

        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值