栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

10.20Java算法集

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

10.20Java算法集

public class Test01 {
    public static void main(String[] args) {
      1.  //题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
        // 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 
        //(1).算法分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
        //(2). 算法书写.
        int last = 1;//前一个月的兔子对数(第2个月为1)
        int last1 = 1;//前前一个月的兔子对数(第1个月为1)
        int temp;//当前兔子总对数
        for (int i = 1; i <= 12; i++) {
            if (i > 2) {
                temp = last + last1;
                last1 = last;
                last = temp;
                System.out.println("第" + i + "个月的兔子总数为" + 2 * temp + "个");
            } else {
                System.out.println("第" + i + "个月的兔子总数为2个");
            }
        }

    }

}

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

public class demo02 {
    public static void main(String[] args) {
        int count = 0;//素数的总数
        for (int i = 101; i < 201; i++) {
            for (int j = 2; j <= i; j++) {
                if (i % j == 0) {
                    if (i == j) {
                        count++;
                        System.out.println(i);
                    } else {
                        break;
                    }
                }

            }
        }
        System.out.println("一共有" + count + "个素数");
    }
}

题目:打印出所有的"水仙花数(narcissus number)",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。 

public class Test03 {
    public static void main(String[] args) {
        int ge, shi, bai, he;
        for (int i = 100; i < 1000; i++) {
            ge = i % 10;
            shi = i / 10 % 10;
            bai = i / 100 % 10;
            he = (int) Math.pow(ge, 3) + (int) Math.pow(shi, 3) + (int) Math.pow(bai, 3);
            if (he == i) {
                System.out.println(i);
            }

        }
    }
}

​​​​​​​题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 

import java.util.Scanner;

public class Test04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个大于2的正整数:");
        int input = sc.nextInt();
        int a = 2;
        System.out.print(input + "=");
        while (true) {
            if (a == input) {
                System.out.print(a);
                break;
            }
            if (input % a == 0) {
                input = input / a;//商作为新的input
                System.out.print(a + "*");
            } else {
                a++;

            }
        }
    }
}

题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C

import java.util.Scanner;

public class Test05 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学习成绩:");
        double score = sc.nextDouble();
        System.out.println((score >= 90) ? "A" : (score < 90 && score >= 60 ? "B" : "C")
        );
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337653.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号