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

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

题目1


2.png2.png

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


2.png

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);
        }
    }
}

2.png

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


题目3


2.png

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

2.png

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

2.png

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

2.png

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

2.png


像没说明有几个测试用例的一般都要用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

2.png

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

2.png

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));
        }
    }
}

2.png


题目10

2.png

方法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

2.png

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

2.png

相关文章
|
关系型数据库 测试技术 数据库
Python 基于人脸识别的实验室智能门禁系统的设计与实现
Python 基于人脸识别的实验室智能门禁系统的设计与实现
|
关系型数据库 Linux PostgreSQL
Linux centos8 docker中安装postgresql12.4及远程访问设置
Linux centos8 docker中安装postgresql12.4及远程访问设置
1223 0
|
存储 Oracle 关系型数据库
Docker-14:Docker安装Oracle11g
Docker 安装 Oracle
3702 1
Docker-14:Docker安装Oracle11g
|
物联网 Linux 流计算
EasyLogger--不一样的打印输出
EasyLogger--不一样的打印输出
|
资源调度 关系型数据库 API
一、next-auth 身份验证凭据-使用电子邮件和密码注册登录
本文是关于如何在Next.js应用中使用next-auth库实现基于电子邮件和密码的注册和登录功能的详细教程,包括环境配置、项目初始化、前后端页面开发、数据库交互以及用户状态管理等方面的步骤和代码示例。
一、next-auth 身份验证凭据-使用电子邮件和密码注册登录
|
存储 API C语言
C语言Log工具推荐-easylogger
C语言Log工具推荐-easylogger
471 1
|
JavaScript
Vue3基础(十)___toRaw___markRaw
本文介绍了Vue 3中`toRaw`和`markRaw`的用法,解释了`toRaw`用于获取`ref`和`reactive`对象的原始数据,而`markRaw`用于阻止Vue将对象转换为响应式数据。文章通过代码示例展示了这两个函数在实际开发中的应用和效果。
259 0
Vue3基础(十)___toRaw___markRaw
|
IDE 开发工具 C++
插件:CLion中使用C/C++ Single File Execution插件编译和运行单个文件
插件:CLion中使用C/C++ Single File Execution插件编译和运行单个文件
1188 0
|
算法 机器人 Java
常用的启发式算法
常用的启发式算法
943 0
|
存储 算法
动态规划与搜索算法
动态规划与搜索算法
305 0