算法题(36-->40) 题目:有n个整数,使其前面各数顺序向后移...

【程序36】题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

public static void main(String[] args) {
    int N = 10;
    int[] a = new int[N];
    Scanner s = new Scanner(System.in);
    System.out.println("请输入10个整数:");
    for (int i = 0; i < N; i++) {
        a[i] = s.nextInt();
    }
    System.out.print("你输入的数组为:");
    for (int i = 0; i < N; i++) {
        System.out.print(a[i] + " ");
    }
    System.out.print("\n请输入向后移动的位数:");
    int m = s.nextInt();
    int[] b = new int[m];
    for (int i = 0; i < m; i++) {
        b[i] = a[N - m + i];
    }
    for (int i = N - 1; i >= m; i--) {
        a[i] = a[i - m];
    }
    for (int i = 0; i < m; i++) {
        a[i] = b[i];
    }
    System.out.print("位移后的数组是:");
    for (int i = 0; i < N; i++) {
        System.out.print(a[i] + " ");
    }
}

【程序37】题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("请输入排成一圈的人数:");
    int n = s.nextInt();
    boolean[] arr = new boolean[n];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = true;
    }
    int leftCount = n;
    int countNum = 0;
    int index = 0;
    while (leftCount > 1) {
        if (arr[index] == true) {
            countNum++;
            if (countNum == 3) {
                countNum = 0;
                arr[index] = false;
                leftCount--;
            }
        }
        index++;
        if (index == n) {
            index = 0;
        }
    }
    for (int i = 0; i < n; i++) {
        if (arr[i] == true) {
            System.out.println("原排在第" + (i + 1) + "位的人留下了。");
        }
    }
}

【程序38】题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("请输入一个字符串:");
    String str = s.nextLine();
    System.out.println("字符串的长度是:" + str.length());
}

【程序39】题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("请输入一个正整数 n= ");
    int n = s.nextInt();
    System.out.println("相应数列的和为:" + sum(n));
}

public static double sum(int n) {
    double res = 0;
    if (n % 2 == 0) {
        for (int i = 2; i <= n; i += 2) {
            res += (double) 1 / i;
        }
    } else {
        for (int i = 1; i <= n; i += 2) {
            res += (double) 1 / i;
        }
    }
    return res;
}

【程序40】题目:字符串排序。

public static void main(String[] args) {
    int N = 5;
    String temp = null;
    String[] s = new String[N];
    s[0] = "matter";
    s[1] = "state";
    s[2] = "solid";
    s[3] = "liquid";
    s[4] = "gas";
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            if (compare(s[i], s[j]) == false) {
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
    }
    for (int i = 0; i < N; i++) {
        System.out.println(s[i]);
    }
}

static boolean compare(String s1, String s2) {
    boolean result = true;
    for (int i = 0; i < s1.length() && i < s2.length(); i++) {
        if (s1.charAt(i) > s2.charAt(i)) {
            result = false;
            break;
        } else if (s1.charAt(i) < s2.charAt(i)) {
            result = true;
            break;
        } else {
            if (s1.length() < s2.length()) {
                result = true;
            } else {
                result = false;
            }
        }
    }
    return result;
}

你可能感兴趣的