当前位置:   article > 正文

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

java acm输入输出

目录

题号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)

  1. import java.util.Scanner;
  2. public class Main{
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNext()) {
  6. int a = sc.nextInt();
  7. int b = sc.nextInt();
  8. System.out.println(a + b);
  9. }
  10. }
  11. }

题号B: A+B(2)

  1. import java.util.Scanner;
  2. public class Main{
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. int num = sc.nextInt();
  6. while(num!=0){
  7. int a = sc.nextInt();
  8. int b = sc.nextInt();
  9. System.out.println(a+b);
  10. num--;
  11. }
  12. }
  13. }

题号C: A+B(3)

  1. // 方法1
  2. import java.util.Scanner;
  3. public class Main{
  4. public static void main(String[] args){
  5. Scanner sc = new Scanner(System.in);
  6. int a = sc.nextInt();
  7. int b = sc.nextInt();
  8. while(a!=0 && b!=0){
  9. System.out.println(a+b);
  10. a = sc.nextInt();
  11. b = sc.nextInt();
  12. }
  13. }
  14. }
  15. //方法2
  16. import java.util.Scanner;
  17. public class Main{
  18. public static void main(String[] args){
  19. Scanner in = new Scanner(System.in);
  20. while(in.hasNextLine()){
  21. int a = in.nextInt();
  22. int b = in.nextInt();
  23. if(a==0&&b==0){
  24. break;
  25. }
  26. System.out.println(a+b);
  27. }
  28. }
  29. }

题号D: A+B4)

  1. import java.util.Scanner;
  2. public class Main{
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNextLine()){
  6. int num = sc.nextInt();
  7. int sum = 0;
  8. for(int i = 0;i<num;i++){
  9. sum += sc.nextInt();
  10. }
  11. if(num == 0){
  12. break;
  13. }
  14. System.out.println(sum);
  15. }
  16. }
  17. }

题号E: A+B(5)

  1. import java.util.Scanner;
  2. public class Main{
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. int num = sc.nextInt();
  6. while(num-->0){
  7. int a = sc.nextInt();
  8. int sum = 0;
  9. for(int i = 0; i < a; i++){
  10. sum += sc.nextInt();
  11. }
  12. System.out.println(sum);
  13. }
  14. }
  15. }

题号F: A+B(6)

  1. import java.util.Scanner;
  2. public class Main{
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNextInt()){ //或者使用hasNext()
  6. int num = sc.nextInt();
  7. int sum = 0;
  8. while(num-->0){
  9. sum += sc.nextInt();
  10. }
  11. System.out.println(sum);
  12. }
  13. }
  14. }

题号G: A+B(7)

  1. import java.util.Scanner;
  2. public class Main{
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNextLine()){
  6. int sum = 0;
  7. String[] arr = sc.nextLine().split(" ");
  8. for(String str:arr){
  9. sum += Integer.parseInt(str);
  10. }
  11. System.out.println(sum);
  12. }
  13. }
  14. }

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

  1. import java.util.*;
  2. public class Main{
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. int num = sc.nextInt();
  6. while(sc.hasNextLine()){
  7. String[] str = sc.nextLine().split(" ");
  8. Arrays.sort(str);
  9. for(int i = 0;i < str.length-1; i++){
  10. System.out.print(str[i] + ' ');
  11. }
  12. System.out.print(str[str.length-1]);
  13. }
  14. }
  15. }


题号I 字符串排序(2)

  1. //方法1
  2. import java.util.*;
  3. public class Main{
  4. public static void main(String[] args){
  5. Scanner sc = new Scanner(System.in);
  6. while(sc.hasNextLine()){
  7. String[] str = sc.nextLine().split(" ");
  8. Arrays.sort(str);
  9. print(str);
  10. System.out.println();
  11. }
  12. }
  13. public static void print(String[] str){
  14. for(int i = 0;i < str.length;i++){
  15. String output = (i == (str.length-1)) ? str[i] : str[i]+' ';
  16. System.out.print(output);
  17. }
  18. }
  19. }
  20. //方法2
  21. import java.util.*;
  22. public class Main{
  23. public static void main(String[] args){
  24. Scanner sc = new Scanner(System.in);
  25. while(sc.hasNextLine()){
  26. String[] str = sc.nextLine().split(" ");
  27. Arrays.sort(str);
  28. StringBuffer sb = new StringBuffer();
  29. for(String s:str){
  30. sb.append(s).append(" ");
  31. }
  32. System.out.println(sb.substring(0,sb.length()-1)); //去掉最后一个空格
  33. }
  34. }
  35. }

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

  1. //方法1
  2. import java.util.*;
  3. public class Main{
  4. public static void main(String[] args){
  5. Scanner sc = new Scanner(System.in);
  6. while(sc.hasNextLine()){
  7. String[] str = sc.nextLine().split(",");
  8. Arrays.sort(str);
  9. print(str);
  10. System.out.println();
  11. }
  12. }
  13. public static void print(String[] str){
  14. for(int i = 0;i < str.length;i++){
  15. String output = (i == (str.length-1)) ? str[i] : str[i]+',';
  16. System.out.print(output);
  17. }
  18. }
  19. }
  20. //方法2
  21. import java.util.*;
  22. public class Main{
  23. public static void main(String[] args){
  24. Scanner sc = new Scanner(System.in);
  25. while(sc.hasNext()){
  26. String[] str = sc.nextLine().split(",");
  27. Arrays.sort(str);
  28. StringBuilder sb = new StringBuilder();
  29. for(String s:str){
  30. sb.append(s).append(",");
  31. }
  32. System.out.println(sb.deleteCharAt(sb.length()-1).toString());//去掉最后一个分号
  33. }
  34. }
  35. }


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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/419038
推荐阅读
相关标签
  

闽ICP备14008679号