OJ在线编程常见输入输出练习场题解汇总

题源链接: https://ac.nowcoder.com/acm/contest/5647?from=hr_test#question     

1 A+B(1) 

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();
            System.out.println(a+b);
        }
    }
}

2 A+B(2)  

 

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

3 A+B(3)  

 

 

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) break;
            System.out.println(a+b);
        }
    }
}

4 A+B(4)  

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

5 A+B(5) 

 

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine()); //Integer.parseInt()是把()里的内容转换成整数。Integer.parseInt(String)遇到一些不能被转换为整型的字符时,会抛出异常。
        while(n>0){
            int sum = 0;
            String a = sc.nextLine();
            String[] as = a.split(" ");
            for(int i=1;i<as.length;i++){
                sum = sum + Integer.parseInt(as[i]);
            }
            System.out.println(sum);
            n--;
        }
    }
}

方法二:

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

 

6 A+B(6) 

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

7 A+B(7) 

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

8 字符串排序(1)

import java.util.Scanner;
import java.util.Arrays;
 
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        while(sc.hasNextLine()){
            String a = sc.nextLine();
            String[] as = a.split(" ");
            Arrays.sort(as);
            for(int i=0;i<n;i++){
                System.out.print(as[i]+' ');
            }
//            System.out.println(as[n-1]);
        }
    }
}

9 字符串排序(2)

import java.util.Scanner;
import java.util.Arrays;
 
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String a = sc.nextLine();
            String[] as = a.split(" ");
            Arrays.sort(as);
            for(int i=0;i<as.length;i++){
                System.out.print(as[i]+' ');
            }
            System.out.println("");
        }
    }
}

10 字符串排序(3)

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[] as = s.split(",");
            Arrays.sort(as);
            for(int i=0;i<as.length-1;i++){//每行少输一个
                System.out.print(as[i]+',');
            }
            System.out.println(as[as.length-1]);//补上每行的最后一个     为了避免最后的逗号
        }
    }
}

11 和1类似,但是必须用long类型,否则会出现数组越界

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

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值