当前位置:   article > 正文

蓝桥杯java历年真题及答案整理(共100道题目及答案)_蓝桥杯真题

蓝桥杯真题

文章目录

1.字符排序

算法是这样的,如果给定N个不同字符,将这N个字符全排列,最终的结果将会是N!种。如:给定 A、B、C三个不同的字符,则结果为:ABC、ACB、BAC、BCA、CAB、CBA一共3!=3*2=6种情况。

package Question1_9;
import java.util.Scanner;
import java.util.Vector;

public class Question1 {
	public static long count=0;
	private void fullPermutation(Vector<Character>sourse, Vector<Character> result) {
		if(sourse.size()==0){
			for (int i = 0; i < result.size(); i++) {
				System.out.print(result.elementAt(i));
			}
			System.out.print("\n");
			count++;
			return;
		}
				for (int i = 0; i < sourse.size(); i++) {
			Vector<Character>tsourse=new Vector<Character>(sourse);
			Vector<Character>tresult=new Vector<Character>(result);
			tresult.add(sourse.elementAt(i));
			tsourse.remove(i);
			new Question1().fullPermutation(tsourse, tresult);
		}
	}
	
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n=scanner.nextInt();
		Vector<Character> sourse=new Vector<Character>();
		Vector<Character> result=new Vector<Character>();
		for (int i = 0; i < n; i++) {
			sourse.add((char)('A'+i));
		}
		
		new Question1().fullPermutation(sourse, result);
		System.out.println(Question1.count);
	}
}

方法二:
import java.util.ArrayList;  
import java.util.Iterator;  
import java.util.LinkedHashSet;  
import java.util.List;  
import java.util.Scanner;  
import java.util.Set;  
  
public class Demo03 {  
    // 去掉重复元素,放入lis   
    public static void removeDuplicate(String s,Set<Character> lis){  
        for(char x:s.toCharArray()){  
            lis.add(x);  
        }  
    }  
    // 为方便操作 将 sets 转 lis    
    public static void convert(List<Character> lis,Set<Character> sets){  
        Iterator<Character> iter = sets.iterator();  
        while(iter.hasNext()){  
            lis.add(iter.next());  
        }  
    }  
    // 检测符合条件的元素组合   
    public static void check(Set<Character> sets){  
        List<Character> lis = new ArrayList<Character>();  
        convert(lis,sets);  // 为方便操作 将 sets 转 lis    
        StringBuffer sb = new StringBuffer();  
        for(int i=0;i<lis.size()-2;i++){  
            for(int j=i+1;j+1<lis.size();j++){   // 向后添加两位,所以 j+1<lis.size()   
                for(int k=j+1;k<lis.size();k++){  
                    sb.append(lis.get(i));  
                    sb.append(lis.get(j));  
                    sb.append(lis.get(k));  
                    System.out.println(sb); // 输出组合   
                    sb.setLength(0);    // 清空   
                }  
            }  
        }  
          
    }  
    public static void main(String[] args){  
        Scanner scan = new Scanner(System.in);  
        System.out.println("输入串(不大于30个字符)。");  
        String s = scan.nextLine();  
        Set<Character> sets = new LinkedHashSet<Character>();  
        removeDuplicate(s,sets);    // 去掉重复元素,放入lis   
        check(sets);    // 检测符合条件的元素组合   
    }  
}  
  • 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

运行结果:
输入串(不大于30个字符)。
abcd
abc
abd
acd
bcd

2.串的简单处理

串的处理
在实际的开发工作中,对字符串的处理是最常见的编程任务。
本题目即是要求程序对用户输入的串进行处理。具体规则如下:

  1. 把每个单词的首字母变为大写。
  2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
  3. 把单词中间有多个空格的调整为1个空格。
    例如:
    用户输入:
    you and me what cpp2005program
    则程序输出:
    You And Me What Cpp_2005_program
    用户输入:
    this is a 99cat
    则程序输出:
    This Is A 99_cat
    我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。
    每个单词间由1个或多个空格分隔。
    假设用户输入的串长度不超过200个字符。
