更新时间:2022-09-24 22:19:56
1.定义一个数组,每个元素除以第一个元素,得到的商作为被除数对应位置的值
1package test;
2/**
3 * @author cz
4 */
5public class Test2 {
6
7 public static void main(String[] args) {
8 int[] arr = new int[]{12,13,14,66,88,99,55};
9 for (int i = arr.length-1; i >= 0; i--) {
10 arr[i]=arr[i]/arr[0];
11 }
12 for (int i : arr) {
13 System.out.print(i + " ");
14 }
15 }
16}
2.定义两个正整数,求最大公约数,最小公倍数
1package test;
2public class Test1 {
3 public static void main(String[] args) {
4 int m=12;
5 int n=28;
6 int max=(m>n)?m:n;
7 int min=(m<n)?m:n;
8 //最大公约数
9 for(int i=min;i>=1;i--){
10 if(m%i==0 && n%i==0){
11 System.out.println("最大公约数:"+i);
12 break;
13 }
14 }
15 //最小公倍数
16 for(int i=max;i<m*n;i++){
17 if(i%m==0 && i%n==0){
18 System.out.println("最小公倍数:"+i);
19 break;
20 }
21 }
22 }
23}
3. 输入一个整形数组,数组中有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和求所有子数组的和的最大值。要求时间复杂度为O(n)。例如:输入的数组为 1,-1,3,10,-4,7,2,-5, 和最大的子数组为3,10,-4,7,2,因此输出为该子数组的和为18
1package test;
2
3/**
4 * @author cz
5*/
6public class Test4 {
7
8 public static void main(String[] args) {
9 int[]arr=new int[]{1,-2,3,10,-4,7,2,-5};
10 int sum = getGreatestSum(arr);
11 System.out.println(sum);
12
13 }
14
15 public static int getGreatestSum(int[] arr){
16 //定义一个最大和
17 int greatestSum = 0;
18 //判断数组是否存在值
19 if(arr==null||arr.length==0){
20 return 0;
21 }
22 int temp = greatestSum;
23 //遍历数组
24 for (int i = 0; i < arr.length; i++) {
25 //求和
26 temp += arr[i];
27
28 //判断temp是否为负数,如果是负数,放弃之前数的和
29 if(temp<0){
30 temp=0;
31 }
32
33 //与之前定义 的最大值进行比较
34 if(temp > greatestSum){
35 greatestSum=temp;
36 }
37 }
38
39 //判断最大和是否为0,
40 if(greatestSum ==0){
41 //如果为0,赋值第一个元素
42 greatestSum=arr[0];
43 //进行for循环
44 for (int i = 0; i < arr.length; i++) {
45 //将数组中的最大值赋值给greatestSum最大和
46 if(greatestSum <arr[i]){
47 greatestSum=arr[i];
48 }
49 }
50 }
51 return greatestSum;
52 }
53}
4.用java实现输入某年某月某日,判断这一天是这一年的第几天? 1package test;
2
3import java.util.Scanner;
4
5public class Test3 {
6 /**
7 * 输入某年某月某日,判断这一天是这一年的第几天?
8 *
9 */
10 public static void main(String[] args) {
11 int year;
12 int mouth;
13 int day=0;
14 int days;
15 //累计天数
16 int d=0;
17 int e = 0;
18 Scanner scanner = new Scanner(System.in);
19 do {
20 System.out.println("输入年:");
21 year = scanner.nextInt();
22 System.out.println("输入月:");
23 mouth = scanner.nextInt();
24 System.out.println("输入日:");
25 days = scanner.nextInt();
26 if (mouth < 0 || mouth > 12 || days < 0 || days > 31) {
27 System.out.println("input error!");
28 e = 1;
29 }
30
31 /**
32 * 闰年:①:非整百年数除以4,无余为闰,有余为平;②整百年数除以400,无余为闰有余平
33 * 二月:平年28天、闰年29天
34 */
35 if ((year % 100 !=0 &&year % 4 == 0) || (year % 100 == 0 && year%400==0)) {
36 if(mouth==2){
37 day = 29;
38 if(days>day){
39 System.out.println("input error!");
40 e = 1;
41 }else{
42 e = 0;
43 }
44 }
45
46 } else {
47 if(mouth==2){
48 day = 28;
49 if(days>day){
50 System.out.println("input error!");
51 e = 1;
52 }else{
53 e = 0;
54 }
55 }
56 }
57
58 } while (e == 1);
59
60 for (int i = 1; i <mouth; i++) {
61 switch (i) {
62 case 1:
63 case 3:
64 case 5:
65 case 7:
66 case 8:
67 case 10:
68 case 12: {
69 day = 31;
70 break;
71 }
72 case 4:
73 case 6:
74 case 9:
75 case 11: {
76 day = 30;
77 break;
78 }
79 case 2: {
80 /**
81 * 闰年:①:非整百年数除以4,无余为闰,有余为平;②整百年数除以400,无余为闰有余平
82 * 二月:平年28天、闰年29天
83 */
84 if ((year % 100 !=0 &&year % 4 == 0) || (year % 100 == 0 && year%400==0)) {
85 day = 29;
86 } else {
87 day = 28;
88 }
89 }
90 default:
91 break;
92 }
93 d+=day;
94 }
95 System.out.println("这是"+year+"年的"+(d+days)+"天");
96 }
97
98}
5.将字符串中指定部分反转
1package test;
2
3public class Test5 {
4
5 public static void main(String[] args) {
6 String str= "abcdefg";
7 String string = reverseString(str,2,5);
8 System.out.println(string);
9
10 }
11
12 public static String reverseString(String str,int start,int end){
13 char[] array = str.toCharArray();
14 String string = reverseArray(array,start,end);
15 return string;
16 }
17
18 public static String reverseArray(char[] c,int start,int end){
19 for(int x=start,y=end;x<y;x++,y--){
20 char temp = c[x];
21 c[x]=c[y];
22 c[y]=temp;
23 }
24 return new String(c);
25 }
26}