ACM输入输出

1.输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。

import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws Exception{
        br2();
    }
 public static void br2() throws Exception{
 BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
 BufferedWriter on=new BufferedWriter(new OutputStreamWriter(System.out));
   String str;
     while((str=in.readLine())!=null){
         String[] nums=str.split(" ");
         on.write(Integer.valueOf(nums[0])+Integer.valueOf(nums[1])+"\n");
    }
     on.flush();
     }
}

1.1小结

 用IO输入输出流,

 BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
 BufferedWriter on=new BufferedWriter(new OutputStreamWriter(System.out));

 while((str=in.readLine())!=null)   flush

2. 输入第一行包括一个数据组数t, 接下来每行包括两个正整数a,b(1 <= a, b <= 1000)

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 a=sc.nextInt();
            int b=sc.nextInt();
            System.out.println(a+b);
            len--;
        }
    }
}

2.1小结

其实感觉这里控制台是在不断输入的

3.输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

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);
        }  
    }
}

3.1小结

2的拓展罢了

4.输入数据包括多组。每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。接下来n个正整数,即需要求和的每个正整数。

4.1 小结

其实我在这里最困惑的是怎么换行,后来想到只需要hasNext判断它后边还有即可

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

5.输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。接下来t行, 每行一组数据。每行的第一个整数为整数的个数n(1 <= n <= 100)。接下来n个正整数, 即需要求和的每个正整数。

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.hasNext()){
             int len= sc.nextInt();
             int sum=0;
             while(len>0){
                 sum+= sc.nextInt();
                 len--;
             }
              System.out.println(sum);
                
            }
        }
    }

 7.输入数据有多组, 每行表示一组输入数据。每行不定有n个整数,空格隔开。(1 <= n <= 100)。

7.1小结  Integer.valueOf(字符)

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
         while(sc.hasNext()){
             String st=sc.nextLine();
             String[] s=st.split(" ");
             int sum=0;
            for(String s1:s){
            sum += Integer.valueOf(s1);
        }
             System.out.println(sum);
             
         }}}

8.字符串:多个测试用例,每个测试用例一行。 每行通过空格隔开,有n个字符,n<100

8.1小结  字符串感觉主要用nextLine()

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

         }}}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值