package Question1_9;
import java.util.Scanner;
import java.util.Vector;

public class Question2 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		String string=scanner.nextLine();
		Vector<Character>vector=new Vector<Character>();
		for (int i = 0; i < string.length(); i++) {
			vector.add(string.charAt(i));
		}
		try {
			int index=0;
			while (index<vector.size()) {
				if(index==0&&vector.elementAt(index)>='a'&&vector.elementAt(index)<='z'){
					vector.set(index, (char)(vector.elementAt(index)-('a'-'A')));
				}else if(vector.elementAt(index-1)==' '&&vector.elementAt(index)==' '){
					vector.remove(index);
					index--;
				}else if (vector.elementAt(index-1)==' '&&(vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')) {
					vector.set(index, (char)(vector.elementAt(index)-('a'-'A')));
				}else if((vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')&&(vector.elementAt(index-1)>='0'&&vector.elementAt(index-1)<='9')){
					vector.add(index, '_');
					index++;
				}else if((vector.elementAt(index-1)>='a'&&vector.elementAt(index-1)<='z')&&(vector.elementAt(index)>='0'&&vector.elementAt(index)<='9')){
					vector.add(index, '_');
					index++;
				}
				index++;
			}
			for (int i = 0; i <vector.size(); i++) {
				System.out.print(vector.elementAt(i));
			}
			System.out.println();
		} catch (ArrayIndexOutOfBoundsException e) {
			// TODO: handle exception
		}
	}
}
  • 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

运行结果:
you and me what cpp2005program
You And Me What Cpp_2005_program

3.猜算式

看下面的算式:
□□ x □□ = □□ x □□□
它表示:两个两位数相乘等于一个两位数乘以一个三位数。
如果没有限定条件,这样的例子很多。
但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。
该算式中1至9的每个数字出现且只出现一次!
比如:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186

请编程,输出所有可能的情况!
注意:
左边的两个乘数交换算同一方案,不要重复输出!
不同方案的输出顺序不重要

package Question1_9;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

public class Question3 {
	public static long count=0;
	public static List<Vector<Character>> filteredNonRedundantResults;
	private static boolean isfilter(Vector<Character> result) {
		int a=(result.elementAt(0)-'0')*10+(result.elementAt(1)-'0');
		int b=(result.elementAt(2)-'0')*10+(result.elementAt(3)-'0');
		int c=(result.elementAt(4)-'0')*10+(result.elementAt(5)-'0');
		int d=(result.elementAt(6)-'0')*100+(result.elementAt(7)-'0')*10+(result.elementAt(8)-'0');
		if(a*b==c*d){
			return true;
		}
		return false;
	}
	public static void print(Vector<Character>vector) {
		System.out.printf("%c%c x %c%c = %c%c x %c%c%c",vector.elementAt(0),vector.elementAt(1),vector.elementAt(2),vector.elementAt(3),vector.elementAt(4),vector.elementAt(5),vector.elementAt(6),vector.elementAt(7),vector.elementAt(8));
	}
	private static void fullPermutation(Vector<Character>sourse, Vector<Character> result) {
		if(sourse.size()==0&&isfilter(result)){
			boolean exit=false;
			for (int i = 0; i < filteredNonRedundantResults.size(); i++) {
				int ra=(result.elementAt(0)-'0')*10+(result.elementAt(1)-'0');
				int rb=(result.elementAt(2)-'0')*10+(result.elementAt(3)-'0');
				int fa=(filteredNonRedundantResults.get(i).elementAt(0)-'0')*10+(filteredNonRedundantResults.get(i).elementAt(1)-'0');
				int fb=(filteredNonRedundantResults.get(i).elementAt(2)-'0')*10+(filteredNonRedundantResults.get(i).elementAt(3)-'0');
				if(ra==fb&&rb==fa){
					exit=true;
					break;
				}
			}
			if(exit==false){
				filteredNonRedundantResults.add(new Vector<Character>(result));
			}
			return;
		}
		for (int i = 0; i < sourse.size(); i++) {
			result.add(sourse.elementAt(i));
			sourse.remove(i);
			fullPermutation(sourse, result);
			sourse.add(i, result.elementAt(result.size()-1));
			result.remove(result.size()-1);
		}
	}
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n=9;
		Vector<Character> sourse=new Vector<Character>();
		Vector<Character> result=new Vector<Character>();
		for (int i = 1; i <= n; i++) {
			sourse.add((char)('0'+i));
		}
		Question3.filteredNonRedundantResults=new ArrayList<Vector<Character>>();
		Question3.fullPermutation(sourse, result);
		for (int i = 0; i < Question3.filteredNonRedundantResults.size(); i++) {
			Question3.print(Question3.filteredNonRedundantResults.get(i));
			System.out.println();
		}
	}
}
  • 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

