返回顶部
首页 > 资讯 > 后端开发 > JAVA >ACM模式常见输入输出专题(Java版)
  • 599
分享到

ACM模式常见输入输出专题(Java版)

java开发语言 2023-09-15 14:09:47 599人浏览 独家记忆
摘要

目录 题号A: A+B(1) 题号B: A+B(2) 题号C: A+B(3) 题号D: A+B4) 题号E: A+B(5) 题号F: A+B(6) 题号G: A+B(7) 题号H: 字符串排序(1) 题号I 字符串排序(2) 题号G: 字

目录

题号A: A+B(1)

题号B: A+B(2)

题号C: A+B(3)

题号D: A+B4)

题号E: A+B(5)

题号F: A+B(6)

题号G: A+B(7)

题号H: 字符串排序(1)

题号I 字符串排序(2)

题号G: 字符串排序(3)

题目K: 自测本地通过提交为0


题号A: 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);        }    }}

题号B: A+B(2)

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

题号C: A+B(3)

// 方法1import java.util.Scanner;public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        int a = sc.nextInt();        int b = sc.nextInt();        while(a!=0 && b!=0){            System.out.println(a+b);            a = sc.nextInt();            b = sc.nextInt();        }    }}//方法2import java.util.Scanner;public class Main{    public static void main(String[] args){        Scanner in = new Scanner(System.in);        while(in.hasNextLine()){            int a = in.nextInt();            int b = in.nextInt();            if(a==0&&b==0){                break;            }            System.out.println(a+b);        }    }}

题号D: A+B4)

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

题号E: A+B(5)

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

题号F: A+B(6)

import java.util.Scanner;public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNextInt()){ //或者使用hasNext()            int num = sc.nextInt();            int sum = 0;            while(num-->0){                sum += sc.nextInt();            }            System.out.println(sum);        }    }}

题号G: 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[] arr = sc.nextLine().split(" ");            for(String str:arr){                sum += Integer.parseInt(str);            }            System.out.println(sum);        }    }}

题号H: 字符串排序(1)

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

题号I 字符串排序(2)

//方法1import 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(" ");            Arrays.sort(str);            print(str);            System.out.println();        }    }    public static void print(String[] str){        for(int i = 0;i < str.length;i++){            String output = (i == (str.length-1)) ? str[i] : str[i]+' ';            System.out.print(output);        }    }}//方法2import 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(" ");            Arrays.sort(str);            StringBuffer sb = new StringBuffer();            for(String s:str){                sb.append(s).append(" ");            }            System.out.println(sb.substring(0,sb.length()-1)); //去掉最后一个空格}    }}

题号G: 字符串排序(3)

//方法1import 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(",");            Arrays.sort(str);            print(str);            System.out.println();        }    }    public static void print(String[] str){        for(int i = 0;i < str.length;i++){            String output = (i == (str.length-1)) ? str[i] : str[i]+',';            System.out.print(output);        }    }}//方法2import java.util.*;public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            String[] str = sc.nextLine().split(",");            Arrays.sort(str);            StringBuilder sb = new StringBuilder();                        for(String s:str){                sb.append(s).append(",");            }            System.out.println(sb.deleteCharAt(sb.length()-1).toString());//去掉最后一个分号        }    }}


题目K: 自测本地通过提交为0

 注意:取值范围是(0,2X10^10),最大值会超过Integer的范围。

import java.util.*;public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        //注意取值范围,用什么样的容器接数据        while(sc.hasNextLong()){            Long a = sc.nextLong();            Long b = sc.nextLong();            System.out.println(a+b);        }    }}

练习链接:牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com)

来源地址:https://blog.csdn.net/qq_41855453/article/details/131542412

--结束END--

本文标题: ACM模式常见输入输出专题(Java版)

本文链接: https://www.lsjlt.com/news/408748.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作