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; } } }
  • 相关阅读:
    3-为什么很多 对 1e9+7(100000007)取模
    6-关于#include<bits/stdc++.h>
    7-n!的位数(斯特灵公式)
    5-math中函数汇总
    6-找数的倍数
    6-Collision-hdu5114(小球碰撞)
    5-青蛙的约会(ex_gcd)
    4-圆数Round Numbers(数位dp)
    4-memset函数总结
    一种Furture模式处理请求中循环独立的任务的方法
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5603012.html
Copyright © 2011-2022 走看看