算法学习day1

    public static void main(String[] args) {
        // changeToByte(-1);
        // jiechengqiuhe(10);
        //int sum=totalFileCount("E:\\test\\jar\\account");
        int sum=totalFileCountUseStack("E:\\test\\jar\\account");
        System.out.print("总文件个数=" + sum);
    }

    /**
     * 1.打印一个整数的32位二进制 
     * 思路:原数据与32位二位进制相与,0与1相与&为0,1与1相与&为1
     */
    public static void changeToByte(int num) {
        StringBuffer bufferResult = new StringBuffer();
        for (int a = 31; a >= 0; a--) {
            if ((num & (1 << a)) == 0) {
                bufferResult.append("0");
            } else {
                bufferResult.append("1");
            }
        }
        System.out.println(bufferResult.toString());
    }

    /**
     * 2.求:1!+2!+3!+...n!之和 
     * 思路 n的阶乘是n-1的阶层之和*n,
     */
    public static void jiechengqiuhe(int n) {
        int j = 1;
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            j = j * i;
            sum = sum + j;
        }
        System.out.println(sum);
    }

    /**
     * 3.统计一个文件夹里面文件的个数 
     * 思路1.递归查询
     */
    static int sum = 0;

    public static int totalFileCount(String filePath) {

        try {
            File file = new File(filePath);
            if (!file.exists()) {
                sum = 0;
            } else {

                for (File fileChild : file.listFiles()) {
                    if (fileChild.isFile()) {
                        sum++;
                    } else {
                        totalFileCount(fileChild.getAbsolutePath());
                    }
                }

            }
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
        return sum;
    }
    /**
     * 3.统计一个文件夹里面文件的个数 
     * 思路2.使用堆栈来循环查询
     */
    public static int totalFileCountUseStack(String filePath) {
        int sumNew = 0;
        
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                sumNew = 0;
            } else {
                if(file.isFile())
                {
                    return 1;
                }
                Stack<File> stack=new Stack<>();
//                stack.add(file);
                stack.push(file);//add 和push效果一样
                while(!stack.isEmpty())//判断目录文件夹是否已经循环完成
                {
                   File fileChild=stack.pop();
                   for(File fileGrandson :fileChild.listFiles() )
                   {
                        if (fileGrandson.isFile()) {
                            sumNew++;
                        } 
                        if(fileGrandson.isDirectory())
                        {
                            stack.push(fileGrandson);
                        }
                   }
                
                }

            }
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
        return sumNew;
    }
    /**
     * 4.冒泡排序 .
     * 思路:将最大值的往上冒泡
     */
    public void bubbleSort() {
        int length = arr.length;
        for (int a = length - 1; a >= 0; a--) {
            for (int b = 0; b < a; b++) {
                if (arr[b] > arr[b + 1]) {
                    int temp = arr[b];
                    arr[b] = arr[b + 1];
                    arr[b + 1] = temp;
                }
            }
        }

        for (int m = 0; m < length; m++) {
            System.out.println(arr[m]);
        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值