赞
踩
目录
- 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);
- }
- }
- }
- 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--;
- }
- }
- }
-
- // 方法1
- import 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();
- }
- }
- }
-
- //方法2
- import 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);
- }
- }
- }
- 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<num;i++){
- sum += sc.nextInt();
- }
- if(num == 0){
- break;
- }
- System.out.println(sum);
- }
- }
- }
- 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);
- }
- }
- }
- 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);
- }
- }
- }
-
- 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);
- }
- }
- }
- 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]);
- }
- }
- }
- //方法1
- import 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);
- }
- }
- }
-
- //方法2
-
- import 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)); //去掉最后一个空格
- }
- }
- }
- //方法1
- import 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);
- }
- }
- }
-
-
- //方法2
- import 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());//去掉最后一个分号
- }
- }
- }
注意:取值范围是(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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。