文章目录
- 1. A+B(1)
- 2. A+B(2)
- 3. A+B(3)
- 4. A+B(4)计算一系列数的和
- 5. A+B(5) 计算一系列数的和
- 6. A+B(6)
- 7. A+B(7)
- 8. 字符串排序(1)
- 9. 字符串排序(2)
- 10 字符串排序(2)
- 11. 注意数据范围
1. A+B(1)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
2. A+B(2)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int count = 0;
count = in.nextInt();
for (int i = 0; i < count ; i++){
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
}
}
3. A+B(3)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
if (a != 0 || b != 0) {
System.out.println(a + b);
}
}
}
}
4. A+B(4)计算一系列数的和
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int count = 0;
count = in.nextInt();
if (count != 0) {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
}
}
5. A+B(5) 计算一系列数的和
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++) {
int count = in.nextInt();
int sum = 0;
for (int j = 0; j < count; j++) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
}
6. A+B(6)
计算一系列数的和
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int count = 0;
count = in.nextInt();
int sum = 0;
for (int i = 0; i < count; i++) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
}
7. A+B(7)
计算一系列数的和
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String str = new String();
str = in.nextLine();
String[] str1 = str.split(" ");
int sum = 0;
for(int i = 0; i < str1.length; i++){
int a = Integer.parseInt(str1[i]);
sum += a;
}
System.out.println(sum);
}
}
}
8. 字符串排序(1)
对输入的字符串进行排序后输出
import java.util.Scanner;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
List<String> data = new ArrayList<>();
for(int i = 0; i < n; i++){
data.add(in.next());
}
Collections.sort(data);
System.out.print(String.join(" ", data));
}
}
9. 字符串排序(2)
对输入的字符串进行排序后输出
import java.util.Scanner;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
String str = new String();
str = in.nextLine();
String[] str1 = str.split(" ");
Arrays.sort(str1);
for(int i = 0; i < str1.length; i++){
System.out.print(str1[i] + " ");
}
System.out.println();
}
}
}
或:
import java.util.Scanner;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
String str = new String();
str = in.nextLine();
String[] str1 = str.split(" ");
Arrays.sort(str1);
System.out.println(String.join(" ", str1));
}
}
}
10 字符串排序(2)
对输入的字符串进行排序后输出
import java.util.Scanner;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
String str = new String();
str = in.nextLine();
String[] str1 = str.split(",");
Arrays.sort(str1);
System.out.println(String.join(",", str1));
}
}
}
11. 注意数据范围
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while(in.hasNextLong()) { // 注意 while 处理多个 case
Long a = in.nextLong();
Long b = in.nextLong();
System.out.println(a + b);
}
}
}