题目来源:
蓝桥杯练习系统(写博客日期为2019.3.23,所以可能读者看到的时候,更新了新的题)
这里只提供每道题的我的解题代码,仅供参考。这里不会写解题思路和详解,如果有需要的话,请留言给我,我会在留言区回复。vip题目来源dotcpp(顺序跟练习系统一样,只不过我没有vip,所以在dotcpp网站中看题并解题)。
试题集截图:
入门训练:
1-1 BEGIN-1 A+B问题
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
}
1-2 BEGIN-2 序列求和
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
double n = in.nextInt();
double s = (1 + n) / 2;
double t = s * n;
System.out.println((long)t);
}
}
1-3 BEGIN-3 圆的面积
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
final double PI = 3.14159265358979323;
double a = in.nextInt();
double c = a*a*PI;
System.out.printf("%.7f", c);
}
}
1-4 BEGIN-4 Fibonacci数列
public class Main {
public static void main(String[] ars){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n < 3){
System.out.println(1);
}else {
int[] a = new int[n + 1];
a[1] = 1 % 10007;
a[2] = 1 % 10007;
for (int i = 3; i <= n; i++) {
a[i] = (a[i - 1] + a[i - 2]) % 10007;
}
System.out.println(a[n]);
}
}
}
基础练习:
2-1 BASIC-1 闰年判断
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int y = in.nextInt();
if((y%4 == 0 && y%100 != 0) || (y%400 == 0)){
System.out.println("yes");
}else {
System.out.println("no");
}
}
}
2-2 BASIC-2 01字串
public class Main {
public static void main(String[] args){
for(int i=0; i<32; i++) {
String str = Integer.toBinaryString(i);
System.out.printf("%05d", Integer.valueOf(str));
System.out.println();
}
}
}
2-3 BASIC-3 字母图形
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 = "BCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder str = new StringBuilder(s);
str = str.reverse();
str.append("A");
str.append(s);
int a = str.indexOf("A");
int b = a+m;
for(int i=0; i<n; i++){
System.out.println(str.substring(a, b));
a--;
b--;
}
}
}
2-4 BASIC-4 数列特征
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] s = new int[n];
for(int i=0; i<n; i++){
s[i] = in.nextInt();
}
int max = s[0];
int min = s[0];
int sum = 0;
for(int j=0; j<s.length; j++){
if(s[j] > max){
max = s[j];
}
if(s[j] < min){
min = s[j];
}
sum = sum + s[j];
}
System.out.println(max);
System.out.println(min);
System.out.println(sum);
}
}
2-5 BASIC-5 查找整数
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] s = new int[n];
for(int i=0; i<n; i++){
s[i] = in.nextInt();
}
int a = in.nextInt();
in.close();
int count = 0;
for(int j=0; j<s.length; j++){
if(s[j] == a){
count = j+1;
break;
}
}
if(count == 0){
System.out.println(-1);
}else {
System.out.println(count);
}
}
}
2-6 BASIC-6 杨辉三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] a = new int[n][n];
if(n == 1){
System.out.println(1);
}else{
a[0][0] = 1;
for(int i=1; i<n; i++){
a[i][0] = 1;
a[i][i] = 1;
if(n != 2){
for(int j=1; j<=i-1; j++){
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
}
// 显示
for(int i=0; i<n; i++){
for(int j=0; j<=i; j++){
System.out.print(a[i][j]);
if(i!=j){
System.out.print(' ');
}
}
System.out.println();
}
}
}
2-7 BASIC-7 特殊的数字
public class Main {
public static void main(String[] args){
for(int dd=100; dd<=999; dd++){
String data = dd + "";
char d0 = data.charAt(0);
char d1 = data.charAt(1);
char d2 = data.charAt(2);
double sum1 = Math.pow((d0-'0'), 3) + Math.pow((d1-'0'), 3) + Math.pow((d2-'0'), 3);
if((int)sum1 == dd){
System.out.println(dd);
}
}
}
}
2-8 BASIC-8 回文数
public class Main {
public static void main(String[] args){
StringBuilder a = new StringBuilder();
for(int i=1; i<=9; i++){
for(int j=0; j<=9; j++){
a.append(i);
a.append(j);
a.append(j);
a.append(i);
System.out.println(a.toString());
a.delete(0, a.length());
}
}
}
}
2-9 BASIC-9 特殊回文数
2-9-1 解法一
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=10000; i<=999999; i++){
String data = "" + i;
int sum = 0;
for(int k=0; k<data.length(); k++){
sum = sum + (data.charAt(k)-'0');
}
int len = data.length();
int count = len/2;
// 计算相等的个数,如果是2,3则满足
StringBuilder flag = new StringBuilder();
for(int j=0; j<count; j++){
if(data.charAt(j) == data.charAt(len-j-1)){
flag.append(1);
}else{
flag.append(0);
}
}
if((flag.toString().equals("111") || flag.toString().equals("11")) && (n == sum)){
System.out.println(i);
}
else{
continue;
}
}
}
}
2-9-2 解法二
import java.util.Scanner;
public class Main {
public static void f5(int n){
/* 五位数处理代码 */
StringBuilder anser1 = new StringBuilder();
// 测试第1位的值
for(int i=1; i<=9; i++){
// 测试第2位的值
for(int j=0; j<=9; j++){
// 计算第3位的值
int third_number = n - 2*i - 2*j;
if(third_number>=0 && third_number<=9){
anser1.append(i);
anser1.append(j);
anser1.append(third_number);
anser1.append(j);
anser1.append(i);
System.out.println(anser1);
anser1.delete(0, anser1.length());
}else{
continue;
}
}
}
}
public static void f6(int n){
/* 六位数处理代码 */
StringBuilder anser2 = new StringBuilder();
// temp: 前三位相加的值
int temp = n/2;
// 测试第1位的值
for(int i=1; i<=9; i++){
// 测试第2位的值
for(int j=0; j<=9; j++){
// 计算第3位的值
int third_number2 = temp-i-j;
if(third_number2>=0 && third_number2<=9){
anser2.append(i);
anser2.append(j);
anser2.append(third_number2);
anser2.append(third_number2);
anser2.append(j);
anser2.append(i);
System.out.println(anser2);
anser2.delete(0, anser2.length());
}else{
continue;
}
}
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
f5(n);
// 偶数才可能是6位
if(n%2==0){
f6(n);
}
}
}
2-10 BASIC-10 十进制转十六进制
import java.util.Scanner;
public class Main {
public static char f(long a){
char temp = ' ';
if(a<=9){
temp = (char)(a+48);
}else {
switch ((int) a) {
case 10:
temp = 'A';
break;
case 11:
temp = 'B';
break;
case 12:
temp = 'C';
break;
case 13:
temp = 'D';
break;
case 14:
temp = 'E';
break;
case 15:
temp = 'F';
break;
}
}
// System.out.println(temp);
return temp;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
long a = in.nextLong();
StringBuilder ss = new StringBuilder();
if(a == 0){
ss.append(0);
}else {
while (a != 0) {
long number = a % 16;
char s = f(number);
ss.append(s);
a = a / 16;
}
}
System.out.println(ss.reverse().toString());
}
}
2-11 BASIC-11 十六进制转十进制
import java.util.*;
public class Main {
public static int f(char a){
int x=0;
if(a>='0' && a<='9'){
x = a-'0';
}else{
switch (a){
case 'A': x = 10;break;
case 'B': x = 11;break;
case 'C': x = 12;break;
case 'D': x = 13;break;
case 'E': x = 14;break;
case 'F': x = 15;break;
default:break;
}
}
return x;
}
public static void h(String str){
int len = str.length();
double x = 0;
for(int i=0; i<len; i++){
int value = f(str.charAt(i));
x = value*Math.pow(16, len-1-i) + x;
}
System.out.println((long)x);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String a = in.next();
in.close();
h(a);
}
}
2-12 BASIC-12 十六进制转八进制
import java.util.Scanner;
public class Main {
public static String f(String s){
int len = s.length();
StringBuilder str = new StringBuilder();
for(int i=0; i<len; i++){
switch (s.charAt(i)){
case '0': str.append("0000");break;
case '1': str.append("0001");break;
case '2': str.append("0010");break;
case '3': str.append("0011");break;
case '4': str.append("0100");break;
case '5': str.append("0101");break;
case '6': str.append("0110");break;
case '7': str.append("0111");break;
case '8': str.append("1000");break;
case '9': str.append("1001");break;
case 'A': str.append("1010");break;
case 'B': str.append("1011");break;
case 'C': str.append("1100");break;
case 'D': str.append("1101");break;
case 'E': str.append("1110");break;
case 'F': str.append("1111");break;
default: break;
}
}
return str.toString();
}
public static void g(String str){
int len = str.length();
StringBuilder a = new StringBuilder();
for(int i=0; i<len; i+=3){
int temp = (str.charAt(i)-'0')*4+(str.charAt(i+1)-'0')*2+(str.charAt(i+2)-'0');
a.append(temp);
}
System.out.println(a.toString().replaceFirst("^0*", ""));
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] a = new String[n];
for(int i=0; i<n; i++){
a[i] = in.next();
}
in.close();
for(int j=0; j<n; j++){
String str2 = f(a[j]);
int len2 = str2.length();
if(len2 % 3 == 1){str2 = "00" + str2;}
else if(len2 % 3 == 2){str2 = "0" + str2;}
g(str2);
}
}
}
2-13 BASIC-13 数列排序
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i=0; i<n; i++){
a[i] = in.nextInt();
}
for(int j=0; j<n-1; j++){
for(int k=j+1; k<n; k++){
if(a[j]>a[k]){
int tt = a[k];
a[k] = a[j];
a[j] = tt;
}
}
}
for(int j=0; j<n; j++){
System.out.print(a[j]);
if(j != n-1){
System.out.print(" ");
}
}
}
}
未完待续。。。(更新于2019.3.23)