编程题(一)

1、 题目:打印出杨辉三角形(要求打印出10行如下图)

              1
            1   1
          1   2   1
        1   3   3   1
     1    4   6   4    1
   1    5   10   10  5   1
......
package com.jzq.test1;

/**
 * 题目:打印出杨辉三角形(要求打印出10行如下图)
     *        1
            1   1
          1   2   1
        1   3   3   1
     1    4   6   4    1
 1    5   10   10   5    1
 .......
 */
public class question1 {
    public static void main(String[] args) {
        int[][] arr = new int[10][10];
        //最左边和最右边的数为1
        for (int i = 0; i < 10; i++) {
            arr[i][i] = 1;
            arr[i][0] = 1;
        }
        //上面两个数相加等于下面的数字
        for (int i = 2; i < 10; i++) {
            for (int j = 1; j < i; j++) {
                arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
            }
        }
        //打印杨辉三角
        for (int i = 0; i < 10; i++) {
            //打印空格
            for (int j = 0; j < 2 * (10 - i) - 1; j++) {
                System.out.print(" ");
            }
            //打印数组中各个数字
            for (int k = 0; k <= i; k++) {
                System.out.print(arr[i][k] + "  ");
            }
            System.out.println();
        }

    }
}

 

2、题目:输入3个数a,b,c,按大小顺序输出。

package com.jzq.test1;

import java.util.Scanner;

/**
 * 题目:输入3个数a,b,c,按大小顺序输出。
 */
public class question2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入3个整数");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        if (a < b) {
            int temp = a;
            a = b;
            b = temp;
        }
        if (a < c) {
            int temp = a;
            a = c;
            c = temp;
        }
        if (b < c) {
            int temp = b;
            b = c;
            c = temp;
        }
        System.out.println("从大到小的顺序输出");
        System.out.println(a + " " + b + " " + c);
    }
}

 

3、题目:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

package com.jzq.test1;

import org.junit.Test;

import java.util.Scanner;

/**
 * 题目:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
 */
public class question3 {
    @Test
    public void test1() {
        int count = 0;
        for (int x = 1; x < 5; x++) {
            for (int y = 1; y < 5; y++) {
                for (int z = 1; z < 5; z++) {
                    if (x != y && y != z && x != z) {
                        count++;
                        System.out.println(x * 100 + y * 10 + z);
                    }
                }
            }
        }
        System.out.println("共有" + count + "个三位数");
    }

 

4、题目:输入某年某月某日,判断这一天是这一年的第几天?

package com.jzq.test1;

import java.util.Scanner;

/**
 * 题目:输入某年某月某日,判断这一天是这一年的第几天?
 */
public class question4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入年,月,日");
        System.out.println("年:");
        int year = scanner.nextInt();
        System.out.println("月:");
        int month = scanner.nextInt();
        System.out.println("日:");
        int day = scanner.nextInt();

        int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int totalDay = 0;

        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
            months[1] += 1;
        }
        if (month == 1) {
            System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + day + "天");
        } else {
            for (int i = 0; i < month - 1; i++) {
                totalDay += months[i];
            }
            totalDay += day;
            System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + totalDay + "天");
        }
    }
}

 

 5、题目:判断101-200之间有多少个素数,并输出所有素数。

package com.jzq.test1;

/**
 * 题目:判断101-200之间有多少个素数,并输出所有素数。
 */
public class question5 {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 101; i < 200; i++) {
            if (isRightNum(i)) {
                System.out.print(i + " ");
                count++;
            }
            if (count % 10 == 0) {
                System.out.println();
            }
        }
        System.out.println("素数的个数为:" + count);

    }

    private static boolean isRightNum(int i) {
        for (int j = 2; j < Math.sqrt(i); j++) {
            if (i % j == 0) {
                return false;
            }
        }
        return true;
    }
}

 

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

package com.jzq.test1;

/**
 * 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。
 * 例如6=1+2+3.编程找出1000 以内的所有完数。
 */
public class question6 {
    public static void main(String[] args) {
        for(int i=1;i<1000;i++){
            int sum =0;
            for(int j=1;j<(i/2+1);j++){
                sum += j;
                if(sum == i){
                    System.out.println(i + "是完数");
                }
            }
            
        }
    }
}

 

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

package com.jzq.test1;

/**
 * 题目:一球从100 米高度自由落下,每次落地后反跳回原高度的一半;
 * 再落下,求它在第10 次落地时,共经过多少米?第10次反弹多高?
 */
public class question7 {
    public static void main(String[] args) {
        float h = 1000;
        float n = 10;
        float sum = h;
        h /= 2;//第一次下落,弹回到最高点
        for (int i = 2; i <= n; i++) {
            //i从2开始,是因为在外层已经计算了第一次返回的h
            sum += h * 2;
            h /= 2;
        }
        System.out.println("总路径是" + sum + "经过10次后的高度" + h);

    }
}

 

8、 题目:输入两个正整数m 和n,求其最大公约数和最小公倍数。

package com.jzq.test1;

import java.util.Scanner;

/**
 * 题目:输入两个正整数m 和n,求其最大公约数和最小公倍数。
 */
public class question8 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入两个数字:");
        int m1 = scanner.nextInt();
        int n1 = scanner.nextInt();
        int m = m1 > n1 ? m1 : n1;
        int n = m1 < n1 ? m1 : n1;
        int sum = m * n;
        while (n != 0) {
            int temp = m % n;
            m = n;
            n = temp;
        }
        System.out.println("最大公约数" + m);
        System.out.println("最小公倍数" + sum / m);

    }
}

 

9、题目:利用递归方法求5!。

package com.jzq.test1;

/**
 * 题目:利用递归方法求5!。
 * 递归公式:f(n)=n*f(n-1)
 */
public class question9 {
    public static void main(String[] args) {
        int start = 5;
        int result = factorial(start);
        System.out.println(result);
    }
    public static int factorial(int i){
        if(i==1){
            return 1;
        }
        return i*factorial(i-1);
    }
}

 

10、题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、正序打印出各位数字。

package com.jzq.test1;

import java.util.Scanner;

/**
 * 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、正序打印出各位数字。
 */
public class question10 {
    public static void main(String[] args) {
        System.out.println("输入一个不多于5位的正整数");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int[] arr = new int[5];
        int i = 0;
        do{
            arr[i] = num % 10;
            num = num /10;
            i++;
        }while (num!=0);
        System.out.println("输入数字串的是"+i+"位数的");
        System.out.println("逆序输出:");
        for(int j = arr.length-1;j>=0;j--){
            System.out.print(arr[j] + " ");
        }
        scanner.close();
    }
}

 

posted @ 2019-08-29 16:09  jet-software  阅读(342)  评论(0编辑  收藏  举报