当前位置: 首页 > news >正文

OJ在线编程输入输出(Java版)

练习地址:校招笔试真题_C++工程师、golang工程师_牛客网OJ在线编程常见输入输出练习,笔试真题面试真题在线oj练习,在线编程学习提升求职竞争力,真正的应届生校招实习求职神器。icon-default.png?t=M85Bhttps://www.nowcoder.com/test/27976983/summary#question

1.[编程题]A+B(1)

输出a+b的结果

 可以看到输入和输出是有两行的,有的题目中可能还有三行四行无限行,因此我们要使用while循环来读取数据。

可以看到所有输入都是数字,因此我们就可以选取 hasNextInt()。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}

2.[编程题]A+B(2)

 这个方式给了一次输入一共有几个值,分行分次来进行计算

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}

3.[编程题]A+B(3)

输入数据有多组, 如果输入为 0 则结束输入

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(a == 0 && b == 0){
                return;
            }
            System.out.println(a + b);
        }
    }
}

4.[编程题]A+B(4)

输入数据包括多组。

每组数据一行,每行的第一个整数为这行整数的个数 n。

n 为 0 的时候结束输入。

接下来 n 个正整数,对这 n 个数求和。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            int sum = 0;
            if(n == 0){
                return;
            }
            for(int i = 0;i < n;i++){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}

5.[编程题]A+B(5)

输入的第一行包括一个正整数 t 表示数据组数。(一共有几组)

接下来 t 行, 每行一组数据。

每行的第一个整数为这行整数的个数 n。

接下来 n 个正整数,对这 n 个数求和。

 

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int sum = 0;
            int count = 0;
            while (sc.hasNextInt() && count < n) {
                count++;
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}

6.[编程题]A+B(6)

输入数据有多组, 每行表示一组输入数据。

每行的第一个整数为整数的个数n(1 <= n <= 100)。

接下来n个正整数, 即需要求和的每个正整数。

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

7.[编程题]A+B(7)

每组数据输出求和的结果

 不知道一行有多少个数,因此我们直接一次读取一行,最后把这个数字分开再相加

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

8.[编程题]字符串排序(1)

对输入的字符串进行排序后输出

输入有两行,第一行n 第二行是n个字符串,字符串之间用空格隔开

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(sc.hasNextLine()){
            String s = sc.nextLine();
            String[] str = s.split(" ");
            Arrays.sort(str);
            for(int i = 0;i < str.length;i++){
                if (i != str.length - 1) {
                    System.out.print(str[i] + " ");
                } else {
                    System.out.print(str[i]);
                }
            }
        }
    }
}

9.[编程题]字符串排序(2)

多个测试用例,每个测试用例一行。

每行通过空格隔开,有n个字符,n<100

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            String[] strings = s.split(" ");
            Arrays.sort(strings);
            for(int i = 0;i < strings.length;i++){
                 if (i != strings.length - 1) {
                    System.out.print(strings[i] + " ");
                } else {
                    System.out.println(strings[i]);
                }
            }
        }
    }
}

总结:

1.如果全部是数字while 中可以使用 hasNextInt() 

2.如果全部是字符串,可以一行一行读,使用 hasNextLine() 

3.如果一行中既有字符串又有数字,则可以直接读取一行,hasNextLine() 。定义一个String 类型的数组,最后再按空格 spilt() ,或者其他什么要求进行分割就行。再把String 类型的数字转化成 int 类型 :Integer.parseInt()。

相关文章:

  • Matlab代码批处理中国地面气象日值数据集(2400站点数据集),提取所需省份全部站点数据
  • 链表之头指针、头结点、首元结点、空链表
  • 【Linux】静态库与共享库
  • POI入门
  • 07- 诊断事件diagnostic events的类图关系
  • 【C#】RestSharp踩坑日记
  • 自学5个月软件测试找到一个8k的工作,我的学习方式值得你借鉴
  • 【JavaEE初阶】文件操作 和 IO (下篇)
  • Nebula Studio:部署与连接
  • Redis 学习笔记
  • 萤火虫(FA)算法(附完整Matlab代码,可直接复制)
  • 01背包完全背包学习记录
  • docker安装redis
  • java毕业设计的婚庆策划系统的设计与实现mybatis+源码+调试部署+系统+数据库+lw
  • Pandas数据分析:处理文本数据(str/object)各类操作+代码一文详解(二)
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • CSS 专业技巧
  • EOS是什么
  • Golang-长连接-状态推送
  • JAVA SE 6 GC调优笔记
  • Redis中的lru算法实现
  • Service Worker
  • SQLServer之创建显式事务
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • vue的全局变量和全局拦截请求器
  • 闭包--闭包作用之保存(一)
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 算法---两个栈实现一个队列
  • 我有几个粽子,和一个故事
  • 一份游戏开发学习路线
  • 原生Ajax
  • Java总结 - String - 这篇请使劲喷我
  • 阿里云服务器购买完整流程
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • #define、const、typedef的差别
  • #git 撤消对文件的更改
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (C语言)字符分类函数
  • (附源码)ssm高校实验室 毕业设计 800008
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (每日持续更新)jdk api之FileFilter基础、应用、实战
  • (篇九)MySQL常用内置函数
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • (转)人的集合论——移山之道
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .NET Core 成都线下面基会拉开序幕
  • .net core控制台应用程序初识
  • .NET/C# 编译期间能确定的相同字符串,在运行期间是相同的实例
  • @Autowired和@Resource装配
  • [ Linux ] Linux信号概述 信号的产生