运行结果:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186
58 x 67 = 29 x 134
58 x 69 = 23 x 174
58 x 73 = 29 x 146
58 x 96 = 32 x 174
63 x 74 = 18 x 259
64 x 79 = 32 x 158
73 x 96 = 12 x 584
76 x 98 = 14 x 532

方法二:
import java.util.List;
import java.util.ArrayList;
public class Demo01 {
static List lis = new ArrayList(); // 结果1(有重复的结果)
static List lis2 = new ArrayList(); // 结果2(去重复后的结果)
// 初始化数组为1~9
public static void init(int[] n) {
for(int i=0;i<9;i++){
n[i] = i+1; // 初始化数组为1~9
}
}
// 是否重复
public static boolean isDup(String s1,String s2){
String a1 = s1.substring(0,2);
String a2 = s1.substring(2,4);
String b1 = s2.substring(0,2);
String b2 = s2.substring(2,4);
if(a1.equals(b2)&&a2.equals(b1)){
return true;
}else{
return false;
}
}
// 去除lis重复元素
public static void removeDuplicate(){
lis2.add(lis.get(0));
for(int i=1;i<lis.size();i++){
boolean flag = true; // 标记是否重复
for(int j=0;j<lis2.size();j++){
flag = isDup(lis.get(i),lis2.get(j)); // 判断是否重复
if(flag) break; // 如果元素重复,直接跳出这层循环,测试下个数据
}
if(!flag){
lis2.add(lis.get(i)); // 不重复,则添加
}
}
}
// 输出
public static void print(){
for(String s:lis2){
String a = s.substring(0,2);
String b = s.substring(2,4);
String c = s.substring(4,6);
String d = s.substring(6);
System.out.println(a+" x “+b+” = “+c+” x "+d);
}
}
// 检测结果,符合条件的输出
public static void check(int[] n){
StringBuffer sb = new StringBuffer();
for(int x:n){ // 数字数组转为字符串
sb.append(x);
}
int a = Integer.parseInt(sb.substring(0,2).toString());
int b = Integer.parseInt(sb.substring(2,4).toString());
int c = Integer.parseInt(sb.substring(4,6).toString());
int d = Integer.parseInt(sb.substring(6).toString());
if(ab==cd){
lis.add(sb.toString()); // 添加结果
}
}
// 全排列进行测试
public static void allSort(int[] n,int start,int end){
if(start>=end){
check(n); // 检测结果,符合条件的输出
return ;
}else{
for(int i=start;i<=end;i++){
int t = n[start]; // 交换元素
n[start] = n[i];
n[i] = t;
// 递归全排列
allSort(n,start+1,end);
t = n[start]; // 还原元素
n[start] = n[i];
n[i] = t;
}
}
}
public static void fun(){
int[] n = new int[9];
init(n); // 初始化数组为1~9
allSort(n,0,n.length-1); // 全排列测试
removeDuplicate(); // 去除重复元素
}
public static void main(String[] args){
fun(); //
print(); // 输出结果
}
}

