0
点赞
收藏
分享

微信扫一扫

【蓝桥杯】基础练习(Java)

yundejia 2022-01-27 阅读 61

蓝桥杯 基础练习

题源 👉 蓝桥杯试题集

BASIC-01 A+B问题

问题描述

数据规模与约定

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		System.out.println(a+b);
	}
}

 

BASIC-13 数列排序

问题描述

输入格式

输出格式

样例输入

样例输出

// 排序算法:冒泡、快排...等等
// 偷懒的话可以直接用Arrays.sort(arr)
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 i = 0; i < n; i++) {
 			System.out.print(arr[i] + " ");
		}
	}
}

 

BASIC-12 十六进制转八进制

问题描述

输入格式

输出格式

  • 【注意

    输入的十六进制数不会有前导0,比如012A。
    输出的八进制数也不能有前导0。

样例输入

样例输出

【提示

// 实际题目应该是要先将16进制转为2进制,再转为8进制。
// 但是又偷懒,取了巧直接用BigInteger转化为8进制
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 输入第一行
		int n = sc.nextInt();
		String arr[] = new String[n];
		// 输入十六进制数
		for(int i = 0; i < n; i++) {
			String temp16 = sc.next();
			// 利用BigInteger转化为8进制
			String temp8 = new BigInteger(temp16,16).toString(8);
			arr[i] = temp8;
		}
		// 输出
		for(int i = 0; i < n; i++) {
			System.out.println(arr[i]);
		}
	}
}

 

BASIC-11 十六进制转十进制

问题描述

样例输入

样例输出

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String temp16 = sc.next();
		String temp = new BigInteger(temp16,16).toString(10);
		System.out.println(temp);
	}
}

 

BASIC-10 十进制转十六进制

问题描述

输入格式

输出格式

样例输入

样例输出

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String temp10 = sc.next();
		String temp16 = new BigInteger(temp10).toString(16).toUpperCase();
		System.out.println(temp16);
	}
}

有点坑的是,这题的十六进制要用 .toUpperCase()转为大写的,不然算作不对…

BASIC-12、11、10三种是同类型,都用了Java的BigInteger简单粗暴地完成转换了,先这么着吧。

 

BASIC-9 特殊回文数

问题描述

输入格式

输出格式

样例输入

样例输出

数据规模和约定

由于要求的是五位和六位十进制数,可以暴力枚举找出 10000~999999 内所有符合要求的情况

  • 用一个循环遍历范围内的数,通过计算个、十、百、千、万、十万位上的数,判断是否符合约束条件(即相加为 n );
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		for(int i = 10000; i <= 999999; i++) {
			if(i < 100000) {				// i为五位数
				int g = i % 10;				// 个位
				int s = i / 10 % 10;		// 十位
				int b = i / 100 % 10;		// 百位
				int q = i / 1000 % 10;		// 千位
				int w = i / 10000 % 10;		// 万位
				
				if(g == w && s == q && g+s+b+q+w == n)
					System.out.println(i);
			}else {			// i为六位数
				int g = i % 10;				// 个位
				int s = i / 10 % 10;		// 十位
				int b = i / 100 % 10;		// 百位
				int q = i / 1000 % 10;		// 千位
				int w = i / 10000 % 10;		// 万位
				int sw = i / 100000 % 10;	// 十万位
				
				if(g == sw && s == w && b == q && g+s+b+q+w+sw == n) {
					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();
		
		// 五位数
		for (int i = 1; i < 10; i++) {			// 操作个位数,最小为1
			for (int j = 0; j < 10; j++) {		// 操作十位数
				for (int k = 0; k < 10; k++) {	// 操作百位数
					if(2*(i+j)+k == n) {		// 回文数各位相加满足n
//						int temp = i + j*10 + k*100 + j*1000 + i*10000;
						int temp = i*10001 + j*1010 + k*100;
						System.out.println(temp);
					}
				}
			}
		}
		
		// 六位数
		for (int i = 1; i < 10; i++) {			// 操作个位数,最小为1
			for (int j = 0; j < 10; j++) {		// 操作十位数
				for (int k = 0; k < 10; k++) {	// 操作百位数
					if(2*(i+j+k) == n) {		// 回文数各位相加满足n
//						int temp = i + j*10 + k*100 + k*1000 + j*10000 + i*100000;
						int temp = i*100001 + j*10010 + k*1100;
						System.out.println(temp);
					}
				}
			}
		}
	}
}

 

BASIC-8 回文数

问题描述

输出格式

  • 就是把四位十进制数中是回文数的都按顺序输出,遍历枚举即可。
public class Main {
	public static void main(String[] args) {
		for (int i = 1000; i <= 9999; i++) {
			int g = i % 10;			// 个
			int s = i / 10 % 10;	// 十
			int b = i / 100 % 10;	// 百
			int q = i / 1000 % 10; 	// 千
			
			if(g == q && s == b)
				System.out.println(i);
		}
	}
}
  • 控制位数的话
public class Main {
	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			for(int j = 0; j < 10; j++) {
//				int temp = i + j*10 + j*100 + i*1000;
				int temp = i * 1001 + j * 110;
				System.out.println(temp);
			}
		}
	}
}

