笔试 OJ 在线编程常见输入输出整理

OJ 在线编程常见输入输出

计算 a+b

输入描述

每行两个正整数 a,b,有多行

例子:

1 5

10 20

输出描述

输出 a + b 的结果

例子:

6

30

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

输入描述

每行两个正整数 a,b,空格隔开

例子:

1 5

10 20

输出描述

输出 a + b 的结果

例子:

6

30

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String[] arr=sc.nextLine().split(" ");
            long sum=0;//int时不通过
            for(int i=0;i<arr.length;i++) sum+=Long.valueOf(arr[i]);
            System.out.println(sum);
        }
    }
}

输入描述

第一行是数组个数

第二行开始每行两个正整数 a,b

例子:

2

1 5

10 20

输出描述

输出 a + b 的结果

例子:

6

30

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

输入描述

每行两个正整数 a,b

如果该行位 0 0,则结束输入

例子:

1 5

10 20

0 0

输出描述

输出 a + b 的结果

例子:

6

30

import java.util.Scanner;
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) return;
            System.out.println(a+b);
        }
    }
}

输入描述

有多组数据

每组数据一行,每行的第一个整数为该组数据个数,n 为 0 时结束输入

例子:

4 1 2 3 4

5 1 2 3 4 5

0

输出描述

每组数据输出求和的结果

例子:

10

15

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 result=0;
            if(a==0) return;
            else
                for(int i=0;i<a;i++)
                    result+=sc.nextInt();
            System.out.println(result);
        }
    }
}

输入描述

第一行表示数组个数

每行一组数据,每行的第一个整数为该组数据个数

例子:

2

4 1 2 3 4

5 1 2 3 4 5

输出描述

每组数据输出求和的结果

例子:

10

15

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

输入描述

每行一组数据,每行的第一个整数为该组数据个数

例子:

4 1 2 3 4

5 1 2 3 4 5

输出描述

每组数据输出求和的结果

例子:

10

15

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

输入描述

每行一组数据

例子:

1 2 3 4

1 2 3 4 5

0 0 0 0 0 0

输出描述

每组数据输出求和的结果

例子:

10

15

0

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

字符串排序

输入描述

第一行是字符串个数

第二行是字符串内容

例子:

5

c d a bb e

输出描述

排序后的字符串

例子:

a bb c d e

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String[] arr=new String[n];
        for(int i=0;i<n;i++) arr[i]=sc.next();
        Arrays.sort(arr);
   		for(int i=0;i<n;i++) System.out.print(arr[i]+" ");
    }
}

输入描述

每组一行字符串内容,空格隔开

例子:

a c bb

f dddd

nowcoder

输出描述

排序后的字符串

例子:

a bb c

dddd f

nowcoder

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextLine()){
            String str=sc.nextLine();
            String[] arr=str.split(" ");
            Arrays.sort(arr);
            System.out.println(String.join(" ",arr));
        }
    }
}

输入描述

每组一行字符串内容,逗号隔开

例子:

a,c,bb

f,dddd

nowcoder

输出描述

排序后的字符串

例子:

a,bb,c

dddd,f

nowcoder

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextLine()){
            String str=sc.nextLine();
            String[] arr=str.split(",");
            Arrays.sort(arr);
            System.out.println(String.join(",",arr));
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值