zoukankan      html  css  js  c++  java
  • 蓝桥杯-练习题(1017-1030)

    1017.猜牌

    一、题干

    Description
    魔术师利用一副牌中的13张黑桃,预先将它们排好后迭在一起,牌面朝下,并对观众说:“我不看牌,只数数就可以猜到每张牌是什么,我大声数数,你们听,不信?你们就看”。魔术师将最上面的那张牌数为1,把牌翻过来正好是黑桃A,将黑桃A放在桌子上;然后按顺序从上到下数手上的余牌,第二次数1、2,此时将第一章牌放在这迭牌的下面,将第二张牌翻过来正好是黑桃2,也将它放在桌子上;第三次数1、23,此时将前面两张依次放在这迭牌的下面,再翻开第三张牌正好是黑桃3。这样依次进行,将13张牌全部翻出来,准备无误。问魔术师手中的牌原始顺序是怎样安排的?
    Input
    无输入。
    Output
    输出一行数字,即魔术师手中牌的原始顺序。(样例输出只是格式提示)
    Sample Input
    无输入
    Sample Output
    2 3 4 5 6 7 8 9 10
    View Code

    二、代码

    public class T17 {
    
            //笔算,还快些
        public static void main(String[] args) {
            
            System.out.println("1 8 2 5 10 3 12 11 9 4 7 6 13");
    
        }
    
    }
    View Code

    1018.金蝉素数

    一、题干

    Description
    某古寺的一块石碑上依稀刻着一些神秘的自然数。这些数是由1,3,5,7,9这五个奇数排列组成的5位素数,且同时去掉它的最高位和最低位数字后剩下的3位数还是素数,同时去掉它的高二位与低二位数字后剩下的一位数还是素数,人们把这些神秘的素数称为金蝉素数,寓意金蝉脱壳之后仍为美丽的金蝉。编程找出石碑上的金蝉素数。
    Input
    无输入。
    Output
    每行输出一个金蝉素数。(样例输出只是格式提示)
    Sample Input
    无输入
    Sample Output
    123456
    789123
    View Code

    二、代码

    public class T18 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            
            //答案
    //        System.out.println(13597);
    //        System.out.println(53791);
    //        System.out.println(79531);
    //        System.out.println(91573);
    //        System.out.println(95713);
            
            int[] num=new int[]{1,3,5,7,9};
            for (int i = 0; i < num.length; i++) {
                for (int j = 1; j < num.length; j++) {
                    for (int j2 = 0;j2 < num.length; j2++) {
                        
                        if (j2!=i && j2!=j && i!=j && j!=4) {
                            int sum = num[j2]+num[j]*10+num[i]*100;
    
                            if (getSuNum(sum)) {
                                
                                for (int k = 0; k < num.length; k++) {
                                    for (int k2 = 0; k2 < num.length; k2++) {
                                        
                                        if ( k!=j2 && k!=j && k!=i && k2!=i && k2!=j && k2!=j2 && k2!=k) {
                                            
                                             int sum1 = sum*10+num[k2]+num[k]*10000;
                                             if (getSuNum(sum1)) {
                                                System.out.println(sum1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    
        private static boolean getSuNum(int sum) {
            
            boolean fa=true;
            
            for (int i = 2; i < sum/2+1; i++) {
                
                if (sum%i==0) {
                    fa=false;
                    break;
                }
                
            }
            
            return fa;
        }
    }
    View Code

    1019.完全数

    一、题干

    Description
    完全数,是一些特殊的自然数。它所有的真因子(即除了自身以外的约数),恰好等于它本身。例如,6就是完全数,6的约数有1、2、3、6,除去本身6外,1+2+3=6。编程找出10000以内的所有完全数。
    Input
    无输入。
    Output
    每行输出一个完全数。(样例输出只是格式提示)
    Sample Input
    无输入
    Sample Output
    6
    28
    View Code

    二、代码

    public class T19 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            
            for (int i = 1; i < 10000; i++) {
                
                int sum=1;
                for (int j = 2; j < i/2+1; j++) {
                    
                    if (i%j==0) {
                        sum+=j;    
                    }
                }
                if (sum==i) {
                    System.out.println(i);
                }
            }
    
        }
    
    }
    View Code

    1020.自守数

    一、题干

    Description
    如果某个数的平方的末尾几位数等于这个数本身,那么就称这个数为自守数。例如,5是自守数,5*5=25。编程找出10000以内的所有自守数。
    Input
    无输入
    Output
    每行输出一个自守数。(样例只是格式提示)
    Sample Input
    无输入
    Sample Output
    1
    5
    6
    View Code

    二、代码

    public class T20 {
    
        /**
         * 如果某个数的平方的末尾几位数等于这个数本身,那么就称这个数为自守数。
         * 例如,5是自守数,5*5=25。编程找出10000以内的所有自守数。
         */
        public static void main(String[] args) {
    
            for (int i = 1; i < 10000; i++) {
                int j = 10;
                for (; ;) {
                    if (i/j>0) {
                        j*=10;
                    }else{
                        break;
                    }
                }
                if ((i*i-i)%j==0) {
                    System.out.println(i);
                }
    
            }
    
        }
    
    }
    View Code

    1021.分数数列

    一、题干

    Description
    有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。 
    Input
    无输入
    Output
    输入该数列的前20项,并在下一行输出该数列的总和。(输出样例只是格式提示)
    Sample Input
    无输入
    Sample Output
    2.0/1.0 + 3.0/2.0 + 5.0/3.0 + 
    sum:12.123456
    View Code

    二、代码

    import java.math.BigDecimal;
    
    
    public class T21 {
    
        /**
         * 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。 
         */
        public static void main(String[] args) {
            
            float s=1f;
            float sum=0 ;
            for (int i = 2; i < 22; i++) {
                float chu=getSum(i)/s;
    
                sum=sum+chu;
                System.out.print(getSum(i)+"/"+s+" + ");
                s=getSum(i);
            }
            System.out.println();
            System.out.println("sum:"+sum);
        }
        public static float getSum(int a) {
            if (a==1) {
                return 1;
            }else if(a==2){
                return 2;
            }else {
                return getSum(a-1)+getSum(a-2);
            }
        }
    }
    View Code

    1022.拉丁方阵

    一、题干

    Description
    在N行N列的数阵中, 数K(〈=K〈=N)在每行和每列中出现且仅出现一次,这样的数阵叫N阶拉丁方阵。例如下图就是一个五阶拉丁方阵。
    编一程序,从键盘输入N值后,打印出其N阶拉丁方阵。
            1  2  3  4  5
            2  3  4  5  1
            3  4  5  1  2
            4  5  1  2  3
            5  1  2  3  4
    Input
    任输入一个(大于3,小于20)的介数
    Output
    打印出其N阶拉丁方阵
    Sample Input
    4
    Sample Output
     1  2  3  4 
     2  3  4  1 
     3  4  1  2 
     4  1  2  3
    View Code

    二、代码

    import java.util.Scanner;
    
    
    public class T22 {
    
        /**
         * 在N行N列的数阵中, 数K(1〈=K〈=N)在每行和每列中出现且仅出现一次,
         * 这样的数阵叫N阶拉丁方阵。例如下图就是一个五阶拉丁方阵。
         * 编一程序,从键盘输入N值后,打印出其N阶拉丁方阵。
         */
        public static void main(String[] args) {
            
            Scanner scanner=new Scanner(System.in);
            
            int a=scanner.nextInt();
    
            getHH(a);
            
        }
        public static void getHH(int a) {
            
            for (int i = 0; i < a; i++) {
                
                for (int j = 0; j < a; j++) {
                    if (i+j+1<=a) {
                        System.out.print(i+j+1+" ");
                    }else {
                        System.out.print((i+j+1)-a+" ");
                    }
                    
                    
                }
                System.out.println();
            }
        }
    }
    View Code

    1023.拾麦子

    一、题干

    Description
    你一定听说过这个故事。国王对发明国际象棋的大臣很佩服,
    问他要什么报酬,大臣说:请在第1个棋盘格放1粒麦子,
    在第2个棋盘格放2粒麦子,在第3个棋盘格放4粒麦子,
    在第4个棋盘格放8粒麦子,......后一格的数字是前一格的两倍,
    直到放完所有棋盘格(国际象棋共有64格)。
    国王以为他只是想要一袋麦子而已,哈哈大笑。
    当时的条件下无法准确计算,但估算结果令人吃惊:即使全世界都铺满麦子也不够用!
    请你借助计算机准确地计算,到底需要多少粒麦子。
    Input
    无输入
    Output
    输出到底需要多少粒麦子。(样例输出只是格式提示)
    Sample Input
    无输入
    Sample Output
    1234567889
    View Code

    二、代码

    import java.math.BigInteger;
    
    
    public class T23 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            BigInteger sum=new BigInteger("2");
            for (int i = 0; i < 64; i++) {
                sum=sum.add(BigInteger.valueOf((long)(Math.pow(2, i))));
            }
            System.out.println(sum);
        }
    
    }
    View Code

    1024.退圈报数

    一、题干

    Description
    有50个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,请将出圈的序号罗列显示出来。
    Input
    无输入
    Output
    将出圈的序号以逗号分隔开依次显示出来。(样例输出只是格式提示)
    Sample Input
    无输入
    Sample Output
    1,2,3,4,5,
    View Code

    二、代码

    public class T24 {
    
        /**
         * 有50个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),
         * 凡报到3的人退出圈子,请将出圈的序号罗列显示出来。
         */
        public static void main(String[] args) {
    
            int[] arr=new int[50];
            
            for (int i = 0; i < arr.length; i++) {
                 arr[i]=i+1;
            }
            
            int co=0;
            int le=50;
            
            while (le>2) {
                int conut=0;
                for (int i = 0; i < 50; i++) {
    
                    if (arr[i]!=-1) {
                        co++;
                    }
                    if (co==3) {
                        System.out.print(arr[i]+",");
                        arr[i]=-1;
                        conut++;
                        co=0;
                    }
                }
                le-=conut;
            }
        }
    }
    View Code

    1025.运动员分组

    一、题干

    Description
    有N个人参加100米短跑比赛。跑道为8条。程序的任务是按照尽量使每组的人数相差最少的原则分组。
     例如:
     N=8时,分成1组即可。
     N=9时,分成2组:一组5人,一组4人。
     N=25时,分4组:7、6、6、6。
    
    
     请编程计算分组数字。
     要求从标准输入获得一个正整数(1~100之间,不必考虑输入错误的情况),表示参赛的人数。
     程序输出每个组的人数。从大到小顺序输出,每个数字一行。
    
    Input
    一个正整数(1~100之间,不必考虑输入错误的情况),表示参赛的人数。
    Output
    程序输出每个组的人数。从大到小顺序输出,每个数字一行。
    Sample Input
    25
    Sample Output
    7
    6
    6
    6
    View Code

    二、代码

    import java.util.Scanner;
    
    
    public class T25 {
    
        /**
         * 有N个人参加100米短跑比赛。跑道为8条。程序的任务是按照尽量使每组的人数相差最少的原则分组。
         例如:
         N=8时,分成1组即可。
         N=9时,分成2组:一组5人,一组4人。
         N=25时,分4组:7、6、6、6。
        
        
         请编程计算分组数字。
         要求从标准输入获得一个正整数(1~100之间,不必考虑输入错误的情况),表示参赛的人数。
         程序输出每个组的人数。从大到小顺序输出,每个数字一行。
         */
        public static void main(String[] args) {
    
            Scanner scanner=new Scanner(System.in);
            
            int a=scanner.nextInt();
            
            int le=0;
            le=a%8==0?(a/8):(a/8+1);
            
            int ji=a/le;
            
            for (int i = 0; i < le; i++) {
                
                if (a==le*ji) {
                    System.out.println(ji);    
                }else {
                    System.out.println(ji+1);
                    a--;
                }
            }
        }
    }
    View Code

    1026.排列序数

    一、题干

    Description
      如果用a b c d这4个字母组成一个串,有4!=24种,如果把它们排个序,每个串都对应一个序号:
      abcd  0
      abdc  1
      acbd  2
      acdb  3
      adbc  4
      adcb  5
      bacd  6
      badc  7
      bcad  8
      bcda  9
      bdac  10
      bdca  11
      cabd  12
      cadb  13
      cbad  14
      cbda  15
      cdab  16
      cdba  17
      ...
    
        现在有不多于10个两两不同的小写字母(从a开始连续的字母),给出它们组成的串,你能求出该串在所有排列中的序号吗?
    
    Input
    一行,一个串。
    Output
    一行,一个整数,表示该串在其字母所有排列生成的串中的序号。注意:最小的序号是0。
    Sample Input
    cedab
    Sample Output
    70
    View Code

    二、代码

    import java.util.Arrays;  
    import java.util.Scanner;  
      
    public class T26 {  
        public static char [] array;  
        public static boolean flag [];  
        public static char[] all_P ;  
        public static int count = 0;  
        public static void main(String[] args) {  
    
            Scanner scan = new Scanner(System.in);  
            String string = scan.next();  
            array = string.toCharArray();
            
            Arrays.sort(array); 
            
            flag= new boolean[array.length];  
            all_P = new char[array.length];  
            dfs(0,string);  
        }  
        private static void dfs(int n,String string) {  
      
            if(n == array.length){  
                if(string.equals(String.valueOf(all_P))){  
                    System.out.println(count);  
                }  
     
                count++;  
                return;  
            }  
            for(int i = 0; i < array.length; i++){  
                if(!flag[i]){  
                    flag[i] = true;  
                    all_P[n] = array[i];  
                    dfs(n+1, string);  
                    flag[i] = false;  
                }  
            }  
        }  
      
    }  
    View Code

    1027高斯日记

    一、题干

    Description
    大数学家高斯有个好习惯:无论如何都要记日记。
    
        他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210
    
        后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?
    
        高斯出生于:1777年4月30日。
        
        在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。
    
        高斯获得博士学位的那天日记上标着:8113  
    
        请你算出高斯获得博士学位的年月日。
    
    Input
    一个正整数
    Output
    算出的日期,按照格式yyyy-mm-dd, 例如:1980-03-21
    Sample Input
    5343
    Sample Output
    1791-12-15
    View Code

    二、代码

    import java.util.Date;
    import java.util.Scanner;
    
    public class T27 {
    
        /**
         * 1027.高斯日记
         * 
         * 在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。
         * 
         * 高斯获得博士学位的那天日记上标着:8113 
         */
        public static void main(String[] args) {
    
            Scanner scanner=new Scanner(System.in);
            
            int day=scanner.nextInt();
    
            int Y=1777,M=4,D=30;
            
            int[] a={0,31,28,31,30,31,30,31,31,30,31,30,31};
            
            int y=0,m,d;
            
            if (day==1) {
                System.out.println("1777-4-30");
                return;
            }
            day--;
            if (day<=245) {
                y=1777;
                for ( m = 5; m <= 12; m++) {
                    if (day<a[m]) {
                        break;
                    }else {
                        day-=a[m];
                    }
                }
            }else{
        
                for (m=5;m<=12;m++) {
                    day-=a[m];
                }
                y=Y+1;
                
                while (day>365) {
                    if(getPy(y)){
                        day-=366;
                    }else {
                        day-=365;
                    }
                    y++;
                }
                if (getPy(y)) {
                    a[2]++;
                }
                for ( m = 1; m <= 12; m++) {
                    if (day<a[m]) {
                        break;
                    }else {
                        day-=a[m];
                    }
                }
            }
            String ma=m<10?"0"+m:m+"";
            String daya=day<10?"0"+day:day+"";
            System.out.println(y+"-"+m+"-"+day);
        }
        
        
        //判断平年还是闰年
        public static boolean getPy(int y) {
            if (y%400 == 0 || (y%4 == 0 && y%100!=0)) {
                return true;
            }else {
                return false;
            }
        }
    }
    View Code

    1028.生物芯片

    一、题干

    Description
    X博士正在研究一种生物芯片,其逻辑密集度、容量都远远高于普通的半导体芯片。
    
        博士在芯片中设计了 n 个微型光源,每个光源操作一次就会改变其状态,即:点亮转为关闭,或关闭转为点亮。
    
        这些光源的编号从 1 到 n,开始的时候所有光源都是关闭的。
    
        博士计划在芯片上执行如下动作:
    
        所有编号为2的倍数的光源操作一次,也就是把 2 4 6 8 ... 等序号光源打开
    
        所有编号为3的倍数的光源操作一次, 也就是对 3 6 9 ... 等序号光源操作,注意此时6号光源又关闭了。
    
        所有编号为4的倍数的光源操作一次。
    
        .....
    
        直到编号为 n 的倍数的光源操作一次。
    
    
        X博士想知道:经过这些操作后,某个区间中的哪些光源是点亮的。
    
    Input
    3个用空格分开的整数:N L R  (L<R<N<10^15)  N表示光源数,L表示区间的左边界,R表示区间的右边界。
    Output
    输出1个整数,表示经过所有操作后,[L,R] 区间中有多少个光源是点亮的。
    Sample Input
    10 3 6
    Sample Output
    3
    View Code

    二、代码

    import java.math.BigInteger;
    import java.util.Scanner;
    
    import javax.xml.namespace.QName;
    
    
    public class T28 {
    
        /**
         * 思路:完全平方数的因子个数为奇数。
    
        一个光源最后是打开的,当且仅当他被操作了奇数次,也即是说该数的因子数为奇数,
        即该数为完全平方数,但是这题是不算1的,故最终答案是,若该数不是完全平方数,则最后为打开状态。
         */
        public static void main(String[] args) {
            
            Scanner scanner=new Scanner(System.in);
            String inString=scanner.nextLine();
            
            String[] arrStrings=inString.split(" ");
            
            BigInteger n=BigInteger.valueOf(Integer.parseInt(arrStrings[0]));
            BigInteger l=BigInteger.valueOf(Integer.parseInt(arrStrings[1]));
            BigInteger r=BigInteger.valueOf(Integer.parseInt(arrStrings[2]));
            
            BigInteger q=BigInteger.valueOf(1);
            
            BigInteger sum=r.subtract(l);
            
            for (BigInteger i = l; i.multiply(i).compareTo(r)==1; i.add(q)) {
    
                if (i.multiply(i).compareTo(l)==-1&&i.multiply(i).compareTo(r)==1) {
                    sum.subtract(q);
                }
                
            }
            System.out.println(sum);
    
        }
    
    }
    View Code

    1029.打印十字图

    一、题干

    略..
    View Code

    二、代码

    import java.util.Scanner;  
    public class T29 {  
        public static void main(String[] args) {  
            Scanner input=new Scanner(System.in);  
            int n=input.nextInt();  
            int low,row,center;  
            low=row=4*(n-1)+9;  
            int [][]array=new int[low][row];  
            for(int i=0;i<low;i++)  
                for(int j=0;j<row;j++){  
                    array[i][j]='.';  
                }  
            center=row/2;  
            for(int i=center-2;i<=center+2;i++){  
                array[center][i]='$';  
                array[i][center]='$';  
            }  
            for(int m=0;m<n;m++){  
            for(int i=center-2-2*m;i<(center+3+2*m);i++){  
                array[center-(4+m*2)][i]='$';  
                array[center+(4+m*2)][i]='$';  
                array[i][center-(4+m*2)]='$';  
                array[i][center+(4+m*2)]='$';  
            }  
            }  
            for(int m=0;m<n;m++){  
            for(int i=center-3-2*m;i<=center+3+2*m;i++){  
                array[center-(2+2*m)][i]='$';  
                array[center+(2+2*m)][i]='$';  
                array[i][center-(2+2*m)]='$';  
                array[i][center+(2+2*m)]='$';  
            }  
              
            array[center-(2+2*m)][center-(1+2*m)]='.';  
            array[center-(2+2*m)][center+(1+2*m)]='.';  
            array[center-(1+2*m)][center-(2+2*m)]='.';  
            array[center-(1+2*m)][center+(2+2*m)]='.';  
            array[center+(1+2*m)][center-(2+2*m)]='.';  
            array[center+(1+2*m)][center+(2+2*m)]='.';  
            array[center+(2+2*m)][center-(1+2*m)]='.';  
            array[center+(2+2*m)][center+(1+2*m)]='.';  
            }  
              
            for(int i=0;i<low;i++){  
                for(int j=0;j<row;j++){  
            System.out.print((char)array[i][j]);  
                }  
             System.out.println();  
            }  
        }  
      
    }  
    View Code
  • 相关阅读:
    2016年第七届蓝桥杯C/C++ A组国赛 —— 第一题:随意组合
    寻找段落
    攻击火星
    图论入门
    实数加法
    求 10000 以内 n 的阶乘
    大整数因子
    计算2的N次方
    大整数减法
    大整数加法
  • 原文地址:https://www.cnblogs.com/gx-143/p/6048338.html
Copyright © 2011-2022 走看看