0%

蓝桥杯基础练习

闰年判断

根据条件写出表达式即可:

  1. 年份是4的倍数而不是100的倍数;
    1. 年份是400的倍数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package basic_try;

import java.util.Scanner;

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

01字串

两种方法

  1. 五层循环,一个个来
  2. for循环,将十进制转换成二进制即可

方法1:五层循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package basic_try;

public class ZeroOne2 {
public static void main(String[] args) {
for(int a =0;a<2;a++) {
for(int b =0;b<2;b++) {
for(int c =0;c<2;c++) {
for(int d =0;d<2;d++) {
for(int e =0;e<2;e++) {
System.out.println(""+a+b+c+d+e);
}
}
}
}
}
}
}

方法2:十进制转二进制

  1. for循环
  2. 十进制转二进制
  3. 填充字符串长度为5

十进制转二进制函数

1
Integer.toBinaryString(i);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package basic_try;

public class ZeroOne {
public static void main(String[] args) {
for(int i =0;i<32;i++) {
String s = Integer.toBinaryString(i);
if(s.length()==1) {
s = "0000" + s;
}else if(s.length()==2) {
s = "000" + s;
}else if(s.length()==3) {
s = "00" + s;
}else if(s.length()==4) {
s = "0" + s;
}
System.out.println(s);
}
}
}

字母图形

根据分析得到,字符串的特点是,不断的右移,并且新增的字符是以A为中心的翻转。

解决:通过字符串截取和翻转共同实现。

  1. 生成A-Z字符串

  2. 输入n和m

  3. 分两种情况:

    第一种:是m大于n,那么左边是翻转,右边是正常序列。

    第二种:是m小于n,那么一开始同第一种,后来就都是翻转的序列了

  4. 输出

翻转字符串

是自己实现的一个函数,递归实现。每次解决最后一个字符,放到前面来,其余的递归同样解决,复杂度为O(n)。

1
2
3
4
5
6
7
8
9
//翻转字符串函数
static String reverse(String s) {
if(s.length()==0) {
return "";
}else if(s.length() == 1) {
return s;
}
return s.charAt(s.length()-1) + reverse(s.substring(0,s.length()-1));
}

答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package basic_try;

import java.util.Scanner;

public class AtoZ {

public static void main(String[] args) {
//生成A-Z字符串
String s = "";
char start = 'A';
char end = 'Z';
for(char i = start;i<=end;i++) {
s+=i;
}
//输入
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
//输出结果
for(int i=0;i<n;i++) {
if(i<=m) {
//第一种情况是左边加上翻转的+右边正确的
System.out.println(reverse(s.substring(1,i+1))+s.substring(0,m-i));
}else {
//第二种情况是都是左边翻转的
System.out.println(reverse(s.substring(i-m+1,i+1)));
}

}
}
//翻转字符串函数
static String reverse(String s) {
if(s.length()==0) {
return "";
}else if(s.length() == 1) {
return s;
}
return s.charAt(s.length()-1) + reverse(s.substring(0,s.length()-1));
}
}

另一种思路

参考别人的,非常契合题目

两层循环,一层控制输出几行,一层控制该行输出m个字符。

另外,通过字符++和字符—找到B后面的C或者B前面的A。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package basic_try;

import java.util.Scanner;

public class AtoZ2 {
public static void main(String[] args) {
//输入
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();

for(int i=0;i<n;i++) {//打印n行
char a = (char)('A'+i);//左边第一个
char b = 'A';

for(int k = 0;k<m;k++) {//每次打印的个数
if(a>'A') {//打印左边的部分,一直打印到'A'左边的
System.out.print(a--);
}else {//打印右边的部分,通过for循环控制
//System.out.print(b++);
}
}
System.out.println(b++);
}
}
}

数列特征

  1. 一次循环解决
  2. 使用ArrayList代替数组加快解决

第一种,也是我直接想到的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package winter_new_try;

import java.util.Scanner;

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

ArrayList

  1. 先排序,第一个元素为最小值,最后一个元素为最大值

    使用Collections.sort(list)进行排序

  2. 直接求最大值和最小值

    最大值:Collections.max(list)

    最小值:Collections.min(list)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package winter_new_try;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Array_feature2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
List<Integer> list = new ArrayList<Integer>();
for (int i=0;i<n;i++) {
list.add(scan.nextInt());
}
// Collections.sort(list);//排序
// System.out.println(list.get(list.size()-1));
// System.out.println(list.get(0));

//直接求
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
}
}

查找整数

  1. for循环查找
  2. 使用list的indexof直接查找

循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package basic_try;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FindInteger {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int [] arr = new int [n];
for(int i=0;i<n;i++) {
arr[i] = scan.nextInt();
}
int num = scan.nextInt();
for(int i =0;i<n;i++) {
if(num == arr[i]) {
System.out.println(i+1);
return;
}
}
System.out.println(-1);
}
}