运行结果:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186
58 x 67 = 29 x 134
58 x 69 = 23 x 174
58 x 73 = 29 x 146
58 x 96 = 32 x 174
63 x 74 = 18 x 259
64 x 79 = 32 x 158
73 x 96 = 12 x 584
76 x 98 = 14 x 532

4.Excel地址转换

Excel是最常用的办公软件。每个单元格都有唯一的地址表示。比如:第12行第4列表示为:“D12”,第5行第255列表示为“IU5”。
事实上,Excel提供了两种地址表示方法,还有一种表示法叫做RC格式地址。
第12行第4列表示为:“R12C4”,第5行第255列表示为“R5C255”。
你的任务是:编写程序,实现从RC地址格式到常规地址格式的转换。
【输入、输出格式要求】
用户先输入一个整数n(n<100),表示接下来有n行输入数据。
接着输入的n行数据是RC格式的Excel单元格地址表示法。
程序则输出n行数据,每行是转换后的常规地址表示法。
例如:用户输入:
2
R12C4
R5C255
则程序应该输出:
D12
IU5
package Question1_9;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;
public class Question4 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
scanner.nextLine(); //必须加上的,不然会导致输入不准确!
while (n>0) {
String string=scanner.nextLine();
String strings[]=string.split(“C”);
strings[0]=strings[0].substring(1, strings[0].length());
int hangshu=Integer.parseInt(strings[0]),lieshu=Integer.parseInt(strings[1]);//获取行数和列数
/*
* 对列数进行变换
*/
Stackstack=new Stack();
while(lieshu>0){
if(lieshu%26==0){
stack.push(‘Z’);
lieshu=lieshu/26-1;
}else {
stack.push((char)(‘A’-1+lieshu%26));
lieshu=lieshu/26;
}
}
while (!stack.empty()) {
System.out.print(stack.pop());
}
System.out.println(hangshu);
n–;
}
}
}
运行结果:
输入一个整数n(n<100)
2
R12C4
R5C255
D12
IU5

方法二:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo03 {
// 计算出字母的个数
public static int checkCount(int n){
int count = 1;
while(true){
int t = (int)Math.pow(26, count);
if(n > t){
count++;
n -= t;
}else{
return count;
}
}
}
// 添加余数对应的字母
public static char f(int n){
if(n26){
return ‘Z’;
}else{
return (char)(‘A’-1+n%26);
}
}
// 计算结果
public static String fun(int Row,int Col){
StringBuffer sb = new StringBuffer();
int count = checkCount(Col); // 计算出字母的个数
while(count>0){
if(Col%26
0){ //如果能除尽
// 例(702):702/26时(2余0) 应该分配成(1,26)
// 个位 加26(‘Z’)时 就等于 十位上的2 去掉(1个26),(一个(个位的26)对应一个(十位的1))
// 修改n的值 2-1; n就等于1; 这时就分配成了(1,26);
sb.insert(0,‘Z’); // 添加’Z’
Col = Col/26 -1;
}else{
sb.insert(0,f(Col));// 添加余数r对应的字母
Col /= 26; // 修改 n 的值
}
count–;
}
sb.append(Row); // RC地址后添加(行号)
return sb.toString();
}
// 输入数据
public static void input(String[] s){
Scanner scan = new Scanner(System.in);
int i = 0;
while(i<s.length){ // 输入n个数据
s[i++] = scan.nextLine();
}
}
// 拆分并计算结果
public static void splitOper(String[] s){
Pattern p = Pattern.compile(“[0-9]+”); // 以数字做分隔
for(int i=0;i<s.length;i++){
Matcher m = p.matcher(s[i]); // 得到所有数字
m.find(); // 得到第一个数字
int Row = Integer.parseInt(m.group()); // 取出第一个数字
m.find(); // 得到第二个数字
int Col = Integer.parseInt(m.group()); // 取出第二个数字
System.out.println(fun(Row,Col)); // 计算结果并输出
}
}
// 主函数
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println(“输入一个整数n(n<100)”);
String[] s = new String[scan.nextInt()];
input(s); // 输入数据
splitOper(s); // 拆分并计算结果
}
}

