zoukankan      html  css  js  c++  java
  • 蓝桥 凑平方数

    /*
    
    凑平方数
    
    把0~9这10个数字,分成多个组,每个组恰好是一个平方数,这是能够办到的。
    比如:0, 36, 5948721
    
    再比如:
    1098524736
    1, 25, 6390784
    0, 4, 289, 15376
    等等...
    
    注意,0可以作为独立的数字,但不能作为多位数字的开始。
    分组时,必须用完所有的数字,不能重复,不能遗漏。
    
    如果不计较小组内数据的先后顺序,请问有多少种不同的分组方案?
    
    注意:需要提交的是一个整数,不要填写多余内容。
    
    
    
    
    
    */
    题目

    题解 :完全排列+DFS+Set(查重)

    import java.util.*;
    
    public class Main{
        static int[] num = {0,1,2,3,4,5,6,7,8,9};
        static long[] record = new long[15];
        static Set<String> set = new HashSet<String>();
        
        static boolean judge(long number) {
            if(Math.sqrt(number)==(long)Math.sqrt(number)) return true;
            else return false;
        }
        
        static String getString(int cnt) {
            long[] temp = new long[15];
            for(int i=0;i<cnt;i++)
                temp[i] = record[i];
            Arrays.sort(temp, 0,cnt);
            String string = "";
            for(int i=0;i<cnt;i++) {
                string += temp[i];
                string += ",";
            }
            return string;
        }
        
        static void dfs(int index,int cnt) {
            if(index==10) {
    //            if(!set.contains(getString(cnt))) 
    //                System.out.println(getString(cnt));    
                set.add(getString(cnt));
                return;
            }
            if(num[index]==0) {
                record[cnt] = 0;
                dfs(index+1, cnt+1);
                return;
            }
            long number = 0;
            for(int i=index;i<10;i++) {
                number = number*10+num[i];
                if(judge(number)) {
                    record[cnt] = number;
                    dfs(i+1, cnt+1);
                }
            }
        }
        
        static void permutation(int step) {
            if(step==10) {
                dfs(0,0);
                return;
            }
            for(int i=step;i<10;i++) {
                int temp = num[step]; num[step] = num[i]; num[i] = temp;
                permutation(step+1);
                temp = num[step]; num[step] = num[i]; num[i] = temp;
            }
        }
        
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            permutation(0);
            System.out.println(set.size());
        }
        
    }
    View Code
    import java.util.*;
    
    public class Main{
        static int[] num = {0,1,2,3,4,5,6,7,8,9};
        static Set<String> set = new HashSet<String>();
        static List<Long> list;
        
        static boolean judge(long number) {
            if(Math.sqrt(number)==(long)Math.sqrt(number)) return true;
            else return false;
        } 
        
        static String getString() {
            Set<Long> treeSet = new TreeSet<Long>();
            for(Long e:list)
                treeSet.add(e);
            String string = "";
            for(Long e:treeSet) {
                string += e;
                string += ",";
            }
            return string;
        } 
        static void dfs(int step) {
            if(step==10) {
    //            if(!set.contains(getString())) {
    //                for(int i=0;i<10;i++)
    //                    System.out.print(num[i]+" ");
    //                System.out.println();
    //                System.out.println(getString());
    //                System.out.println();
    //            
    //            }
                set.add(getString());
                return;
            }
            if(num[step]==0) {
                list.add((long) num[step]);
                dfs(step+1);
                list.remove(list.size()-1);
                return;
            }
            long number = 0; 
            for(int i=step;i<10;i++) {
                number = number*10 + num[i];
                if(judge(number)) {
                    list.add(number);
                    dfs(i+1);
                    list.remove(list.size()-1);
                }
            }
        }
        static void permutation(int step) {
            if(step==10) {
                list = new ArrayList<Long>();
                dfs(0);
                return;
            }
            for(int i=step;i<10;i++) {
                int temp = num[step]; num[step] = num[i]; num[i] = temp;
                permutation(step+1);
                temp = num[step]; num[step] = num[i]; num[i] = temp;
            }
        }
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            permutation(0);
            System.out.println(set.size());
        }
    }
    View Code
  • 相关阅读:
    核函数矩阵为什么要是positive semidefinite的
    Autoencoders
    Statistics与Machine Learning有什么区别
    [zz]Using SyncTeX with LaTeX
    Metric Learning的一个疑惑
    [zz]Latex中texworks编辑器正向反向跳转搜索
    [zz]Python3.x新特性
    LyX 2.0.5中文排版
    function
    多行文字结尾显示省略号
  • 原文地址:https://www.cnblogs.com/Lemon1234/p/10782782.html
Copyright © 2011-2022 走看看