Java经典编程题50道之三十八

编写一个函数:输入n为偶数时,调用函数求1/2+1/4+...+1/n;当输入n为奇数时,调用函数1/1+1/3+...+1/n。

public class Example38 {
    public static void main(String[] args) {
        double d = sum(3);
        System.out.println("运算结果为:" + d);
    }

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

时间: 2024-08-10 10:04:50

Java经典编程题50道之三十八的相关文章

Java经典编程题50道之三十四

输入3个数a,b,c,按大小顺序输出. public class Example34 {    public static void main(String[] args) {        sort(5, 20, 15);    } public static void sort(int a, int b, int c) {        if (a < b) {            int t = a;            a = b;            b = t;        

Java经典编程题50道之三十五

有一个数组,将其最大的元素与第一个元素交换,最小的元素与最后一个元素交换,然后输出数组. public class Example35 {    public static void main(String[] args) {        int[] a = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };        convertElement(a);    } public static void convertElement(int[] a) {        Syste

Java经典编程题50道之三十二

取一个整数a从右端开始的4-7位. public class Example32 {    public static void main(String[] args) {        cut(123456789);    } public static void cut(long n) {        String s = Long.toString(n);        char[] c = s.toCharArray();        int j = c.length;      

Java经典编程题50道之三十

有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. public class Example30 {    public static void main(String[] args) {        int[] m = { 3, 5, 9, 12, 16, 20, 25, 33 };        addElement(m, 17);    } public static void addElement(int[] a, int n) {        System.ou

Java经典编程题50道之三十九

写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. public class Example39 {    public static void main(String[] args) {        length("Hello World!");    } public static void length(String s) {        int n = s.length();        System.out.println("输入的字符

Java经典编程题50道之三十六

有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数. public class Example36 {    public static void main(String[] args) {        int[] m = { 18, 12, 23, 34, 95, 76, 57, 28, 9 };        moveElement(m, 5);    } public static void moveElement(int[] m, int n) {        

Java经典编程题50道之十八

两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人,以抽签决定比赛名单.有人向队员打听比赛的名单:a说他不和x比,c说他不和x. z比.请编程序找出三队赛手的名单. public class Example18 {    public static void main(String[] args) {        vs();    } public static void vs() {        char[] m = { 'a', 'b', 'c' };      

Java经典编程题50道之四十八

某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 每位数字都加上5,然后用和除以10的余数代替该数字, 再将第一位和第四位交换,第二位和第三位交换. public class Example48 {    public static void main(String[] args) {        f(2345);    } public static void f(int num) {        int[] c = new int[4];      

Java经典编程题50道之五十

有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud"中. public class Example50 {    public static void main(String[] args) {        stud();    } public static void stud() {        Scanner ss = new Scanner(System.in);