indexof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package basic_try;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FindInteger {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
List<Integer> list = new ArrayList<Integer>();
for(int i=0;i<n;i++) {
list.add(scan.nextInt());
}
int result = list.indexOf(scan.nextInt());
if(result == -1) {
System.out.println(-1);
}else {
System.out.println(list.indexOf(scan.nextInt())+1);
}
}
}

杨辉三角形

动态创建二维数组

1
2
3
4
5
int [][] arr;
arr = new int[n][];
for(int i=0;i<n;i++) {
arr[i] = new int[n];
}

exp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package basic_try;

import java.util.Scanner;

public class Yang {
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
int [][] arr;
arr = new int[n][];
for(int i=0;i<n;i++) {
arr[i] = new int[n];
}

arr[0][0] = 1;
arr[1][0] = 1;
arr[1][1] = 1;

for(int i=2;i<n;i++) {
arr[i][0] = 1;
arr[i][i] = 1;
for(int j=1;j<=i-1;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();
}
}
}

特殊的数字

  1. 找到三位数的分开表述
  2. 三层for循环

三位数的分开表述

1
2
3
a = i/100;
b = i/10%10;
c = i%10;

exp1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package basic_try;

public class SpecialNum {
public static void main(String[] args) {
int a,b,c;
for(int i=100;i<1000;i++) {
a = i/100;
b = i/10%10;
c = i%10;
if(Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3) == i) {
System.out.println(i);
}
}
}
}

exp2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package basic_try;

public class SpecialNum2 {
public static void main(String[] args) {
for(int a=1;a<10;a++) {
for(int b=0;b<10;b++) {
for(int c=0;c<10;c++) {
if(a*100+b*10+c == Math.pow(a,3)+ Math.pow(b,3)+ Math.pow(c,3)) {
System.out.println(a*100+b*10+c);
}
}
}
}
}
}

对于这种与进制相关的,可以考虑循环来做,循环的条件是几进制,循环的层数是几位

回文数

这道题,网上的exp大多是比较第一位、低四位和第二位、第三位是否相同,循环从1000到9999

其实就是所有的二进制,然后后面和前面是镜面相同,所以输出所有的二进制,然后字符串翻转即可。

exp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package basic_try;

public class Huiwen {
public static void main(String[] args) {
for(int i=10;i<100;i++) {//前两位
System.out.println(i+reverse(i+""));
}
}
//翻转
static String reverse(String s) {
if(s.length()==0) {
return "";
}else if(s.length() == 1) {
return s;
}
return s.charAt(s.length()-1) + reverse(s.substring(0,s.length()-1));
}
}

特殊回文数

牛逼哄哄的觉着,首先是循环,分循环写得劲,然后五位和六位的判断【注意:都是if】也很简单,因为要顺序排,所以六位放字符串里面存着,末尾加回车,最后输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package basic_try;

import java.util.Scanner;

public class SpecialHui {
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
String s="";
for(int a=1;a<10;a++) {
for(int b=0;b<10;b++) {
for(int c=0;c<10;c++) {
if(2*(a+b)+c == n) {//五位
System.out.println(""+a+b+c+b+a);
}
if(2*(a+b+c) == n) {//六位
s = s+a+b+c+c+b+a+"\n";
}
}
}
}
System.out.println(s);
}
}

十进制转十六进制

使用java内置的api:

  1. String.format("%X", num)
  2. Integer.toHexString(num)【转大写用:toUpperCase()

注意:X大写则输出的十六进制都是大写,小写则小写

1
2
3
4
5
6
7
8
9
10
package basic_try;

import java.util.Scanner;

public class FormSixteen {
public static void main(String[] args) {
int num = new Scanner(System.in).nextInt();
System.out.println(String.format("%X", num));
}
}

十六进制转十进制

进制转换java都有实现好的,直接用即可。

【注意:最大二进制八位,要用long型】

1
2
3
4
5
6
7
8
9
10
package basic_try;

import java.util.Scanner;

public class FormSixteen {
public static void main(String[] args) {
String hex = new Scanner(System.in).next();
System.out.println(Long.parseLong(hex,16));
}
}

十六进制转八进制

有点被恶心到了,感觉和别人写的差球不多,,,但是自己的运行不通过,555

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package basic_try;

import java.util.Scanner;