运行结果:
输入一个整数n(n<100)
2
R12C4
R5C255
D12
IU5

5.手机尾号评分

30年的改革开放,给中国带来了翻天覆地的变化。2011全年中国手机产量约为11.72亿部。手机已经成为百姓的基本日用品!给手机选个好听又好记的号码可能是许多人的心愿。
但号源有限,只能辅以有偿选号的方法了。
这个程序的目的就是:根据给定的手机尾号(4位),按照一定的规则来打分。其规则如下:

  1. 如果出现连号,不管升序还是降序,都加5分。例如:5678,4321都满足加分标准。
  2. 前三个数字相同,或后三个数字相同,都加3分。例如:4888,6665,7777都满足加分的标准。
    注意:7777因为满足这条标准两次,所以这条规则给它加了6分。
  3. 符合AABB或者ABAB模式的加1分。例如:2255,3939,7777都符合这个模式,所以都被加分。
    注意:7777因为满足这条标准两次,所以这条标准给它加了2分。
  4. 含有:6,8,9中任何一个数字,每出现一次加1分。例如4326,6875,9918都符合加分标准。其中,6875被加2分;9918被加3分。
    尾号最终得分就是每条标准的加分总和!
    要求程序从标准输入接收数据,在标准输出上输出结果。
    输入格式为:第一行是一个整数n(<100),表示下边有多少输入行,接下来是n行4位一组的数据,就是等待计算加分的手机尾号。
    例如,输入:
    14
    3045
    ….
    ……
    6789
    8866
    则输出:
    0
    0
    ….

    8
    5
    */

package Question1_9;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;
public class Question5 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
scanner.nextLine();
while ((n–)>0) {
String telphone=scanner.nextLine();
int sum=0;
/*
* 情况一
/
if(telphone.charAt(0)-telphone.charAt(1)1){
char ch=telphone.charAt(0);
int index=0;
while (index<4&&ch
telphone.charAt(index)) {
ch–;
index++;
}
if(index>=4){
sum+=5;
}
}
if (telphone.charAt(0)-telphone.charAt(1)-1) {
char ch=telphone.charAt(0);
int index=0;
while (index<4&&ch
telphone.charAt(index)) {
ch++;
index++;
}
if(index>=4){
sum+=5;
}
}
/

* 情况二
*/
if (telphone.charAt(0)==telphone.charAt(1)&&telphone.charAt(1)==telphone.charAt(2)) {
sum+=3;
}
if(telphone.charAt(1)==telphone.charAt(2)&&telphone.charAt(2)==telphone.charAt(3)){
sum+=3;
}

		/*
		 * 情况三
		 */
		if(telphone.charAt(0)==telphone.charAt(1)&&telphone.charAt(2)==telphone.charAt(3)){
			sum+=1;
		}
		if(telphone.charAt(0)==telphone.charAt(2)&&telphone.charAt(1)==telphone.charAt(3)){
			sum+=1;
		}
		/*
		 * 情况四
		 */
		for (int i = 0; i < 4; i++) {
			if(telphone.charAt(i)=='6'||telphone.charAt(i)=='8'||telphone.charAt(i)=='9'){
				sum+=1;
			}
		}
		System.out.println(sum);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

}
运行结果:
14
3045
0211
……
……
……
8
5

