当前位置:   article > 正文

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

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()。

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNextInt()){
  6. int a = sc.nextInt();
  7. int b = sc.nextInt();
  8. System.out.println(a + b);
  9. }
  10. }
  11. }

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

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

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int t = sc.nextInt();
  6. for (int i = 0; i < t; i++) {
  7. int a = sc.nextInt();
  8. int b = sc.nextInt();
  9. System.out.println(a + b);
  10. }
  11. }
  12. }

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

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

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNextInt()){
  6. int a = sc.nextInt();
  7. int b = sc.nextInt();
  8. if(a == 0 && b == 0){
  9. return;
  10. }
  11. System.out.println(a + b);
  12. }
  13. }
  14. }

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

输入数据包括多组。

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

n 为 0 的时候结束输入。

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

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNextInt()){
  6. int n = sc.nextInt();
  7. int sum = 0;
  8. if(n == 0){
  9. return;
  10. }
  11. for(int i = 0;i < n;i++){
  12. sum += sc.nextInt();
  13. }
  14. System.out.println(sum);
  15. }
  16. }
  17. }

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

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

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

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

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

 

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int t = sc.nextInt();
  6. while (sc.hasNextInt()) {
  7. int n = sc.nextInt();
  8. int sum = 0;
  9. int count = 0;
  10. while (sc.hasNextInt() && count < n) {
  11. count++;
  12. sum += sc.nextInt();
  13. }
  14. System.out.println(sum);
  15. }
  16. }
  17. }

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

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

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

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

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNextInt()){
  6. int n = sc.nextInt();
  7. int sum = 0;
  8. for(int i = 0;i < n;i++){
  9. sum += sc.nextInt();
  10. }
  11. System.out.println(sum);
  12. }
  13. }
  14. }

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

每组数据输出求和的结果

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

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while (sc.hasNextLine()) {
  6. String s = sc.nextLine();
  7. String[] strings = s.split(" ");
  8. int sum = 0;
  9. for (int i = 0; i < strings.length; i++) {
  10. sum += Integer.parseInt(strings[i]);
  11. }
  12. System.out.println(sum);
  13. }
  14. }
  15. }

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

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

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

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = sc.nextInt();
  6. while(sc.hasNextLine()){
  7. String s = sc.nextLine();
  8. String[] str = s.split(" ");
  9. Arrays.sort(str);
  10. for(int i = 0;i < str.length;i++){
  11. if (i != str.length - 1) {
  12. System.out.print(str[i] + " ");
  13. } else {
  14. System.out.print(str[i]);
  15. }
  16. }
  17. }
  18. }
  19. }

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

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

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

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while (sc.hasNextLine()) {
  6. String s = sc.nextLine();
  7. String[] strings = s.split(" ");
  8. Arrays.sort(strings);
  9. for(int i = 0;i < strings.length;i++){
  10. if (i != strings.length - 1) {
  11. System.out.print(strings[i] + " ");
  12. } else {
  13. System.out.println(strings[i]);
  14. }
  15. }
  16. }
  17. }
  18. }

总结:

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/394763
推荐阅读
相关标签
  

闽ICP备14008679号