[編程題] 牛客輸入輸出練習Demo-計算a+b(多組輸入)


[編程題] 牛客輸入輸出練習1-計算a+b(多組輸入)]

1、牛客輸入輸出練習1-計算a+b(多組輸入)

題目描述

計算多組輸入的a+b

示例

輸入輸出描述:

輸入描述:

輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入數據包括多組。

輸出描述:

輸出a+b的結果

例如2:

輸入:

1 5
10 20

輸出:

6
30

Java代碼

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> lis = new ArrayList<Integer>();
        while(scanner.hasNextLine()){
            String[] lines = scanner.nextLine().split(" ");
            int a = Integer.parseInt(lines[0]);
             int b = Integer.parseInt(lines[1]);
            lis.add(a+b);
        }
        //輸出
        for(Integer i : lis){
            System.out.println(i);
        }
    }
    
    
}

輸出

已AC

2、牛客輸入輸出練習1-計算a+b(多組輸入)

題目描述

image-20200713122104714

Java代碼(方法1)

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

Java代碼(方法2)

import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws Exception{
        //Scanner scanner = new Scanner(System.in);
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int t =Integer.parseInt(bf.readLine().trim());
        String line = null;
        while(--t>=0){
            line = bf.readLine();
            int a = Integer.parseInt(line.trim().split(" ")[0]);
            int b = Integer.parseInt(line.trim().split(" ")[1]);
            System.out.println(a+b);
        }
    }
}

3、客輸入輸出練習-計算a+b(多組輸入)

題目描述

image-20200713123811239

Java代碼-方法1

import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            String[] line = scanner.nextLine().trim().split(" ");
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);
            if(a==0 && b==0){
                break;
            }else{
                System.out.println(a+b);
            }
            
        }
    }
} 

Java方法2

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

Java方法3

import java.util.*;
public class Main{
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        
        while(scanner.hasNextLine()){
            String[] line = scanner.nextLine().trim().split(" ");
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);
            if(a==0 && b==0){
                break;
            }else{
                System.out.println(a+b);
            }
            
        }
    }
}
    
    

4、輸入輸出練習4

題目描述

image-20200713133920288

java方法1:按行讀取的方式

(list使用Stream求和;截取部分數組)

import java.util.*;

public class Main{
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        
        //按行輸入的方法
        while(scanner.hasNextLine()){
            String[] line = scanner.nextLine().trim().split(" ");
            
            int first = Integer.parseInt(line[0]);
            if(first==0){
                break;
            }
            
            ArrayList<Integer> lis = new ArrayList<Integer>();
            for(String s : line){
                lis.add(Integer.parseInt(s));
            }
            
            //去除首
            lis.remove(0);
            //對list求和
            int sum = lis.stream().reduce(Integer::sum).orElse(0);
            System.out.println(sum);   
        }
    }
}

Java方法:按單獨的字符讀取

(借用Stream轉int[] to List; 借用Stream對List求和)

知識點

//數組轉List
List<Integer> l = Arrays.stream(arr).boxed().collect(Collectors.toList());
//list求和
int sum = l.stream().reduce(Integer::sum).orElse(0);

Java代碼

import java.util.*;
import java.util.stream.Collectors;

public class Main{
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        
        //按行輸入的方法
        while(scanner.hasNextInt()){
            int first = scanner.nextInt();
            if(first==0){
                break;
            }else{
                int[] arr = new int[first];
                for(int i=0;i<first;i++){
                    arr[i] = scanner.nextInt();
                }
                //數組轉List
                List<Integer> l = Arrays.stream(arr).boxed().collect(Collectors.toList());
                //list求和
                int sum = l.stream().reduce(Integer::sum).orElse(0);
                System.out.println(sum);
            }
        }
    }
           
}
    

Java方法3:-計數

import java.util.*;
import java.util.stream.Collectors;

public class Main{
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        while(true){
             int sum=0;
            int fisrt = scanner.nextInt();
            if(fisrt==0){
                return;
            }else{
                while(fisrt!=0){
                    sum += scanner.nextInt();
                    fisrt--;
                }
                System.out.println(sum);
            }
        }
    }         
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM