首页 > 技术文章 > java 算法练习

yiMro 2020-08-14 17:23 原文

/**
         * 打印99乘法
         */
        for (int i = 1; i <= 9; i++){
            for (int j = 1; j <= i; j++){
                System.out.print(i+"*"+j+"="+i*j);
            }
            System.out.println();
        }
 /**
         * 定义一个二维数组,int[2][4],要求是循环输入8个整数,存入到数组中,然后输出这个数组中的最大值。
         */
        int[][] str =new int[2][4];
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < 2; i++){
            for (int j = 0; j < 4; j++){
                System.out.println("输入第"+(i+1)+"行"+(j+1)+"个数");
                str[i][j] = in.nextInt();
            }
        }

        int max = str[0][0];
        int temp;
        for (int c = 0; c < 2; c++){
            temp = 0;
            for (int v = 0; v < 4; v++){
                if (max < str[c][v]){
                    temp = max;
                    max = str[c][v];
                    str[c][v] = temp;
                }
            }
        }
        System.out.println("最大数:"+ max);
 /**
         * 定义一个长度为10的整型数组,循环输入10个整数。然后判断这个数组中有几个偶数,再定义一个正好能存放这几个偶数的数组,将上一个数组中的所有偶数复制过来。最后循环输出这些偶数。
         */
        int [] str = new int[10];
        int count = 0;
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < str.length; i++){
            System.out.println("第"+(i+1)+"条数据");
            str[i] = in.nextInt();
            if (str[i] % 2 == 0){
                count ++;
            }
        }

        int[] str2 = new int[count];
        int ev = 0;
        for (int l = 0; l < str.length; l++){
            if (str[l] % 2 == 0){
               str2[ev] = str[l];
               ev ++;
            }
        }

        for (int j = 0; j < str2.length; j++){
            System.out.print(str2[j]+"\t");
        }
/**
         * 输入一个数判断一个数是否为质数(只能被1和本身整除的数叫质数)
         */
        Scanner in = new Scanner(System.in);
        System.out.println("输入一个数");
        int num = in.nextInt();
        int count = 0;
        for (int i = 1; i <= num; i++){
            if (num % i == 0){
                count ++;
            }
        }

        if (count == 2){
            System.out.println(num+"是质数");
        }else {
            System.out.println(num+"不是质数");
        }
 /**
         * 计算1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10=?
         */
        long sum = 1;
        for (int i = 2; i < 11; i++){
            sum += 1/i;
        }
        System.out.println(sum);
/**
             * 给一个数组做反序。
             */
            int[] array = new int[]{13,0,6,42,8,24,9,10};
            int start = 0;
            int end = array.length-1;  
            for (; start < end; start++) {
                int temp = array[start];
                array[start] = array[end];
                array[end] = temp;
                end--;
            }
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + "\t");
            }
 /**
         * 求1-100以内所有的质数
         */
        for (int i = 1; i <= 100; i++) {
            int count = 0;
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0) {
                    count++;
                }
            }
            if (count < 2) {
                System.out.print(i + "  ");
            }
        }
 /**
         * 输入一个四位数,要求计算出它的千位,百位,十位,个位,并且打印出来。
         */
        Scanner scanner = new Scanner(System.in);
        int num;
        do {
            System.out.print("请输入一个四位数:");
            num = scanner.nextInt();
        }while (num/1000 < 1);
        int rest = num;
        int gewei = rest % 10;
        rest /= 10;
        int shiwei = rest % 10;
        rest /= 10;
        int baiwei = rest % 10;
        rest /= 10;
        int qianwei = rest % 10;
        System.out.print("个位" + gewei + "  十位" + shiwei + "  百位" + baiwei + "  千位" + qianwei);
