1.将数组中的0去掉后返回一个新数组。例如数组int[] arr={1,13,45,5,0,0,16,6,0,25,4,17,6,7,0,15};要求将以上数组中的0项去掉,将不为0的值存入一个新数组,生成新的数组结果为:int[] newArr={1,13,45,5,16,6,25,4,17,6,7,15};

public class Task {
    public static void main(String[] args) {
        int[] arr = { 1, 13, 45, 5, 0, 0, 16, 6, 0, 25, 4, 17, 6, 7, 0, 15 };
        int count = 0;
        for (int i : arr) {
            if (i != 0)
                count++;
        }
        int newArr[] = new int[count];
        for (int i = 0, j = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                newArr[j] = arr[i];
                j++;
            }
        }
        System.out.println(Arrays.toString(newArr));
    }

2.把一个数组倒置输出。

例如:

{1, 5, 7}

倒置后为:

{7, 5, 1}

public class Task {
    public static void main(String[] args) {
        int[] arr = { 1, 13, 45, 5, 0, 0, 16, 6, 0, 25, 4, 17, 6, 7, 0, 15 };
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }
    }
}

3.现在给出两个数组:

  • 数组A:1,7,9,11,13,15,17,19

  • 数组b:2,4,6,8,10

两个数组合并为数组c,按升序排列。

public class Task {
    public static void main(String[] args) {
        int arr1[] = { 1, 7, 9, 11, 13, 15, 17, 19 };
        int arr2[] = { 2, 4, 6, 8, 10 };
        int newLength = arr1.length + arr2.length;
        int arrAdd[] = new int[newLength];

        for (int j = 0; j < arr1.length; j++)
            arrAdd[j] = arr1[j];

        for (int i = arr1.length; i < newLength; i++)
            arrAdd[i] = arr2[i - arr1.length];

        Arrays.sort(arrAdd);
        System.out.println(Arrays.toString(arrAdd));
    }
}

4.将 一组乱序的字符进行排序,然后进行升序和逆序输出。

public class Task {
    public static void main(String[] args) {
        char[] ch = { 'h', 'f', 'e', 'c', 'g', 'd', 'a', 'b' };
        System.out.println("原數組為:" + Arrays.toString(ch));
        Arrays.sort(ch);
        System.out.println("昇序排序為:" + Arrays.toString(ch));
        System.out.print("逆序排序為:");
        for (int i = ch.length - 1; i >= 0; i--) {
            System.out.print(ch[i] + " ");
        }
    }
}

5.将原有积分进行备份,然后赠送每位会员500积分,编写程序输出积分情况

public class Task {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入5位會員的積分:");
        int[] integral = new int[5];
        for (int i = 1; i <= 5; i++) {
            integral[i - 1] = scan.nextInt();
        }
        System.out.println("序號\t歷史積分\t新年積分");
        for (int i = 1; i <= 5; i++) {
            System.out.print(i + "\t");
            System.out.print(integral[i - 1] + "\t");
            System.out.println(integral[i - 1] + 500);
        }
    }
}

6.求出4家店的最低手机价格

public class Task {
    public static void main(String[] args) {
        Scanner scanner = new    Scanner(System.in);
        System.out.println("请输入四家店的价格:");
        int score[]=new int[4];
        for(int i=1;i<=4;i++){
            System.out.print("第"+i+"家店的价格为:");
            score[i-1]=scanner.nextInt();
        }
        Arrays.sort(score);
        System.out.println("最低价格是:"+score[0]);
    }
}

7.歌手打分:在歌唱比赛中,共有10位评委进行打分,在计算歌手得分时,去掉一个最高分,去掉一个最低分,然后剩余的8位评委的分数进行平均, 就是该选手的最终得分。输入每个评委的评分,求某选手的最终得分。

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Task {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请十位评委依次打分:");
        int score[] = new int[10];
        for (int i = 1; i <= 10; i++) {
            System.out.print("第" + i + "位评委打的成绩为:");
            score[i - 1] = scanner.nextInt();
        }
        Arrays.sort(score);
        int sumScore = 0;
        for (int i = 1; i < score.length - 1; i++) {
            sumScore += score[i];
        }
        System.out.println("选手最终得分是:" + sumScore / 8.0);
    }
}

8.随机生成400 个 [6,50]的整数,统计出生成的每个整数的个数。输出入格式如下:

数字:个数

例如:

40:4

50:6

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Task {
    public static void main(String[] args) {
        Random rand = new Random();
        int[] nums = new int[400];
        for (int i = 0; i < 400; i++) {
            nums[i] = rand.nextInt(45) + 6;
        }
        for (int k = 6; k <= 50; k++) {
            int count = 0;
            for (int i : nums) {
                if (k == i)
                    count++;
            }
            if (count != 0)
                System.out.println(k + ":" + count);
        }
    }
}

9.​有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数), 凡报到3的人退出圈子,问最后留下的是原来第几号的那位。思路:n长度的数组,boolean类型。出圈置为false,当最后只有一个true的时候, 就是留下来的人。

import java.util.Arrays;
import java.util.Scanner;
public class Task {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入玩游戏的人数n:");
        int n = scan.nextInt();
        boolean bool[] = new boolean[n];
        Arrays.fill(bool, true);// 全部置为true

        int j = 0; // 报数记号
        int count = 0;// 记置为false的次数
        // 循环n-1次,置为false,最后剩下一个true
        while (count != n - 1) {
            for (int i = 0; i < n; i++) {
                if (bool[i]) {
                    j++;
                    if (j == 3) {
                        bool[i] = false;
                        j = 0;// 报数记号复位为0
                        count++;
                    }
                }
            }
        }
        for (int i = 0; i < bool.length; i++) {
            if (bool[i])
                System.out.println("最后留下的玩家序号为:" + (i + 1));
        }
    }
}

results matching ""

    No results matching ""