zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯 算法提高 三进制数位和

    算法提高 三进制数位和
    时间限制:1.0s 内存限制:256.0MB
    提交此题
    问题描述
      给定L和R,你需要对于每一个6位三进制数(允许前导零),计算其每一个数位上的数字和,设其在十进制下为S。

    一个三进制数被判断为合法,当且仅当S为质数,或者S属于区间[L,R]。

    你的任务是给出合法三进制数的个数。
    输入格式
      一行两个非负整数L,R。
    输出格式
      一行一个非负整数表示答案。
    样例输入
    0 0
    样例输出
    330
    数据规模和约定
      保证0<=L<R<=12。
    提示
      判断x是否为质数核心代码:for (int i = 2; i * i <= x; ++i) if (x % i == 0) {/你猜?/}

    最简单的就是破解,也不是很大的数,下面还有一个别人写的方法的,个人感觉还可以

        import java.util.Scanner;  
    
        public class 三进制数位和 {  
            public static void main(String[] args) {  
                Scanner s = new Scanner(System.in);  
                int L = s.nextInt();  
                int R = s.nextInt();  
                int sum = 0, k = 0;  
                for (int a = 0; a < 3; a++) {  
                    for (int b = 0; b < 3; b++) {  
                        for (int c = 0; c < 3; c++) {  
                            for (int d = 0; d < 3; d++) {  
                                for (int e = 0; e < 3; e++) {  
                                    for (int f = 0; f < 3; f++) {  
                                        sum = a + b + c + d + e + f;//求和  
                                        if ((sum >= L && sum <= R) || (sum == 2)  
                                                || (sum == 3) || (sum == 5)  
                                                || (sum == 7) || (sum == 11))  
                                            k++;//计数  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
                System.out.println(k);  
            }  
        }  
    

    转自李晓斌96

    import java.util.Scanner;
    
    
    public class 三进制数位和 {
    	 static int time = 0;
    
    	    public static boolean panduanSan(int result) {//判断是否是三进制数
    	        int index = 0;
    	        while(result != 0) {
    	            int flag = result % 10;
    	            result /= 10;
    	            if(flag > 2) {
    	                return false;
    	            }
    	        }
    	        return true;
    	    }
    
    	    public static int QiuHe(int a) {//求每位数
    	        int sum = 0,i = 0;
    	        while(a != 0) {
    	            int flag = a % 10;
    	            a /= 10;
    	            sum += flag;
    	            i ++;
    	        }
    	        return sum;
    	    }
    
    	    public static boolean panduanSu(int a) {
    	        if(a == 1 || a == 0) {
    	            return false;
    	        }
    	        for(int i = 2; i <= a / 2;i++) {
    	            if(a % i == 0) {
    	                return false;
    	            }
    	        }
    	        return true;
    	    }
    
    	    public static void main(String[] args) {
    	        Scanner sc = new Scanner(System.in);
    	        String L = sc.next();
    	        String R = sc.next();
    	        String str = new String();
    	        for(int i = 0;i <= 222222;i++) {
    	            int sum = QiuHe(i);
    	            if(panduanSan(i) == true) {
    	                if(sum >= Integer.valueOf(L) && sum <= Integer.valueOf(R)) {//三进制
    	                    time ++;
    	                    continue;
    	                }
    	                if(panduanSu(sum) == true) {
    //	                  System.out.println(i);
    	                    time ++;
    	                }
    	            }
    
    	        }
    	        System.out.println(time);
    	    }
    
    }
    
    
  • 相关阅读:
    Python 集合
    Python sorted()
    CodeForces 508C Anya and Ghosts
    CodeForces 496B Secret Combination
    CodeForces 483B Friends and Presents
    CodeForces 490C Hacking Cypher
    CodeForces 483C Diverse Permutation
    CodeForces 478C Table Decorations
    CodeForces 454C Little Pony and Expected Maximum
    CodeForces 313C Ilya and Matrix
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078803.html
Copyright © 2011-2022 走看看