BASIC-9 和 BASIC-8 还有下面的 BASIC-7 是一样的套路.

 

BASIC-7 特殊的数字

问题描述

输出格式

public class Main {
	public static void main(String[] args) {
		for(int i = 100; i <= 999; i++) {
			int g = i % 10;
			int s = i / 10 % 10;
			int b = i / 100 % 10;
			
			if (g*g*g+s*s*s+b*b*b == i)
				System.out.println(i);
		}
	}
}

 

BASIC-6 杨辉三角形

问题描述

输入格式

输出格式

样例输入

样例输出

数据规模与约定

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++) {
			arr[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
			}
		}
		
		// 输出杨辉三角
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= i; j++) 
				System.out.print(arr[i][j]+" ");
			System.out.println();
		}
	}
}

 

BASIC-5 查找整数

问题描述

输入格式

输出格式

样例输入

样例输出

数据规模与约定

直接遍历,设一个标识符 flag 判断是否找到了target值:

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 target = sc.nextInt();
		
		int flag = 0;
		// 查找
		for(int i = 0; i < n; i++) {
			if(arr[i] == target) {
				System.out.println(i+1);
				flag = 1;
				break;
			}
		}
		
		if (flag == 0)
			System.out.println(-1);
	}
}

 

BASIC-4 数列特征

问题描述

输入格式

输出格式

样例输入

样例输出

数据规模与约定

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		int max = 0,min = 0,sum = 0;
		for(int i = 0; i < n; i++) {
			int temp = sc.nextInt();
			
			if(i == 0)
				max = min = temp;
			
			if(temp > max)
				max = temp;
			else if(temp < min)
				min = temp;
			sum += temp;
		}
		
		System.out.println(max);
		System.out.println(min);
		System.out.println(sum);
	}
}

 

BASIC-3 字母图形

问题描述

输入格式

输出格式

样例输入

样例输出

数据规模与约定

第 i 行 i 列位置上字母均为 A,其他位置字母与其离 A的距离绝对值 有关:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				// ‘A’的ASCII码为65,直接用‘A’也行
				char temp = (char)(65 + Math.abs(j - i));
				System.out.print(temp);
			}
			System.out.println();
		}
	}
}

 

BASIC-2 01字串

问题描述

输入格式

输出格式

样例输出

用十进制转二进制

public class Main {
	public static void main(String[] args) {

		for(int i = 0; i < 32; i++) {
			String temp = Integer.toString(i, 2);
			int d = Math.abs(temp.length() - 5);
			while(d != 0) {
				System.out.print('0');
				d--;
			}
			System.out.print(temp);
			System.out.println();	
		}
	}
}

可以借助Integer.parseInt()转换成int型,再用printf格式化输出

public class Main {
	public static void main(String[] args) {

		for(int i = 0; i < 32; i++) {
//			String temp = Integer.toString(i, 2);
//			int a = Integer.parseInt(temp);
			int a = Integer.parseInt(Integer.toString(i, 2));
			System.out.printf("%05d\n",a);
		}
	}
}

 

BASIC-1 闰年判断

问题描述

输入格式

输出格式

样例输入

样例输出

样例输入

样例输出

数据规模与约定

import java.util.Scanner;

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

		if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			System.out.println("yes");
		else
			System.out.println("no");
	}
}

 

BASIC-04 Fibonacci数列

问题描述

输入格式

输出格式

样例输入

样例输出

样例输入

样例输出

数据规模与约定

  • 要计算只包含加法、减法和乘法的整数表达式除以正整数n的余数,可以在每步计算之后对n取余,结果不变。
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		int f1 = 1, f2 = 1;
		int fn = 0;
		if(n == 1 || n == 2)
			System.out.println(1);
		else {
			for(int i = 3; i <= n; i++) {
				fn = (f1 + f2) % 10007;
				f1 = f2;
				f2 = fn;
			}
			System.out.println(fn);
		}
	}
}

用递归会占用较大内存。

 

BASIC-03 圆的面积

问题描述

输入格式

输出格式

样例输入

样例输出

数据规模与约定

提示

import java.util.Scanner;

public class Main {
	final static double PI = 3.14159265358979323;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		
		System.out.printf("%.7f",Area(r));
	}
	
	public static double Area(int r) {
		double area = PI * r * r;
		return area;
	}
}

 

BASIC-02 序列求和

问题描述

输入格式

输出格式

样例输入

样例输出

样例输入

说明:

样例输出

数据规模与约定

说明:

主要是最后说明当中提到的整型范围问题,使用 Long就🆗了。

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Long temp = (long)(1 + n) * n / 2;
		System.out.println(temp);
	}
}
举报

相关推荐

0 条评论