题目一
代码
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String str=input.nextLine();//nextLine()方法遇到回车结束输入,next()遇到空格
int[]a=new int[10];//用来计数
a[0]=-1;
for(int i=0;i<str.length();i++){
if(str.charAt( i )=='0')
a[0]++;
else if(str.charAt( i )=='1')//charAt(i)获取字符方法
a[1]++;
else if(str.charAt( i )=='2')
a[2]++;
else if(str.charAt( i )=='3')
a[3]++;
else if(str.charAt( i )=='4')
a[4]++;
else if(str.charAt( i )=='5')
a[5]++;
else if(str.charAt( i )=='6')
a[6]++;
else if(str.charAt( i )=='7')
a[7]++;
else if(str.charAt( i )=='8')
a[8]++;
else if(str.charAt( i )=='9')
a[9]++;
else
;
}
for(int i=0;i<10;i++){
System.out.printf("%-2d",i);
}
System.out.println();
for(int i=0;i<10;i++){
System.out.printf("%-2d",a[i]);
}
}
}
输入输出
1 23 53 87 90 12 15 89 0
0 1 2 3 4 5 6 7 8 9
1 3 2 2 0 2 0 1 2 2
题目二
代码
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int locks[]=new int[n];//动态初始化
// 进行n轮循环
for (int i = 1; i <= n; i++) {
// 对每隔i把锁进行操作
for (int j = i - 1; j < n; j += i) {
// 如果锁的状态为关闭,则将其状态改为打开
if (locks[j] == 0) {
locks[j] = 1;
}
// 如果锁的状态为打开,则将其状态改为关闭
else {
locks[j] = 0;
}
}
}
int cnt=0;//记打开锁的个数
for (int i = 0; i < n; i++) {
if (locks[i] == 1) {
cnt++;
}
}
for (int i = 0; i < n; i++) {
if (locks[i] == 1) {
System.out.print(i+1);
cnt--;
if(cnt>0)
System.out.printf(" ");
}
}
}
}
输入输出
8
1 4
题目三
代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
if (isAscendingOrder(arr)) {
System.out.println("The list is already sorted.");
} else {
System.out.println("The list is not sorted");
Arrays.sort(arr);//排序方法参考的课件
for(int i=0;i<n;i++){
System.out.print(arr[i]);
if(i<n-1)
System.out.printf(" ");
}
System.out.println();
}
}
public static boolean isAscendingOrder(int[] arr) {//函数声明方法参考网络
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
return false;
}
}
return true;
}
}
输入输出
8 10 1 5 16 61 9 11 1
The list is not sorted
1 1 5 9 10 11 16 61
题目四
代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//数组1
int n = input.nextInt();
int[] arrN = new int[n];
for (int i = 0; i < n; i++) {
arrN[i] = input.nextInt();
}
//数组2
int m = input.nextInt();
int[] arrM = new int[m];
for (int i = 0; i < m; i++) {
arrM[i] = input.nextInt();
}
//合并到数组3
int x = n + m;
int[] arr = new int[x];
//合并
System.arraycopy(arrN, 0, arr, 0, n);//方法参考课件+CSDN
System.arraycopy(arrM, 0, arr, n, m);
//排序
Arrays.sort(arr);
//打印
System.out.print("The merged list is ");
for(int i = 0; i < x; i++){
System.out.print(arr[i]);
if(i<x-1)
System.out.printf(" ");
}
}
}
输入输出
5 1 5 16 61 111 4 2 4 5 6
The merged list is 1 2 4 5 5 6 16 61 111