我是靠谱客的博主 忧伤音响,这篇文章主要介绍蓝桥杯-练习题(1008-1016),现在分享给大家,希望可以做个参考。

1008.数组元素调整

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Description 给定一个存放整数的数组,元素个数视输入情况而定,将数组中最小的元素与首元素交换,最大的元素与尾元素交换。 Input 输入由多组测试数据组成。第一行输入一个整数t,表示测试数据的组数。 每组测试数据即为一个数组,输入数组元素的格式为:“数1 数2 … 数n”,“数”与“数”之间以空格隔开。 Output 输出有t行,每行依次对应一个数组的输出。 Sample Input 3 8 21 1 3 9 15 6 78 25 485 12 37 100 41 68 90 -67 248 987 235 185 Sample Output 1 6 8 3 9 15 21 12 25 37 78 485 -67 41 68 90 100 248 185 235 987
View Code

二、代码

复制代码
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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class T8 { /** * 接收输入的,然后排序(从小到大),得到最大数和最小数的值, * 然后跟原数组交换 */ public static void main(String[] args) throws IOException { Scanner s=new Scanner(System.in); int conut=Integer.parseInt(s.nextLine()); String[] arrStrings=new String[conut]; for (int i = 0; i < arrStrings.length; i++) { arrStrings[i]=s.nextLine(); } for (int i = 0; i < arrStrings.length; i++) { getpaixu(arrStrings[i]); } } private static void getpaixu(String str) { String[] sarr=str.split(" "); int[] arry=new int[sarr.length]; int[] ret=new int[sarr.length]; for(int i=0;i<sarr.length;i++){ ret[i]=arry[i]= Integer.parseInt(sarr[i]); } int[] arryPaixu=arry; //给排序 for(int j=0;j<arryPaixu.length;j++){ for(int k=0;k<arryPaixu.length;k++){ if(arryPaixu[j]<arryPaixu[k]){ int tmp=arryPaixu[j]; arryPaixu[j]=arryPaixu[k]; arryPaixu[k]=tmp; } } } //把最大值, for(int co=0;co<ret.length;co++){ if(arryPaixu[0]==ret[co]){ int tmp=ret[0]; ret[0]=ret[co]; ret[co]=tmp; } if(arryPaixu[arryPaixu.length-1]==ret[co]){ int tmp=ret[ret.length-1]; ret[arryPaixu.length-1]=ret[co]; ret[co]=tmp; } } for(int co=0;co<ret.length;co++){ System.out.print(ret[co]+" "); } System.out.println(); } }
View Code

 

1009.谁在说谎

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
11
Description 张三说李四在说谎,李四说王五在说谎,王五说张三和李四在说谎,请问:这三人中到底谁说的是真话,谁说的是假话? Input 无输入 Output 按“XX said is true/false”格式输出结果,每一个的输出占一行。(样例输出只是格式提示) Sample Input 无输入 Sample Output ZhangSan said is true LiSi said is true
View Code

二、代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class T9 { 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++){ if(((A==0&&B==1)||(A==1&&B==0))&&((B==0&&C==1)||(B==1&&C==0))&&((C==0&&A+B==2)||(C==1&&(A+B)!=2))){ System.out.println("ZhangSan said is "+(A==0?true:false)); System.out.println("LiSi said is "+(B==0?true:false)); System.out.println("WangWu said is "+(C==0?true:false)); } } } } } }
View Code

 

1010.兔子产仔

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Description 一般而言,兔子在出生2个月后就有繁殖能力,一对兔子每个月能生出一对小兔子来,如果所有兔子都不死,那么经过n个月总共有多少对兔子? Input 输入由多组测试数据组成。第一行输入一个整数t,表示测试数据的组数。 接下来的t行,每行输入一个月份n。 Output 输出有t行,每行依次对应一个月份n的输出结果:经过n个月,每个月有多少对兔子。 Sample Input 3 5 10 13 Sample Output 1 1 2 3 5 1 1 2 3 5 8 13 21 34 55 1 1 2 3 5 8 13 21 34 55 89 144 233
View Code

二、代码

