一些练习

1.编写一个算法来判断一个数是不是“快乐数”
定义:对于一个正整数,每一次将该数替换为每个位置上的数字的平方和,然后重复这个过程直到这个数变为1.若可以变为1,则为快乐数
方法一;找规律:3 :9 81 65 61 37 58 89 145 42 20 4 16 37 58 89 145 42 …
21: 5 25 29 85 89 145 42 20 4 16 37 58…
发现若不是快乐数则会一直循环,且循环里包含4 16 37…等数字
因此可以由此判断。代码如下:

//快乐数
public class Test1 {
    //判断各个位上的平方和
    public static int function(int num) {
        int ret = 0;
        while (num != 0) {
            ret = ret + (num % 10) * (num % 10);
            num = num / 10;  //123/10=12

        }
        return ret;
    }

    public static boolean isHappy(int n) {
        int ret = 0;
        if (n <= 0) {
            return false;
        }
        while (n != 1) {
            n = function(n);
            if (n == 4) {
                return false;
            }

        }
        return true;
    }


    public static void main(String[] args) {
        System.out.println(isHappy(5));
        System.out.println(isHappy(7));
    }
}

方法二:不用涉及到找规律
引入集合

import java.util.HashSet;
import java.util.Set;

public class Test2 {
    public static int getNext(int num) {
        int result = 0;
        result = result + (num % 10) * (num % 10);
        num = num / 10;  //123/10=12
        if (num == 0) {
            return result;
        } else {
            return result + getNext(num);
        }
    }
    public static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n != 1) {
            int flag = getNext(n);
            if (set.contains(flag)) {
                return false;
            } else {
                set.add(flag);
                n = flag;
            }
        }
        return true;

    }



    public static void main(String[] args) {
        Test2 test=new Test2();
        System.out.println(test.isHappy(5));
    }
}

2.给一个英文字符串,写一段代码找出字符串中首先出现三次的英文字母。输入描述:输入一个字符串,包括字母,数字。输出描述:输出首先出现三次的那个英文字符。

import java.util.Scanner;

public class Test3 {
    public static char function(String str){
        int[] hash=new int[256];
        char[] chars=str.toCharArray();
        for(int i=0;i<chars.length;i++){
            char ch=chars[i];
            if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){
                hash[ch]++;   //次数
                if(hash[ch]==3){
                    return ch;
                }
            }
        }
        return '0';
    }

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("please input the string: ");
        String str=scan.nextLine();
        System.out.println(function(str));
    }
}

3.约瑟夫环(有10个人围成一圈,顺序排号。从第一个人开始报数,凡报到3的人退出圈子,问最后留下来的是原来的几号)
方法一:
从一号开始报数,定义一个数组,当这个人数大于1的时候,开始报数,定义一个count,每当count到3 的时候,总数减1,下一个人又从一号开始报数,循环前一个步骤。最后只剩下一个的时候返回。

public class Test4 {
    public static int function(int n){
        int[] array=new int[n];
        for(int i=0;i<array.length;i++){
            array[i]=1;
        }
        int count=0;

        int number=n;
        while(number>1) {
            for (int i = 0; i < n; i++) {
                if (array[i] == 1) {
                    count++;       //次数
                    if (count == 3) {
                        array[i] = 0;
                        number--;
                        count = 0;
                    }
                }
            }
        }
            for(int i=0;i<n;i++){
                if(array[i]==1){
                    return i+1;
                }
            }

        return -1;
    }

    public static void main(String[] args) {
        System.out.println(function(10));
    }
}



方法二:采用ArrayList,将报到3的从列表中删除。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void yuesefu(int totalNum,int countNum){
 //初始化人数
        List<Integer>start=new ArrayList<Integer>();
        for(int i=1;i<=totalNum;i++){
            start.add(i);   //将a添加到list中
        }
        //从第K个开始计数
    int k=0;
    while(start.size()>0){
        k+=countNum;
        //第m人的索引位置
        k=k%(start.size())-1;
        //判断是否到队尾
        if(k<0) {
            System.out.print(start.get(start.size() - 1));
            start.remove(start.size() - 1);
            k = 0;

            System.out.println();
        }
        else{
            System.out.print(start.get(k));
            start.remove(k);
        }
        }

    }

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入总人数:");
        int totalNum=scan.nextInt();
        System.out.println("请输入报数的大小:");
        int cycleNum=scan.nextInt();
        yuesefu(totalNum,cycleNum);

}
}

猜你喜欢

转载自blog.csdn.net/weixin_42373873/article/details/89397287