/**
         * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
         * 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
         */
        int f1=1,f2=1,f;
        int M=30;
        for(int i=3;i<M;i++) {
            f=f2;
            f2=f1+f2;
            f1=f;
            System.out.println(f2);
        }
 /**
         * 题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
         */
        Scanner input=new Scanner(System.in);
        int score=input.nextInt();
        char grade=score>=90?'A':score>=60?'B':'C'; // 用三目跟简单
        System.out.println(grade);
    /**
         * 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
         */
        public static void main(String[] args) {
            Scanner input =new Scanner(System.in);
            int a=input.nextInt();
            int b=input.nextInt();
            test06 test=new test06();
            int i = test.gongyinshu(a, b);
            System.out.println("最小公因数"+i);
            System.out.println("最大公倍数"+a*b/i);
        }
        public int gongyinshu(int a,int b) {
            if(a<b) {
                int t=b;
                b=a;
                a=t;
            }
            while(b!=0) {
                if(a==b)
                    return a;
                int x=b;
                b=a%b;
                a=x;
            }
            return a;
        }
 /**
         * 题目:求s=a+aa+aaa+aaaa+aa...a的值,
         * 其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
         * 几个数相加有键盘控制。
         */
        Scanner input=new Scanner(System.in);
        int a=input.nextInt();
        int n=input.nextInt();
        int sum=0,b=0;
        for(int i=0;i<n;i++) {
            b+=a;
            sum+=b;
            a=a*10;
        }
        System.out.println(sum);

 十大数据算法  https://www.cnblogs.com/ice-line/p/11753852.html

/**
         * 题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,
         * 其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",
         * 因为153=1的三次方+5的三次方+3的三次方。
         */
        int num = 999;
        for (int i = 100; i <= num; i++){
            int a = i / 100;
            int b = i / 10 % 10;
            int c = i % 10;
            if ((a * a * a + b * b * b + c * c * c) == i){
                System.out.println(i+"是水仙花数");
            }
        }
        /**
         * 题目:利用条件运算符的嵌套来完成此题:
         * 学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
         */
        Scanner input = new Scanner(System.in);

        int score = input.nextInt();

        char grade = score >= 90 ? 'A' : score >= 60 ? 'B' : 'C';

        System.out.println(grade);
public static void main(String[] args) {
        /**
         * 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
         * 在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,
         * 取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,
         * 返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
         */

        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();

        Test test=new Test(); // 调用gongyinshu方法
        int i = test.gongyinshu(m, n);

        System.out.println("最小公因数"+i);
        System.out.println("最大公倍数"+m * n / i);
    }

    public int gongyinshu(int a,int b) {
        if (a < b) {
            int t = b;
            b = a;
            a = t;
        }
        
        while (b != 0) {
            if (a == b) 
                return a;
            int x = b;
            b = a % b;
            a = x;
        }
        return a;
    }
 /**
         * 题目:输入一行字符,分别统计出其中字符、空格、数字的个数。
         */

        Scanner input = new Scanner(System.in);
        int abccount = 0, spacecount = 0, numcount = 0, othercount = 0;
        System.out.println("输入需要判断的数");
        String toString = input.nextLine();

        char [] ch = toString.toCharArray();
        for(int i = 0;i < ch.length; i++) {
            if(Character.isLetter(ch[i])) {
                abccount++;
            }else if(Character.isDigit(ch[i])) {
                numcount++;
            }else if(Character.isSpaceChar(ch[i])){
                spacecount++;
            }else {
                othercount++;
            }
        }
        System.out.println(abccount);
        System.out.println(spacecount);
        System.out.println(numcount);
        System.out.println(othercount);
 /**
         * 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
         * 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
         * 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
         */
        int num = 1;
        for (int i = 10 ; i > 1; i--){
            num = (num + 1) * 2;
        }
        System.out.println(num+"个桃子");
  /**
         * 题目:打印出图案(菱形)
         */
        int h = 9, w = 9;
        for (int i = 0; i < (h + 2) / 2; i++){
            for (int j = 0; j < (w / 2) - i; j++){
                System.out.print(" ");
            }
            for (int k = 1; k < (i + 1) * 2; k++){
                System.out.print("*");
            }
            System.out.println();
        }
        // 反着写
        for(int i = 1; i<= h / 2; i++) {
            for(int j = 1; j <= i; j++) {
                System.out.print(" ");
            }
            for(int k = 1; k <= w - 2 * i; k++) {
                System.out.print('*');
            }
            System.out.println();
        }
 /**
         * 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
         */
        long sum = 0, temp = 2;
        for (int i = 1; i <= 20; i++){
            sum += temp / i;
            temp += i;
        }
        System.out.println(sum);
 /**
         * 题目:求1+2!+3!+…+20!的和
         */

        int sum = 0, temp = 1;
        for (int i = 1; i <= 20; i++){
            temp = temp * i;
            sum += temp;
        }
        System.out.println(sum);
 public static void main(String[] args) {

        /**
         * 题目:利用递归方法求5!。
         */
        T_J1 t_j1 = new T_J1();
        System.out.println(t_j1.recursion(3));
    }

    public int recursion(int n){
        if (n != 1){
            return n * recursion(n -1);
        }
        return n;
    }