复制代码
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
import java.util.Scanner; public class T10 { /** * 一般而言,兔子在出生2个月后就有繁殖能力,一对兔子每个月能生出一对小兔子来, * 如果所有兔子都不死,那么经过n个月总共有多少对兔子? */ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int[] arr=new int[n]; for (int i = 0; i < arr.length; i++) { arr[i]=scanner.nextInt(); } for (int j = 0; j < arr.length; j++) { for (int i = 1; i <= arr[j]; i++) { System.out.print(getMoney(i)+" "); } System.out.println(); } } //递归方法 public static int getMoney(int n){ if(n==1||n==2) return 1; else { return getMoney(n-1)+getMoney(n-2); } } }
View Code

 

1011.回文数

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
11
Description 一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 Input 输入任意五位数 Output 如果是回文数则输出“true” 如果不是回文数则输出“false” Sample Input 12321 Sample Output true
View Code

二、代码

复制代码
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
import java.util.Scanner; public class T11 { /** * 一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 */ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); if (n<10000||n>=100000) { System.out.println("这不是一个五位数"); return; } if (n%10==n/10000) { if (n/10%10==n/1000%10) { System.out.println("true"); }else{ System.out.println("false"); } }else{ System.out.println("false"); } } }
View Code

 

1012.字符串连接

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
11
Description 两个字符串连接 Input 输入任意两个字符串 Output 输出两个字符串连接后的字符串 Sample Input adc ert Sample Output adcert
View Code

二、代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner; public class T12 { /** * @param args */ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s1=scanner.next(); String s2=scanner.next(); //concat字符串连接 String string=s1.concat(s2); System.out.println(string); } }
View Code

 

1013.求和

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
Description 输出1+2!+3!+...+20!的和 Input 无输入 Output 计算结果。(样例输出仅仅是格式提示) Sample Input 无输入 Sample Output 123456789
View Code

二、代码

复制代码
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
import java.math.BigInteger; public class T13 { /** * 输出1+2!+3!+...+20!的和 */ public static void main(String[] args) { BigInteger sum=BigInteger.valueOf(0); for (int i = 1; i <= 20; i++) { sum=sum.add(getNum(i)); } System.out.println(sum); } //递归求阶乘 public static BigInteger getNum(int n){ if (n==1) { return BigInteger.valueOf(1); }else if (n==2) { return BigInteger.valueOf(2); }else{ return getNum(n-1).multiply(BigInteger.valueOf(n)); } } }
View Code

 

1014.统计字符个数

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
Description 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 Input 输入任意字符 Output 统计出其中英文字母、空格、数字和其它字符的个数,并依次输出。 Sample Input sddf 123 !!! Sample Output 4 3 2 3
View Code

二、代码

复制代码
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
import java.util.Scanner; public class T14 { /** * 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 */ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int yinwen=0,kongge=0,shuzi=0,qita=0; String string=scanner.nextLine(); kongge=string.length(); kongge-=string.length(); for (int i = 0; i < string.length(); i++) { char a=string.charAt(i); if ((a>='a'&&a<='z')||(a>='A'&&a<='Z')) { yinwen++; }else if (a-48>=0&&a-48<=9) { shuzi++; }else if (a==32) { kongge++; }else{ qita++; } } System.out.println(yinwen); System.out.println(shuzi); System.out.println(kongge); System.out.println(qita); } }
View Code

 

1015.最大公约数最小公倍数

一、题干

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
Description 输入两个正整数m和n,求其最大公约数和最小公倍数。 Input 输入任意两个正整数 Output 输出两行,分别是输入两个数的最小公约数和最大公倍数 Sample Input 10 8 Sample Output 2 40
View Code

二、代码

复制代码
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
import java.util.Scanner; public class T15 { /** * 输入两个正整数m和n,求其最大公约数和最小公倍数。 */ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int b=scanner.nextInt(); System.out.println(geiMin(a,b)); System.out.println(geiMax(a,b)); } //最大公约数 public static int geiMin(int a,int b) { for (int i = a<b?a:b; i > 1; i--) { if (a%i==0&&b%i==0) { return i; } } return 1; } //最小公倍数 public static int geiMax(int a,int b) { int ret=0; for (int i = 1; ; i++) { if (a*i%b==0) { ret = a*i; break; } } return ret; } }
View Code

 

 

最后

以上就是忧伤音响最近收集整理的关于蓝桥杯-练习题(1008-1016)的全部内容,更多相关蓝桥杯-练习题(1008-1016)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部