zoukankan      html  css  js  c++  java
  • 254.Factor Combinations

         /*
          * 254.Factor Combinations  
          * 2016-3-13 by Mingyang
          * Numbers can be regarded as product of its factors. For example,
          * 8 = 2 x 2 x 2; 8= 2 x 4.
          * Write a function that takes an integer n and return all possible combinations of its factors
          * 1.长度标准:无(固定)
          * 2.可选的范围:最开始是从2到n-1的所有整数,每一次递进以后。就是从上次取得那个数起到n-1的所有整数。
          * 3.往前走一步:temp加入这个数,num乘以这个数--网上的版是这个数除以i。少了一个参数,一样的原理
          * 4.后退一步:temp remove
          * 5.特别的case:到了长度检查。
          */
         public static List<List<Integer>> getFactors(int n) {
           List<List<Integer>> res=new ArrayList<List<Integer>>();
           List<Integer> temp=new ArrayList<Integer>();
           dfs1(res,temp,1,n,2);
           return res;
         }
    public static void dfs1(List<List<Integer>> res, List<Integer>temp, int num,int n,int start){ if(num==n){ res.add(new ArrayList<Integer>(temp)); return; } if(num>n) return; for(int i=start;i<n;i++){ if(n%(num*i)==0){//这里很巧妙地判断整除性,这样可以省去很多不必要的数
    //num=num*i;原来这么分开写,但是这么分开写后面就要重新赋初值,把num改回去。所以直接在dfs1的参数里面乘 temp.add(i); dfs1(res,temp,num*i,n,i); temp.remove(temp.size()-1); //num=num/i; } } }
  • 相关阅读:
    H3C交换机流量镜像
    脚本引发的思考
    【PAT Advanced Level】1015. Reversible Primes (20)
    JSTL自定义函数完成ACL即时认证
    [翻译Joel On Software]选择一门语言/Choosing a language
    MFC-CWinApp
    poj2462
    HDU 3472 混合图欧拉回路 + 网络流
    Vim命令合集
    Windows下SQLMAP的安装图解
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5603012.html
Copyright © 2011-2022 走看看