public static void main(String[] args) {

        /**
         * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
         * 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
         */
        int yue = 12;
        System.out.println(f(12));
    }

    public static int f(int n){
        if (n != 1 && n != 2){
            return f(n -1) + f(n - 2);
        }
        return 1;
    }
public static void main(String[] args) {
        /**
         * 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入n的值:");
        int n = scanner.nextInt();
        System.out.print( "分解质因数:" + n +"=");
        resole(n);
    }
    public static void resole(int n) {
        for (int i = 2; i <= n; i++) {
            if (n % i == 0) {
                System.out.print(i);
                if(n != i){
                    System.out.print("*");
                }
                resole(n / i);
            }
        }
        System.exit(0);
    }
public static void main(String[] args) {
        /**
         * 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入整数n");
        int n = scanner.nextInt();
        System.out.println("输入整数m");
        int m = scanner.nextInt();
        int a = 0, b = 0;
        a = commonDivisor(n, m);
        b = n * m / a;
        System.out.println("最大公约数:"+ a);
        System.out.println("最小公倍数:"+ b);
    }
    public static int commonDivisor(int x,int y){
        if(x<y){
            int 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;
    }
 /**
         * 题目:求s = a + aa + aaa + aaaa + aa...a的值,
         * 其中a是一个数字。例如2 + 22 + 222 + 2222 + 22222(此时共有5个数相加),
         * 几个数相加有键盘控制。
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入整数n");
        int n = scanner.nextInt();
        System.out.println("输入整数a");
        int a = scanner.nextInt();
        int s = 0, x = 0;
        for (int i = 1; i <= n; i++){
            x += a;
            s += x;
            a *= 10;
        }
        System.out.println(s);
/**
         * 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。
         * 例如6=1+2+3。编程找出1000以内的所有完数。
         */
        for (int i = 1; i < 1000; i++){
            int s = 0;
            for (int j = 1; j < i; j++){
                if (i % j == 0){
                    s += j;
                }
            }
            if (i == s){
                System.out.println("完数"+ i);
            }
        }
 /**
         * 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;
         * 再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
         */
        double s = 0, h = 100;
        for (int i = 1; i <= 10; i++){
            s += h / 2;
            h /= 2;
            s += h;
        }
        System.out.println("路程"+ s);
        System.out.println("第十次"+ h);
/**
         * 题目:输入某年某月某日,判断这一天是这一年的第几天?
         */
        System.out.println("请输入年,月,日:");
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        int month = in.nextInt();
        int day = in.nextInt();
        // 使用jdk提供的calendar方法
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day);
        int sum = cal.get(Calendar.DAY_OF_YEAR);
        System.out.println("这一天是这一年的第" + sum +"天");
 /**
         * 题目:一个5位数,判断它是不是回文数。即12321是回文数,
         * 个位与万位相同,十位与千位相同。
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入整数n");
        int n = scanner.nextInt();
        System.out.println("输入整数a");
        int m = scanner.nextInt();
        int i1 = String.valueOf(n).length();
        int i2 = String.valueOf(m).length();
        if (i1 != 5 && i2 != 5){
            System.exit(0);
        }
        for (int i = n; i <= m; i++){
            String str = String.valueOf(i);
            Integer a = Integer.valueOf(str.substring(str.length() / 2 + 1, str.length()));
            Integer b =  Integer.valueOf(new StringBuffer(str.substring(0, str.length() / 2)).reverse().toString());
            if (a == b){
                System.out.println(i +"是回文数");
            }
        }
/**
         *  题目:对10个数进行排序。
         */
        Scanner scanner = new Scanner(System.in);
        String[] objects = new String[10];
        for (int i = 0; i < objects.length; i++){
            objects[i] = scanner.next();
        }
        List<String> list = new ArrayList<>(objects.length);
        Collections.addAll(list, objects);
        list.sort(null);
        for (int j = 0; j < list.size(); j++){
            System.out.println(list.get(j));
        }
