牛客在线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

相关文章
|
自然语言处理 安全 C++
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
11598 4
|
8月前
|
人工智能 物联网 API
以一杯奶茶为例,带你读懂这35个大模型核心概念
用“奶茶制作”类比大模型核心概念,生动解析数据集、预训练、Token、Prompt、RLHF、RAG、LoRA、多模态、AI Agent等40+术语,轻松理解技术本质,告别名词焦虑!
873 3
|
缓存 Linux 开发工具
CentOS 7- 配置阿里镜像源
阿里镜像官方地址http://mirrors.aliyun.com/ 1、点击官方提供的相应系统的帮助 :2、查看不同版本的系统操作: 下载源1、安装wget yum install -y wget2、下载CentOS 7的repo文件wget -O /etc/yum.
280444 0
|
12月前
|
运维 Linux Python
linux命令
本文整理了Linux系统常用命令,涵盖系统版本、CPU、内存信息查看,及文件操作、文本处理(grep/sed/awk)、压缩解压、进程后台运行等实用技巧,包含别名、通配符、重定向、正则表达式及nohup用法,适合日常运维与自动化脚本编写,提升效率。
486 112
|
存储 监控 关系型数据库
B-tree不是万能药:PostgreSQL索引失效的7种高频场景与破解方案
在PostgreSQL优化实践中,B-tree索引虽承担了80%以上的查询加速任务,但因多种原因可能导致索引失效,引发性能骤降。本文深入剖析7种高频失效场景,包括隐式类型转换、函数包裹列、前导通配符等,并通过实战案例揭示问题本质,提供生产验证的解决方案。同时,总结索引使用决策矩阵与关键原则,助你让索引真正发挥作用。
|
9月前
|
存储 弹性计算 缓存
阿里云服务器地域、实例规格、镜像、云盘、购买时长及带宽选择注意事项
本文为新手用户提供了详尽的阿里云服务器选购指南,涵盖地域选择、实例规格、操作系统、云盘配置、购买时长及带宽规划等六个方面。通过考虑目标用户群体、备案需求、服务互通性等因素,帮助用户选择适合的地域;根据业务特点和性能需求,挑选合适的实例规格和操作系统;平衡性能与成本,选择适宜的云盘配置;结合预算、长期规划及业务需求,确定购买时长。
601 14
阿里云服务器地域、实例规格、镜像、云盘、购买时长及带宽选择注意事项
|
9月前
|
缓存 供应链 搜索推荐
外贸B2B网站建设实践:从架构设计到转化链路的技术思考(2026)
本文聚焦B2B外贸独立站建设,从网站架构、SEO优化、交互设计及数据追踪四大技术维度,剖析模板化、插件驱动与数据驱动三类建站模式的优劣,并提出信任信息结构化展示、简洁询盘设计、系统集成等关键实践,助力企业高效获客。(239字)
496 1
外贸B2B网站建设实践:从架构设计到转化链路的技术思考(2026)
|
9月前
|
数据采集 JSON API
从踩坑到高效落地:关键词搜索京东商品列表API的实操心得
本指南聚焦京东商品列表API实操,详解jd.item_search接口调用要点:涵盖必填参数(app_key、timestamp、sign等)、关键词/分页/价格筛选配置及核心响应字段(SKU、标题、售价、销量等),助开发者快速对接,高效获取合规商品数据。(239字)
614 22
|
算法 Java Go
Go内存原理-GC原理
本文介绍了Go语言中垃圾回收(GC)机制的发展与实现原理,涵盖从标记-清除算法到三色标记法,再到三色标记加混合写屏障的演进过程,重点解析各版本GC的核心思想、优缺点及性能优化方向。
612 4
|
11月前
|
存储 弹性计算 监控
游戏公司不愿看到的代码:为《英雄联盟》《三角洲》打造高稳定代练业务系统源码与架构解析!
代练行业痛点如何破?基于UNIAPP+PHP轻量架构,低成本实现高并发稳定运行,阿里云全栈部署月成本不足20元,支持《英雄联盟》《三角洲行动》等大型赛事流量,开源方案助你快速搭建合规、高效、防封的代练系统。
922 1
游戏公司不愿看到的代码:为《英雄联盟》《三角洲》打造高稳定代练业务系统源码与架构解析!