方法二:
import java.util.Scanner;
public class Demo07 {
// 输入数据
public static void input(String[] n){
Scanner scan = new Scanner(System.in);
int i = 0;
while(i<n.length){
n[i++] = scan.nextLine();
}
}
// 得到分数
public static int getGrade(String n){
int s = 0; // 总分
char[] x = n.toCharArray();
int a = x[0]-‘0’;
int b = x[1]-‘0’;
int c = x[2]-‘0’;
int d = x[3]-‘0’;

    if(a+1==b&&b+1==c&&c+1==d||a-1==b&&b-1==c&&c-1==d){  
        s += 5; // 1.出现连号 +5分   
    }  
      
    if(a==b&&b==c){   
        s += 3; // 2.三个数字相同 (前三)+3分   
    }  
    if(b==c&&c==d){   
        s += 3; // 2.三个数字相同 (后三)+3分   
    }  
      
    if(a==b&&c==d){   
        s += 1; // 3.AABB模式 +1分   
    }  
    if(a==c&&b==d){   
        s += 1; // 3.ABAB模式 +1分   
    }  
      
    if(a==6||a==8||a==9){     
        s += 1; // 4.a含有:6 或 8 或 9  +1分   
    }  
    if(b==6||b==8||b==9){     
        s += 1; // 4.b含有:6 或 8 或 9  +1分   
    }  
    if(c==6||c==8||c==9){     
        s += 1; // 4.c含有:6 或 8 或 9  +1分   
    }  
    if(d==6||d==8||d==9){     
        s += 1; // 4.d含有:6 或 8 或 9  +1分   
    }  
    return s;  
}  
//  取得每一行分数并输出   
public static void fun(String[] n){  
    for(String x:n){      
        System.out.println(getGrade(x));  
    }  
}  
public static void main(String[] args){  
    Scanner scan = new Scanner(System.in);  
    String[] n = new String[scan.nextInt()];  
    input(n);   // 输入数据   
    fun(n);   
}  
  • 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

}

运行结果:
14
3045
0211
2345
6543
7777
8888
7878
7788
6688
2424
2244
9918
6789
8866
0
0
5
6
8
12
3
3
5
1
1
3
8
5

6.括号问题

下面的代码用于判断一个串中的括号是否匹配所谓匹配是指不同类型的括号必须左右呼应,可以相互包含,但不能交叉
例如:
…(…[…]…)… 是允许的
…(…[…)…]… 是禁止的
对于 main 方法中的测试用例,应该输出:
false
true
false
false
请分析代码逻辑,并推测划线处的代码。
答案写在“解答.txt”文件中
注意:只写划线处应该填的内容,划线前后的内容不要抄写。
import java.util.*;
publicclassDemo06 {
publicstaticboolean isGoodBracket(String s) {
Stack a = new Stack();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ‘(’)
a.push(‘)’);
if (c == ‘[’)
a.push(‘]’);
if (c == ‘{’)
a.push(‘}’);
if (c == ‘)’ || c == ‘]’ || c == ‘}’) {
if (a.size()==0)
returnfalse; // 填空
if (a.pop() != c)
returnfalse;
}
}
if (a.size()!=0)
returnfalse; // 填空
returntrue;
}
publicstaticvoid main(String[] args) {
System.out.println(isGoodBracket(“…(…[.)…].{.(…).}…”));
System.out.println(isGoodBracket(“…(…[…].(.).).{.(…).}…”));
System.out.println(isGoodBracket(“…[…].(.).){.(…).}…”));
System.out.println(isGoodBracket(“…(…[…].(.).){.(…)…”));
}
}
运行结果:
false
true
false
false

7.扑克牌排列

下面代码模拟了一套扑克牌(初始排序A~K,共13张)的操作过程。
操作过程是:
手里拿着这套扑克牌,从前面拿一张放在后面,再从前面拿一张放桌子上,再从前面拿一张放在后面,…
如此循环操作,直到剩下最后一张牌也放在桌子上。
下面代码的目的就是为了求出最后桌上的牌的顺序。
初始的排列如果是A,2,3…K,则最后桌上的顺序为:
[2, 4, 6, 8, 10, Q, A, 5, 9, K, 7, 3, J]
请分析代码逻辑,并推测划线处的代码。
答案写在“解答.txt”文件中
注意:只写划线处应该填的内容,划线前后的内容不要抄写。
package Question1_9;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
public class Question7 {
public static List moveCard(List src) {
if (src == null)
return null;
List dst = new Vector();
for (;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/484277
推荐阅读
相关标签