蓝桥杯练习01

  • 母牛的故事

    import java.util.Scanner;

     

    public class Main {

        

        public static void main(String[] args) {

            Scanner in=new Scanner(System.in);

            int n=in.nextInt();

            while (n>0&&n<55) {

                System.out.println(f(n));

                n=in.nextInt();

            }

        }

        private static int f(int n) {

            if (n==0) {

                return 0;

            }

            if (n==1) {

                return 1;

            }

            if (n==2) {

                return 2;

            }

            if (n==3) {

                return 3;

            }

            return f(n-1)+f(n-3);

        }

     

    }

  • Hello,World!

    import java.util.Scanner;

     

    public class Main {

        

        public static void main(String[] args) {

            Scanner in=new Scanner(System.in);

            int n;

            while (in.hasNext()) {

                n=in.nextInt();

                System.out.print((char)n);    

            }    

        }

    }

  • 用筛法求之N内的素数

    import java.util.Scanner;

     

    public class Main {

        

        public static void main(String[] args) {

            Scanner in=new Scanner(System.in);

            int n=in.nextInt();

            boolean[] notPrime=new boolean[n];

            for (int i = 2; i < n; i++) {

                if (!notPrime[i]) {

                    System.out.println(i);

                    for (int j = 2; j*i < n; j++) {

                        notPrime[i*j]=true;

                    }

                }

            }

        }

    }

  • 字符逆序

    import java.util.ArrayList;

    import java.util.Collections;

    import java.util.List;

    import java.util.Scanner;

     

    public class Main {

        

        public static void main(String[] args) {

            Scanner in=new Scanner(System.in);

            String string=in.nextLine();

            List<Character> list=new ArrayList<>();

            for (int i = 0; i < string.length(); i++) {

                list.add(string.charAt(i));

            }

            Collections.reverse(list);

            for (Character c : list) {

                char ch=c;

                System.out.print(ch);

            }

        }

    }

  • 字符串的输入输出处理

    import java.util.Scanner;

    public class Main {

        public static void main(String[] args) {

            Scanner in=new Scanner(System.in);

            int n=in.nextInt();

            String s1=in.nextLine();

            while (n>0) {

                s1=in.nextLine();

                System.out.println(s1);

                System.out.println();

                n--;

            }

            String s2;

            while (in.hasNext()) {

                s2=in.next();

                System.out.println(s2);

                System.out.println();

            }

        }

    }

  • The 3n+1 Problem    

    import java.util.Scanner;

    public class Main {

        public static void main(String[] args) {

            Scanner in=new Scanner(System.in);

            while (in.hasNext()) {

                int n=in.nextInt();

                int m=in.nextInt();

                System.out.print(n+" "+m+" ");

                int max=0;

                for (int j = Math.min(n, m); j <= Math.max(n, m); j++) {

                    int i=1;

                    int k=j;

                    while (k!=1&&(k>0&&k<1000000)) {

                        if (k%2==0) {

                            k/=2;

                        }else {

                            k=k*3+1;

                        }

                        i++;

                    }

                    max=Math.max(i, max);

                }

                System.out.println(max);

            }

        }

    }

  • Minesweeper扫雷

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner in=new Scanner(System.in);

        int n=in.nextInt();

        int m=in.nextInt();

        String s=in.nextLine();//读取回车符,下同

        int c=1;//判断雷的数量

        while (in.hasNext()&&(n!=0||m!=0)) {    

            char[][] a=new char[n+2][m+2];//增加两行两列,便于判断雷的数量,防止数组越界

            for (int i = 0; i < n+2; i++) {

                if (i!=0&&i!=n+1) {

                    s=in.nextLine();

                }

                for (int j = 0; j < m+2; j++) {

                    if (i==0||i==n+1||j==0||j==m+1) {

                        a[i][j]='.';

                    }else {    

                        a[i][j]=s.charAt(j-1);

                    }

                }

            }

            for (int i = 1; i < n+1; i++) {

                for (int j = 1; j < m+1; j++) {

                    if (a[i][j]=='*') {

                        continue;

                    }else {

                        int num=0;

                        for (int k = i-1; k <= i+1; k++) {

                            for (int l = j-1; l <= j+1; l++) {

                                if (k==i&&l==j) {

                                    continue;

                                }else if(a[k][l]=='*'){

                                    num++;

                                }

                            }

                        }

                        a[i][j]=(char)(num+48);

                    }

                }

            }

            System.out.println("Field #"+c+":");

            for (int i = 1; i < n+1; i++) {

                for (int j = 1; j < m+1; j++) {

                    System.out.print(a[i][j]);

                }

                System.out.println();

            }

            System.out.println();

            n=in.nextInt();

            m=in.nextInt();

            s=in.nextLine();

            c++;

        }

        

    }

}

posted @ 2019-02-26 15:55  GavinYGM  阅读(117)  评论(0编辑  收藏  举报