/**
         * 题目:打印出杨辉三角形(要求打印出10行如下图)
         * 1
         * 1   1
         * 1   2   1
         * 1   3   3   1
         * 1   4   6   4   1
         * 1   5   10   10   5   1
         */
        int n = 10;
        int[][] str = new int[n][n];
        for (int k = 0; k < n; k++){
            str[k][0] = 1;
            str[k][k] = 1;
        }
        for (int i = 2; i < n; i++){
            for (int j = 1; j <= i - 1; j++){
                str[i][j] = str[i - 1][j - 1] + str[i - 1][j];
            }
        }
        for (int i = 0; i < n; i++){
            for (int j = 0; j <= i; j++){
                System.out.print(str[i][j] +"\t");
            }
            System.out.println();
        }
public static void main(String[] args) {
        /**
         * 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到m报数),
         * 凡报到m的人退出圈子,问最后留下的是原来第几号的那位。
         */
        Scanner scanner = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int m = scanner.nextInt(), n = scanner.nextInt();
        for (int i = 1; i <= n;  i++){
            list.add(i);
        }
        fun(list, m);

    }
    public static void fun(List<Integer> list, int m){
        if (list.size() < m){
            for (int i = 0; i < list.size(); i++){
                System.out.println( "幸存者编号"+ list.get(i));
            }
            System.exit(0);
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        int k = 1;
        for (Integer item : list){
            if (k == 3){
                k = 1;
            }else {
                arrayList.add(item);
                k++;
            }
        }
        fun(arrayList, m);
    }
/**
         * 题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,
         * 当输入n为奇数时,调用函数1/1+1/3+...+1/n
         */
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = n % 2 == 0 ? 2 : 1;
        double sum = 1 / m;
        int temp = m;
        for (int i = 1; i < n; i++){
            sum += (double) 1 / (temp + 2);
            temp += 2;
        }
        System.out.println(sum);
 /**
         * 题目:某个公司采用公用电话传递数据,
         * 数据是四位的整数,在传递过程中是加密的,加密规则如下:
         * 每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,
         * 第二位和第三位交换。
         */
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        if (1000 > n && n > 9999){
            System.out.println("请重新收入");
        }else {
            // 作者选择了暴力破解,后续有时间在加
            int s = 0;
            s = n + 1000 * 5 + 100 * 5 + 10 * 5 + 5;
            s /= 10;
            String s1 = String.valueOf(s);
            char[] chars = s1.toCharArray();
            String[] strings = new String[chars.length];
            strings[1] = String.valueOf(chars[4]);
            strings[2] = String.valueOf(chars[3]);
            strings[3] = String.valueOf(chars[2]);
            strings[4] = String.valueOf(chars[1]);
            for (String l : strings){
                System.out.print(l);
            }
        }

 

推荐阅读