Java几个常用的处理输入的小技巧

Example 1:


Input:

3,3
1 1 1
1 1 1
1 1 1

Description:

第一行的3、3表示二维数组的行和列,相邻元素用逗号分隔,第二行的元素是二维数组中的值


Solution:

import java.util.Scanner;

public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    String[] s=sc.nextLine().split(",");

    int m = Integer.parseInt(str[0]);
    int n = Integer.parseInt(str[1]);
    int[][] arr = new int[m][n];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = sc.nextInt();
        }
    }
    //TODO 处理二维数组
}

Example 2:


Input:

9
1,2,3,4,5,6,7,8,9

Description:

第一行的9表示输入字符串的长度,第二行是字符串的值,相邻元素用逗号分隔


Solution:

import java.util.Scanner;

public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n <= 0) {
            return;
        }
        sc.nextLine();//跳过换行符
        int[] arr = new int[n];
        String[] str = sc.nextLine().split(",");
        for (int i = 0; i < str.length; i++) {
            arr[i] = Integer.parseInt(str[i]);
        }
        //TODO 处理一维数组
}

 


Example 3:


Input:

1 1
1 1

Description:

第一行的1、1表示两个输入a和b,相邻元素空格分隔,第二行也是如此


Solution:

import java.util.Scanner;

public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    while(sc.hasNext()){//多组数据
        int a=sc.nextInt();
        int b=sc.nextInt(); 
        //TODO 处理a和b
        System.out.println(a+b);
    }
}

 


Example 4:


Input:

2
1 1
1 1

Description:

第一行的2表示2组数据,第二行的1、1表示两个输入a和b,相邻元素空格分隔,第三行也是如此


Solution:

import java.util.Scanner;

public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int len=sc.nextInt();
         
    int[] arr=new int[len];
    for(int i=0;i<len;i++){
        int a=sc.nextInt();
        int b=sc.nextInt();
        //TODO 处理a和b
    }
}

Example 5:


Input:

1 1
1 1
0 0

Description:

第一行的1、1表示两个输入a和b,相邻元素空格分隔,第二行也是如此,第三行的0、0表示结束符


Solution:

import java.util.*;
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(); 
            if(a==0&&b==0)
                break;
            //TODO 处理a和b
            System.out.println(a+b);
        }
    }
}

 


Example 6:


Input:

4 1 2 3 4
5 1 2 3 4 5
0

Description:

第一行的4表示输入4个数,后面1,2,3,4表示输入的4个整数,相邻元素空格分隔,第二行也是如此,第三行的0表示结束符


Solution:

import java.util.*;
public class Main{
     public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n;
        while((n=sc.nextInt())!=0){
            int[] arr=new int[n];
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
            }
            //TODO 处理一维数组
        }
    }
}

Example 7:


Input:

2
4 1 2 3 4
5 1 2 3 4 5

Description:

第一行2表示2组数据,第二行的4表示输入4个数,后面1,2,3,4表示输入的4个整数,相邻元素空格分隔,第三行也是如此,


Solution:

import java.util.*;
public class Main{
     public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int len=sc.nextInt();
        for(int i=0;i<len;i++){
            int n=sc.nextInt();
            int[] a=new int[n];
            for(int j=0;j<n;j++){
                a[j]=sc.nextInt();
            }
            //TODO 处理一维数组
        }
    }
}


 


Example 8:


Input:

4 1 2 3 4
5 1 2 3 4 5

Description:

第一行的4表示输入4个数,后面1,2,3,4表示输入的4个整数,相邻元素空格分隔,第二行也是如此,


Solution:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int len=sc.nextInt(); 
            int[] a=new int[len];
            for(int i=0;i<len;i++){
                a[i]=sc.nextInt();
            } 
            //TODO 处理一维数组   
            System.out.println(func(a));
        }
    }
}



Example 9:


Input:

1 2 3
4 5
0 0 0

Description:

第一行的1,2,3表示输入的3个整数,相邻元素空格分隔,第二、三行也是如此,但是它们的长度不是固定的


Solution:

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
        String[] s=sc.nextLine().split(" ");
        int[] a=new int[s.length];
        for(int i=0;i<s.length;i++){
            a[i]=Integer.valueOf(s[i]);
        }
        //TODO 处理一维数组
    }
}



Example 10:


Input:

5
a e f g b

Description:

第一行的5表示输入长度为5的字符串,相邻元素空格分隔,第二行表示5个字符串


Solution:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        sc.nextLine();//跳过换行符
        String[] nums=sc.nextLine().split(" ");
        //TODO 对字符串数组进行处理
        Arrays.sort(nums);
        for(int i=0;i<a;i++){
            if(i==a-1){
              System.out.print(nums[i]);
            }else{
              System.out.print(nums[i]+" ");
            }
        }
    }
}


Example 11:


Input:

a e f g b

Description:

第一行表示5个字符串,相邻元素空格分隔


Solution:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String[] nums=sc.nextLine().split(" ");
            //TODO 对字符串进行操作
            Arrays.sort(nums);
            for(int i=0;i<nums.length;i++){
                if(i==nums.length-1){
                  System.out.println(nums[i]);
                }else{
                  System.out.print(nums[i]+" ");
                }
            }
        }
    }
}


 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值