判断素数.水仙花数(hash实现).分解因数.猴子吃桃

1、判断素数

/**
 * Created by "Seaside" on 2016/12/15.
 */
public class isPrime {
    /*
    * 判断素数
    * */
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            int num2 = isPrime(i);
            if (num2 != 0) {
                System.out.println(num2);
            }
        }

    }

    static int isPrime(int num) {
        boolean flag = false;
        for (int j = 2; j <= Math.sqrt(num); j++) {
            if (num % j == 0) {
                return 0;
            }
        }
        return num;
    }
}

2、因式分解

/**
 * Created by "Seaside" on 2016/12/15.
 */
public class isZhiYinShuo {
    /*
    * 分解质因数:找到和是最小的那一组
    * 如:12+2*6
    *     12=3*4
    *     12=2*2*3
    *     这组和是最小的
    * */
    public static void main(String[] args) {
        ZhiYinShuoFenJie(12);
    }

    static void ZhiYinShuoFenJie(int num) {
        if (isPrime(num)) {
            System.out.println(num);
        } else {
            for (int i = 2; i < Math.sqrt(num); i++) {
                if (num % i == 0) {
                    ZhiYinShuoFenJie(i);
                    ZhiYinShuoFenJie(num / i);
                    break;
                }
            }
        }
    }

    static boolean isPrime(int num) {
        boolean flag = true;
        for (int j = 2; j <= Math.sqrt(num); j++) {
            if (num % j == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}

3.水仙花数

import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * Created by "Seaside" on 2016/12/15.
 */
public class ArmstrongNumber {
    /*
    * 水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)   *
    * */
    static int one = 1;
    static int two = 2 * 2 * 2;
    static int three = 3 * 3 * 3;
    static int four = 4 * 4 * 4;
    static int five = 5 * 5 * 5;
    static int six = 6 * 6 * 6;
    static int seven = 7 * 7 * 7;
    static int eight = 8 * 8 * 8;
    static int nine = 9 * 9 * 9;
    static int zero = 0;

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int number = scan.nextInt();
        System.out.println(isArmstrongNumber(number));
    }

    static boolean isArmstrongNumber(int num) {
        int temp=num;
        boolean flag = false;
        HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
        hm.put(0, zero);
        hm.put(1, one);
        hm.put(2, two);
        hm.put(3, three);
        hm.put(4, four);
        hm.put(5, five);
        hm.put(6, six);
        hm.put(7, seven);
        hm.put(8, eight);
        hm.put(9, nine);
        int sum = 0;
        do {
            if (num < 10) {
                sum += hm.get(num);
                break;
            }
            int k = num % 10;
            num /= 10;
            sum += hm.get(k);
        } while (num >= 0);
        if (sum == temp) {
            flag = true;
        }
        return flag;
    }
}

4、成绩判断+猴子吃桃问题

/**
 * Created by "Seaside" on 2016/12/15.
 */
public class ScoreMonkey {
    /*
    * 学生成绩+猴子吃桃子的问题
    * */
    public static void main(String[] args) {
//        Score(100);
        Monkey();
    }

    static void Score(int num) {
        /*
        * 成绩是0-100
        * */
        String result = "";
        switch (num / 10) {
            case 10:
                result = "非常棒!";
                break;
            case 9:
                result = "优秀!";
                break;
            case 8:
                result = "良好!";
                break;
            case 7:
                result = "良";
                break;
            case 6:
                result = "及格";
                break;
            default:
                result = "不及格,有待努力!";
        }
        System.out.println(result);
    }

    public static void Monkey() {
        /*
        * 猴子吃枣问题。猴子摘了一堆枣。第一天吃了一半,还嫌不过瘾,又吃一个;第二天又吃了剩下的一半零一个;
          以后每天如此。到第十天,猴子一看只剩下一个了。问第一天有多少个枣子?
        * */
        int[] touzi = new int[10];   //声明一个整型数组,用来存储每天剩余的桃子数量
        touzi[9] = 1;  //第十天剩下一个桃子
        for (int i = 8; i >= 0; i--) {
            touzi[i] = (touzi[i + 1] + 1) * 2;  //每前一天的桃子数都等一现在剩余的桃子数加一在乘以二
        }
        System.out.println("第一天摘的桃子数为:" + touzi[0]); //输出第一天共摘的桃子数

    }}
    //递归实现猴子吃桃问题
    static int sum = 0;

    static int Monkey_DiGui(int day) {
        if (day == 10) {
            return 1;
        }

        sum += 2*Monkey_DiGui(day + 1)+2;
        return sum;
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漁陽

彼此共勉,砥砺前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值