public class FormSixteen2 {
static int in;
static String middle;
static String result1 = "";
static String result2 = "";
static String last = "";
static int lenght;

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i =0 ;i<n;i++) {
last += twotoeight(hextotwo(scan.next()))+'\n';
}
System.out.println(last);
}
//二进制转八进制
static String twotoeight(String eight) {
result1="";
lenght = eight.length();
if(lenght%3==1) {
eight = "00"+eight;
}else if(lenght%3==2) {
eight = "0"+eight;
}
if(eight.startsWith("000")) {
eight = eight.substring(3);
}
for(int i=0;i<eight.length();i=i+3) {
middle = eight.substring(i,i+3);
result1 += (middle.charAt(0)-'0')*4+ (middle.charAt(1)-'0')*2+ (middle.charAt(2)-'0')+"";
}
return result1;
}
//十六进制转二进制
static String hextotwo(String hex) {
result2="";
for(int i=0;i<hex.length();i++) {
result2+=two(hex.charAt(i));
}
return result2;
}
static String two(char six) {
switch(six) {
case '0':
return "0000";
case '1':
return "0001";
case '2':
return "0010";
case '3':
return "0011";
case '4':
return "0100";
case '5':
return "0101";
case '6':
return "0110";
case '7':
return "0111";
case '8':
return "1000";
case '9':
return "1001";
case 'A':
return "1010";
case 'B':
return "1011";
case 'C':
return "1100";
case 'D':
return "1101";
case 'E':
return "1110";
case 'F':
return "1111";
}
return "";
}
// static String oc(String two) {
// switch(two) {
// case "000":
// return "0";
// case "001":
// return "1";
// case "010":
// return "2";
// case "011":
// return "3";
// case "100":
// return "4";
// case "101":
// return "5";
// case "110":
// return "6";
// case "111":
// return "7";
// }return "";
// }
}

image-20210115102701132

照着网上的敲的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package basic_try;

import java.util.Scanner;

public class two12 {
static String[] bin = {"0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"};
static String[] oct = {"0","1","2","3","4","5","6","7",
"8","9","a","b","c","d","e","f"};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String [] str = new String[n];

for(int i=0;i<n;i++) {
str[i] = scan.next();
}
for(int i=0;i<n;i++) {
String result = hexToBin(str[i]).toString();
String octResult = binToOct(result);
if(octResult.startsWith("0")) {
octResult = octResult.substring(1);
}
if(octResult.startsWith("0")) {
octResult = octResult.substring(1);
}
System.out.println(octResult);
}
}
//十六进制转换成二进制
private static StringBuffer hexToBin(String str) {
int length = str.length();
int start = 0;
int end = 1;
StringBuffer result = new StringBuffer();
for(int i=0;i<length;i++) {
String subStr = str.substring(start,end);
start++;
end++;
String s = transform(subStr);
result.append(s);
}
return result;
}
private static String binToOct(String str) {
int length = str.length();
if(length % 3 == 1) {
str = "00"+str;
}else if(length % 3 == 2) {
str = "0" + str;
}

int start = 0;
int end = 3;
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length()/3;i++) {
String subStr = str.substring(start,end);
start += 3;
end += 3;
String s= transform2(subStr);
sb.append(s);
}
return sb.toString();
}
private static String transform(String str) {
String result = "";
switch(str) {
case "0":result = bin[0];break;
case "1":result = bin[1];break;
case "2":result = bin[2];break;
case "3":result = bin[3];break;
case "4":result = bin[4];break;
case "5":result = bin[5];break;
case "6":result = bin[6];break;
case "7":result = bin[7];break;
case "8":result = bin[8];break;
case "9":result = bin[9];break;
case "A":result = bin[10];break;
case "B":result = bin[11];break;
case "C":result = bin[12];break;
case "D":result = bin[13];break;
case "E":result = bin[14];break;
case "F":result = bin[15];break;
default:break;
}
return result;
}
private static String transform2(String str) {
String result = "";
switch(str) {
case "000":result = oct[0];break;
case "001":result = oct[1];break;
case "010":result = oct[2];break;
case "011":result = oct[3];break;
case "100":result = oct[4];break;
case "101":result = oct[5];break;
case "110":result = oct[6];break;
case "111":result = oct[7];break;
}
return result;
}
}

嗯嗯,思路还行,,,就是内存和时间限制,,,就有问题了,,,咋也不知道,咋也不敢问。。。。

数列排序

使用list或者array的sort方法

list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package basic_try;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Sort {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
List<Integer> list = new ArrayList<Integer>();
for(int i =0 ;i<n;i++) {
list.add(scan.nextInt());
}
Collections.sort(list);
for(int i =0 ;i<n;i++) {
System.out.print(list.get(i)+" ");
}
}
}

array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package basic_try;

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

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

经常使用的

1
2
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
1
int n = new Scanner(System.in).nextInt();
1
2
3
int [] arr = new int[n];

arr[i] = scan.nextInt();
1
2
3
for(int i =0 ;i<n;i++) {

}
1
System.out.print(list);
1
2
3
4
List<Integer> list = new ArrayList<Integer>();
for(int i =0 ;i<n;i++) {
list.add(scan.nextInt());
}
Q:如果阅读本文需要付费,你是否愿意为此支付1元?