蓝桥杯习题(基础练习)

闰年判断

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0)){
            System.out.println("yes");
        }else{
            System.out.println("no");
        }
    }
}

01字串

public class Main {
    public static void main(String[] args) {
        for(int i = 0; i <= 1; i++){
            for(int j = 0; j <= 1; j++){
                for(int z = 0; z <= 1; z++){
                    for(int m = 0; m <= 1; m++){
                        for(int r = 0; r <= 1; r++){
                            System.out.printf("%s%s%s%s%s\n",i,j,z,m,r);
                        }
                    }
                }
            }
        }
    }
}

字母图形

import java.util.Scanner;

import static java.lang.Math.abs;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int n2 = sc.nextInt();
        String[] arr = {"A","B","C","D","E","F","G",
                "H","I","J","K","L","M","N","O","P",
                "Q","R","S","T","U","V","W","X","Y","Z"
        };
        for(int i = 0; i < n1; i++){
            for(int j = 0; j < n2; j++){
                int k = abs(i-j);
                System.out.print(arr[k]);
            }
            System.out.println();
        }
    }
}

数列特征

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        }
        Arrays.sort(arr);
        System.out.println(arr[n-1]);
        System.out.println(arr[0]);
        int sum = 0;
        for (int value : arr) {
            sum += value;
        }
        System.out.println(sum);
    }
}

查找整数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        }
        int s = sc.nextInt();
        System.out.println(find(arr, n, s));
    }

    public static int find(int[] arr, int n, int s){
        int index = -1;
        for(int i = 0; i < n; i++){
            if(arr[i] == s){
                index = i + 1;
                break;
            }
        }
        return index;
    }
}

杨辉三角形

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] arr = new int[n][n];

        //存储数值
        for(int i = 0; i < n; i++){
            for(int j = 0; j <= i; j++){
                //第一列和最后一列为0
                if(j == 0 || j == i){
                    arr[i][j] = 1;
                }else{
                    arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
                }
            }
        }

        //打印
        for(int i = 0; i < n; i++){
            for(int j = 0; j <= i; j++){
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

特殊的数字

public class Main{
    public static void main(String[] args) {
        for(int i = 1; i <= 9; i++){
            //有0!
            for(int j = 0; j <= 9; j++){
                for(int z = 0; z <= 9; z++){
                    int index = i * 100 + j * 10 + z;
                    if(index == i * i * i + j * j * j + z * z *z){
                        System.out.println(index);
                    }
                }
            }
        }
    }
}

回文数

public class Main {
    public static void main(String[] args) {
        //除以10可以提取前面,余10可以提取后面
        for(int i = 1000; i <= 9999; i++){
            if((i / 1000 == i % 10) && (i / 100 % 10 == i % 100 / 10)){
                System.out.println(i);
            }
        }
    }
}

特殊回文数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //除以10可以提取前面,余10可以提取后面
        //求一个三位数的各个位数           :   n / 100 % 10        n / 10 % 10         n / 1 % 10
        for(int i = 10000; i <= 999999; i++){
            if(i < 100000){
                if((i / 10000 % 10 == i  % 10) && (i / 1000 % 10 == i / 10 % 10)){
                    if((i / 10000 % 10+ i / 1000 % 10 + i / 100 % 10 + i / 10 % 10 + i  % 10) == n){
                        System.out.println(i);
                    }
                }
            }
            if(i >= 100000){
                if((i / 100000 % 10 == i % 10) && (i / 10000 % 10 == i / 10 % 10) && (i / 1000 % 10 == i / 100 % 10)){
                    if((i / 100000 % 10 + i / 10000 % 10 + i / 1000 % 10 + i /100 % 10 + i / 10 % 10 + i % 10) == n){
                        System.out.println(i);
                    }
                }
            }
        }
    }
}

数列排列

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        }
        Arrays.sort(arr);
        for(int a : arr){
            System.out.print(a + " ");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/yfyyy/p/12002206.html