牛客在线OJ自定义输入输出练习

题目1

在这里插入图片描述
在这里插入图片描述



import java.util.Scanner;

/**
 * 输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            System.out.println(a + b);
        }
    }
}

同时while循环也是为了保证能够一直在控制台输入数据,才这么做的

题目2

在这里插入图片描述


import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String s = scanner.nextLine();
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            System.out.println(a + b);
        }
    }
}

在这里插入图片描述
同时while循环也是为了保证能够一直在控制台输入数据,才这么做的

题目3

在这里插入图片描述

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) {
               System.out.println(a + b);
           }else {
               return;
           }
          
       }
   }
}

题目4

在这里插入图片描述

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 sum = 0;

           if(a == 0) {
               continue;
           }
           for(int i = 0 ; i < a ;i++) {
               sum += sc.nextInt();
           }
           System.out.println(sum);
           
          
       }
   }
}

题目5

在这里插入图片描述

import java.util.*;
public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       while(sc.hasNext()) {
           int count = sc.nextInt();
           
           for(int i = 0 ; i < count ; i++) {
               int a = sc.nextInt();
               int sum = 0;

               if(a == 0) {
                  continue;
               }
               for(int j = 0 ; j < a ;j++) {
                  sum += sc.nextInt();
               } 
               System.out.println(sum);
          }
       }
   }
}

题目6

在这里插入图片描述

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 sum = 0;
               if(a == 0) {
                  continue;
               }
               for(int j = 0 ; j < a ;j++) {
                  sum += sc.nextInt();
               } 
               System.out.println(sum);
          }
       }
   
}

题目7

在这里插入图片描述

像没说明有几个测试用例的一般都要用while循环

import java.util.*;
public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       while(sc.hasNext()) {
               int sum = 0;
               String str = sc.nextLine();
               String[] str1 = str.split(" ");  
               for(int i = 0 ; i < str1.length ; i++) {
                   sum += Integer.parseInt(str1[i]);
               }
               System.out.println(sum);
          }
       }
   
}

题目8

在这里插入图片描述

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int sum = Integer.parseInt(in.nextLine());
        String[] arr = in.nextLine().split(" ");
        Arrays.sort(arr);
        for (String e : arr)
            System.out.print(e + " ");
    }
}

题目9

在这里插入图片描述

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) {
            String[] arr = in.nextLine().split(" ");
            Arrays.sort(arr);
            System.out.println(String.join(" ", arr));
        }
    }
}

在这里插入图片描述

题目10

在这里插入图片描述
方法1

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) {
            String[] arr = in.nextLine().split(","); 
            StringBuffer sb = new StringBuffer();
            Arrays.sort(arr);
            for(int i = 0 ; i < arr.length ; i++) {
               
                if(i != arr.length - 1) {
                    sb.append(arr[i]);
                    sb.append(",");
                }else {
                    sb.append(arr[i]);
                }
            }
            System.out.println(sb.toString());
        }
    }
}

方法2

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) {
            String[] arr = in.nextLine().split(",");
            Arrays.sort(arr);
            System.out.println(String.join(",", arr).trim());
        }
    }
}

题目11

在这里插入图片描述

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextLong()){
            long a = in.nextLong();
            long b = in.nextLong();
            System.out.println(a+b);
        }
    }
}

题目12

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值