java编程题练习2

版权声明:博客内容为本人自己所写,请勿转载。 https://blog.csdn.net/weixin_42805929/article/details/82157056

【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
/**在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,
取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即
为最大公约数,最小公倍数为两数之积除以最大公约数。* /

import java.util.Scanner;

public class Test6 {
    public static void main(String[] args) {
        int a, b, m = 0, n = 0;
        @SuppressWarnings("resource")
        Scanner s = new Scanner(System.in);
        System.out.print( "输入两个正整数: "); 
        String mn = s.nextLine();
        String[] st = mn.split(" ");
        for (int i = 0; i < st.length; i++) {
            a = Integer.parseInt(st[0]);
            b = Integer.parseInt(st[1]);
            m = Test6.deff(a, b);
            n = a * b / m;
        }
        System.out.println("最大公约数: " + m);
        System.out.println("最小公倍数: " + n);
    }

    public static int deff(int x, int y) {
        int t;
        if(x < y) {
            t = x;
            x = y;
            y = t;
        }
        while(y != 0) {
            if(x == y) {
                return x;
            }else {
                int k = x % y;
                    x = y;
                    y = k;
            }
        }
        return x;
    }
}

【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

import java.util.Scanner;

public class Test7 {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一行字符:");
        String s = sc.nextLine();
        int character = 0, digital = 0;
        int blank = 0, other = 0;
        char[] ch = null;
        ch = s.toCharArray();//将字符串对象中的字符转换为一个字符数组
        for (int i = 0; i < ch.length; i++) {
            if (Character.isLetter(ch[i])) {//字符是否为字母
                character++;
            }else if (Character.isDigit(ch[i])) {//字符是否为数字
                digital++;
            }else if(ch[i] == ' ') {//空格
                blank++;
            }else {//其他
                other++;
            }
        }
        System.out.println("数字个数: " + digital);
        System.out.println("英文字母个数: " + character);
        System.out.println("空格个数: " + blank);
        System.out.println("其他字符个数:" + other);
    }
}

【程序8】
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

import java.util.Scanner;

public class Test8 {
    public static void main(String[] args) {
        long a,sum = 0;
        int i = 0;
        @SuppressWarnings("resource")
        Scanner s = new Scanner(System.in);
        System.out.print("输入数字a的值: ");
        a = s.nextInt();
        System.out.print("输入相加的项数:");
        int n = s.nextInt();
        while (i < n) {
            sum = sum + a;
            a = a * 10 + a;
            i++;
        }
        System.out.println(sum);
    }
}

【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为 “完数 “。例如6=1+2+3.编程;找出1000以内的所有完数。

public class Test9 {
    // 完数等于它的因子之和
    // 分解合数时得到的质数
    //一个自然数的所有的真因子(即除了自身以外的约数)的和恰好等于它本身
    /**
     *  6=1+2+3 
        28=1+2+4+7+14
        496=1+2+4+8+16+31+62+124+248
        8128=1+2+4+8+16+32+64+127+254+508+1016+2032+4064
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("1到1000之间的所有完数:");
        for (int i = 1; i < 1000; i++) {// 遍历1000以内的所有整数
            int t = 0;
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0) {// 满足是i的因子,就累加 
                    t = t + j;
                }
            }
            if(t == i) {// 满足因子之和等于i就打印该完数 
                System.out.print(i+"  ");
            }
        }
    }
}

【程序10】
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

public class Test10 {
    public static void main(String[] args) {
        double h = 100, length = 100;
        for (int i = 1; i < 11; i++) {
            length = length + h;
            h = h / 2;
        }
        System.out.println("第10次,经过路程:" + length);
        System.out.println("第10次,反弹高度:" + h / 2);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42805929/article/details/82157056