2019年 第10届 蓝桥杯 Java B组 省赛真题详解及总结


  • 注意:部分代码及程序 源自 蓝桥杯 官网视频(历年真题解析) 郑未老师
  1. 2013年 第04届 蓝桥杯 Java B组 省赛真题详解及小结
  2. 2014年 第05届 蓝桥杯 Java B组 省赛真题详解及小结
  3. 2015年 第06届 蓝桥杯 Java B组 省赛真题详解及小结
  4. 2016年 第07届 蓝桥杯 Java B组 省赛真题详解及小结
  5. 2017年 第08届 蓝桥杯 Java B组 省赛真题详解及小结
  6. 2018年 第09届 蓝桥杯 Java B组 省赛真题详解及小结
  7. 2019年 第10届 蓝桥杯 Java B组 省赛真题详解及小结
  8. 2020年 第11届 蓝桥杯 Java B组 第1次模拟赛真题详解及小结(校内模拟)
  9. 2020年 第11届 蓝桥杯 Java B组 第2次模拟赛真题详解及小结
  10. 2020年 第11届 蓝桥杯 C/C++ B组 省赛真题详解及小结【第1场省赛 2020.7.5】【Java版】
  11. 2020年 第11届 蓝桥杯 Java B组 省赛真题详解及小结【第1场省赛 2020.7.5】
  12. 2020年 第11届 蓝桥杯 Java C组 省赛真题详解及小结【第1场省赛 2020.7.5】

目   录

01-试题 A: 组队

02-试题 B: 不同子串

解法一

解法二

03-试题 C: 数列求值

04-试题 D: 数的分解

解法一

解法二

解法三

05-试题 E: 迷宫

06-试题 F: 特别数的和

解法一

解法二

解法三

07-试题 G: 外卖店优先级

08-试题 H: 人物相关性分析

H人物相关性分析_split分析

H人物相关性分析

H人物相关性分析2

H人物相关性分析3

09-试题 I: 后缀表达式

10-试题 J: 灵能传输


本文代码及解题思路来源于:第十届蓝桥题目讲解 Java B组 A-J题

    

01-试题 A: 组队

本题总分:5 分

【问题描述】

作为篮球队教练,你需要从以下名单中选出 1 号位至 5 号位各一名球员,组成球队的首发阵容。

每位球员担任 1 号位至 5 号位时的评分如下表所示。请你计算首发阵容 1 号位至 5 号位的评分之和最大可能是多少?

(如果你把以上文字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 team.txt,内容与上面表格中的相同,请注意第一列是编号)

【答案提交】

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:490

Excel -> 数据 -> 分列
MAX() -> SUM()

每一列的最大值 相加 -> 计算器

492错误

不能是同一个人!!!

490 

package provincialGames_10_2019;

import java.util.Scanner;

public class A组队 {  //490

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			int[][] team = new int[20][5];
			for (int i = 0; i < 20; i++) {
				for (int j = 0; j < 5; j++) {
					team[i][j] = input.nextInt();
				}
			}
			int maxSum = 0;
			for (int i = 0; i < 20; i++)
				for (int j = 0; j < 20; j++)
					for (int k = 0; k < 20; k++)
						for (int h = 0; h < 20; h++)
							for (int g = 0; g < 20; g++)
								if ((i != j && i != k && i != h && i != g) && (j != k && j != h && j != g) && (k != h && k != g) && h != g) {
									int max = team[i][0] + team[j][1] + team[k][2] + team[h][3] + team[g][4];
									if (max > maxSum)
										maxSum = max;
								}
			System.out.println(maxSum);

		} catch (Exception e) {
			input.close();
		}
	}
}
/*
 
 测试用例
 * 97 90 0 0 0 92 85 96 0 0 0 0 0 0 93 0 0 0 80 86 89 83 97 0 0 82 86 0 0 0 0 0
 * 0 87 90 0 97 96 0 0 0 0 89 0 0 95 99 0 0 0 0 0 96 97 0 0 0 0 93 98 94 91 0 0
 * 0 0 83 87 0 0 0 0 98 97 98 0 0 0 93 86 98 83 99 98 81 93 87 92 96 98 0 0 0 89
 * 92 0 99 96 95 81
*/

02-试题 B: 不同子串

本题总分:5 分

【问题描述】

一个字符串的非空子串是指字符串中长度至少为 1 的连续的一段字符组成的串。例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab, aaab,一共 7 个。注意在计算时,只算本质不同的串的个数。

请问,字符串0100110001010001 有多少个不同的非空子串?

【答案提交】

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:100

解法一

package provincialGames_10_2019;

import java.util.HashSet;

public class B不同字串 {	 //100
	public static void main(String[] args) {
        HashSet set = new HashSet();
		String str = "0100110001010001";
		int N = str.length();  //16
        while(N > 0){  //循环16次    N: 16 -> 1
            int dis = str.length() - N + 1;  //dis: 1 -> 16
            for(int i = 0; i < N; i++){
                String ss = str.substring(i, i + dis);  //字串长度: 1 -> 16
                set.add(ss);
            }
            N--;
        }
		System.out.println(set.size());
	}
}
/**

C++方法:
for(int i = 0; i < str.length(); i++) {
	for(int j = i; j <= str.length(); j++) {
		set.add( str.substr(i, j - i + 1) );
	}
}

substring(int beginIndex):返回一个新字符串,它是此字符串的一个子字符串。
该子字符串始于指定索引处的字符,一直到此字符串末尾。
"unhappy".substring(2) -> "happy"

substring(int beginIndex, int endIndex):返回一个新字符串, 它是此字符串的一个子字符串。
该子字符串从指定的 beginIndex 处开始, endIndex:到指定的 endIndex-1处结束。
[beginIndex, endIndex - 1]
*/

解法二

package provincialGames_10_2019;

import java.util.HashSet;
import java.util.Set;

public class B不同字串2 {

	public static void main(String[] args) {
        String target = "0100110001010001";
        Set<String> sub = new HashSet<String>();
        for (int step = 0; step < target.length(); step++) {
            for (int beginIndex = 0, endIndex = 1 + step; endIndex <= target.length(); beginIndex++, endIndex++) {
                sub.add(target.substring(beginIndex, endIndex));
            }
        }
        System.out.println(sub.size());
    }
}
/*审题发现要求是不同的非空子串,则想到Set集合去重,String.substring()方法求子串(一切	为快速解题为前提),
 * 然后我们发现它的子串规律为一开始子串长度为1,然后在为2,……,最后为原字符串,
 * 这就好	比切豆腐,一开始要求切成每刀间隔为1豆腐块,每次移动距离为1,后来要求切成每刀间隔为2豆腐块,
 * 每次移动距离	为1,……,直至为整个大豆腐的大小。
*/

03-试题 C: 数列求值

本题总分:10 分

【问题描述】

给定数列 1, 1, 1, 3, 5, 9, 17, …,从第 4 项开始,每项都是前 3 项的和。求第 20190324 项的最后 4 位数字。

【答案提交】

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个 4 位整数(提示:答案的千位不为 0),在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:4659

package provincialGames_10_2019;

public class C数列求值 {  //4659

	public static void main(String[] args) {
//		int a[] = new int[20190324];
//		a[0] = a[1] = a[2] = 1;
//		for(int i = 3; i < 20190324; i++) {
//			a[i] = (a[i-1] + a[i-2] + a[i-3]) % 10000;
//		}
//		System.out.println(a[20190323]);



		int arr[] = new int[20190325];
        arr[1] = arr[2] = arr[3] = 1;
        for(int i = 4; i <= 20190324; i++){
            arr[i] = (arr[i-1] + arr[i-2] + arr[i-3]);  // % 10000
            arr[i] %= 10000;
        }
        System.out.println(arr[20190324]);



//		int a = 1, b = 1, c = 1;
//		int n = 20190324;
//		while(--n > 0) {
//			int d = (a + b + c) % 10000;
//			a = b;
//			b = c;
//			c = d;
//		}
//		System.out.println(a);



//		int a = 1, b = 1, c = 1;
//		for (int i = 4; i <= 20190324; i++) {
//			int d = (a + b + c) % 10000;  //巧用模运算
//			a = b;
//			b = c;
//			c = d;
//		}  //  20190324%3 == 0 , 6730108*3 == 20190324
//		System.out.println(c);
		
	}

}
/*
此题类似于斐波那契数列,但是所求20190324项的最后四位数字,要是单纯按照斐波那契数列的思想求下去,
别说long类型,BigInteger类型都存不了这么大的数,然后我们发现,所求20190324项的最后四位数字
(也就是变相的告诉我们运算过程只和每个数的后四位有关系),那么我们只需要保留每次运算结果的后四位就OK了,这样绝对不会溢出。
*/

04-试题 D: 数的分解

本题总分:10 分

【问题描述】

把 2019 分解成 3 个各不相同的正整数之和,并且要求每个正整数都不包含数字 2 和 4,一共有多少种不同的分解方法?

注意交换 3 个整数的顺序被视为同一种方法,例如 1000+1001+18 和 1001+1000+18 被视为同一种。

【答案提交】

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:40785

解法一

package provincialGames_10_2019;

public class D数的分解2 {  //40785
	
	public static void main(String[] args) {
		int n = 2019;
		int result = 0;
		for(int i = 1; i <= 2019; i++) {
			for(int j = i + 1; j <= n && n - i - j > j; j++) {
				int k = n - i - j;
				if(!check(i) && !check(j) && !check(k)) {
					result++;
				}
			}
		}
		System.out.println(result);
	}
	
	public static boolean check(int number) {
		while(number > 0) {
			int t = number % 10;
			if(t == 2 || t == 4) {
				return true;
			}
			number /= 10;
		}
		return false;
	}
}
/***

p(1):不定顺序:要除以6.

p(2):人为规定:a <= b <= c
 */

解法二

package provincialGames_10_2019;

public class D数的分解3 {

	public static void main(String[] args) {
        int n = 2019;
        int num = 0;
        for (int i = 1; i < n; i++) {
            if (String.valueOf(i).indexOf("2") != -1 || String.valueOf(i).indexOf("4") != -1)
                continue;
            for (int j = i + 1; j < n; j++) {
                if (String.valueOf(j).indexOf("2") != -1 || String.valueOf(j).indexOf("4") != -1)
                    continue;
                int k = n - i - j;
                if (i == k || j == k || i == j)
                    continue;
                if (k > 0 && String.valueOf(k).indexOf("2") == -1 && String.valueOf(k).indexOf("4") == -1) {
                    num++;
                }
            }
        }
        System.out.println(num / 3);        
    }

}

解法三

package provincialGames_10_2019;

public class D数的分解4 {

	public static void main(String[] args) {
		int x = 2019;// 2019/3 == 673
        int sum = 0;
        String aa;
        for (int i1 = 1; i1 <= 673; i1++) {
            aa = String.valueOf(i1);
            if (aa.contains("2") || aa.contains("4"))
                continue;
            for (int i2 = i1 + 1; i2 < (2019 - i1 + 1) / 2; i2++) {
                aa = String.valueOf(i2);
                if (aa.contains("2") || aa.contains("4"))
                    continue;
                aa = String.valueOf(2019 - i1 - i2);
                if (aa.contains("2") || aa.contains("4"))
                    continue;
                sum++;
            }
        }
        System.out.println(sum);
	}

}

05-试题 E: 迷宫

本题总分:15 分

【问题描述】

下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。

010000

000100

001001

110000

迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这个它的上、下、左、右四个方向之一。

对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫,一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。

对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。再试题目录下有一个 maze.txt,内容与下面的文本相同)

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

【答案提交】

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个字符串,包含四种字母 D、U、L、R,在提交答案时只填写这个字符串,填写多余的内容将无法得分。

package provincialGames_10_2019;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class E迷宫 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			String s = "01010101001011001001010110010110100100001000101010"
					+ "00001000100000101010010000100000001001100110100101"
					+ "01111011010010001000001101001011100011000000010000"
					+ "01000000001010100011010000101000001010101011001011"
					+ "00011111000000101000010010100010100000101100000000"
					+ "11001000110101000010101100011010011010101011110111"
					+ "00011011010101001001001010000001000101001110000000"
					+ "10100000101000100110101010111110011000010000111010"
					+ "00111000001010100001100010000001000101001100001001"
					+ "11000110100001110010001001010101010101010001101000"
					+ "00010000100100000101001010101110100010101010000101"
					+ "11100100101001001000010000010101010100100100010100"
					+ "00000010000000101011001111010001100000101010100011"
					+ "10101010011100001000011000010110011110110100001000"
					+ "10101010100001101010100101000010100000111011101001"
					+ "10000000101100010000101100101101001011100000000100"
					+ "10101001000000010100100001000100000100011110101001"
					+ "00101001010101101001010100011010101101110000110101"
					+ "11001010000100001100000010100101000001000111000010"
					+ "00001000110000110101101000000100101001001000011101"
					+ "10100101000101000000001110110010110101101010100001"
					+ "00101000010000110101010000100010001001000100010101"
					+ "10100001000110010001000010101001010101011111010010"
					+ "00000100101000000110010100101001000001000000000010"
					+ "11010000001001110111001001000011101001011011101000"
					+ "00000110100010001000100000001000011101000000110011"
					+ "10101000101000100010001111100010101001010000001000"
					+ "10000010100101001010110000000100101010001011101000"
					+ "00111100001000010000000110111000000001000000001011"
					+ "10000001100111010111010001000110111010101101111000";
			int[][] labyrinth = new int[30][50];
			for (int i = 0; i < 30; i++) {
				for (int j = 0; j < 50; j++) {
					labyrinth[i][j] = s.charAt(50 * i + j) - '0';
				}
			}
			System.out.println(BFS(labyrinth, 30, 50));
		} catch (Exception e) {
			input.close();
		}
	}

	public static String BFS(int[][] labyrinth, int row, int column) {
		int[][] stepArr = { { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 0 } };
		String[] direction = { "U", "R", "L","D"}; 
		int[][] visit = new int[row][column];  // 标记是否已经访问过
		StringBuilder sb = new StringBuilder();
		Node node = new Node(0, 0, -1, -1, 0, null);
		Queue<Node> queue = new LinkedList<Node>();
		Stack<Node> stack = new Stack<Node>();
		queue.offer(node);
		while (!queue.isEmpty()) {
			Node head = queue.poll();
			stack.push(head); // 用于回溯路径
			visit[head.x][head.y] = 1;
			for (int i = 0; i < 4; i++) {
				int x = head.x + stepArr[i][0];
				int y = head.y + stepArr[i][1];
				String d = direction[i];
				// exit
				if (x == row - 1 && y == column - 1 && labyrinth[x][y] == 0 && visit[x][y] == 0) {
					// 打印路径
					Node top = stack.pop();
					sb.append(d);
					sb.append(top.direction);
					int preX = top.preX;
					int preY = top.preY;
					while (!stack.isEmpty()) {
						top = stack.pop();
						if (preX == top.x && preY == top.y) {
							if (top.direction != null)
								sb.append(top.direction);
							preX = top.preX;
							preY = top.preY;
						}

					}
					return sb.reverse().toString();
				}
				// bfs
				if (x >= 0 && x < row && y >= 0 && y < column && labyrinth[x][y] == 0 && visit[x][y] == 0) {
					Node newNode = new Node(x, y, head.x, head.y, head.step + 1, d);
					queue.offer(newNode);
				}
			}
		}
		return null;
	}
}

class Node {
	int x, y;
	int step;
	int preX, preY;
	String direction;

	Node(int x, int y, int preX, int preY, int step, String direction) {
		this.x = x;
		this.y = y;
		this.preX = preX;
		this.preY = preY;
		this.step = step;
		this.direction = direction;
	}

}

06-试题 F: 特别数的和

时间限制: 1.0s 内存限制: 512.0MB 本题总分:15 分

【问题描述】

小明对数位中含有 2、0、1、9 的数字很感兴趣(不包括前导 0),在 1 到 40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。

请问,在 1 到 n 中,所有这样的数的和是多少?

【输入格式】

输入一行包含两个整数 n。

【输出格式】

输出一行,包含一个整数,表示满足条件的数的和。

【样例输入】

40

【样例输出】

574

【评测用例规模与约定】

对于 20% 的评测用例,1 ≤ n ≤ 10。

对于 50% 的评测用例,1 ≤ n ≤ 100。

对于 80% 的评测用例,1 ≤ n ≤ 1000。

对于所有评测用例,1 ≤ n ≤ 10000。

解法一

package provincialGames_10_2019;

import java.util.Scanner;

public class F特别数的和 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int sum = 0;
		for(int i = 1; i <= n; i++) {
			if( 1 <= i && i <= 9 ) {
				if( f(i) == true ) {
					sum += i;
				}
			}else if( 10 <= i && i <= 99 ) {  //12
				int a = i / 10;
				int b = i % 10;
				if( f(a) == true || f(b) == true ) {
					sum += i;
				}
			}else if( 100 <= i && i <= 999 ) {  //123
				int a = i/100;
				int b = i/10%10;
				int c = i%10;
				if( f(a) == true || f(b) == true || f(c) == true) {
					sum += i;
				}
			}else if( 1000 <= i && i <= 10000 ) {  //1234 10000
				int a = i/1000;
				int b = i/100%10;
				int c = i/10%10;
				int d = i%10;
				if( f(a) == true || f(b) == true || f(c) == true || f(d) == true) {
					sum += i;
				}
			}
		}
		System.out.println(sum);
	}
	public static boolean f(int x) {
		if( x ==0 || x == 1 || x == 2 || x ==9) {
			return true;
		}
		return false;
	}
}

解法二

package provincialGames_10_2019;

import java.util.Scanner;

public class F特别数的和2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            if ( check(i) ) {
            	sum += i;
            }
        }
        System.out.println(sum);
        sc.close();
	}
	
	public static boolean check(int number) {
		while(number > 0) {
			int t = number % 10;
			if(t == 2 || t == 0 || t == 1 || t == 9) {
				return true;
			}
			number /= 10;
		}
		return false;
	}
}

解法三

package provincialGames_10_2019;

import java.util.Scanner;

public class F特别数的和3 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            String s = String.valueOf(i);
            if (s.contains("2") || s.contains("0") || s.contains("1") || s.contains("9"))
                sum += i;
        }
        System.out.println(sum);
        sc.close();
	}
	
}
/*
public static void main(String[] args) {
	int n = 2019;
	int num = 0;
	for (int i = 1; i < n; i++) {
		if ((i + "").indexOf("2") != -1 || (i + "").indexOf("4") != -1)
			continue;
		for (int j = i + 1; j < n; j++) {
			if ((j + "").indexOf("2") != -1 || (j + "").indexOf("4") != -1)
				continue;
			int k = n - i - j;
			if (i == k || j == k || i == j)
				continue;
			if (k > 0 && (k + "").indexOf("2") == -1 && (k + "").indexOf("4") == -1)
				num++;
		}
	}
	System.out.println(num / 3);
}


public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	try {
		int n = input.nextInt();
		int sum = 0;
		for (int i = 1; i <= n; i++) {
			String target = Integer.toString(i);
			if (target.indexOf("2") != -1 || target.indexOf("0") != -1 || target.indexOf("1") != -1
					|| target.indexOf("9") != -1) {
				sum += i;
			}
		}
		System.out.println(sum);
	} catch (Exception e) {
		input.close();
	}
}
*/

07-试题 G: 外卖店优先级

时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分

【问题描述】

“饱了么”外卖系统中维护着 N 家外卖店,编号 1 ∼ N。每家外卖店都有 一个优先级,初始时 (0 时刻) 优先级都为 0。

每经过 1 个时间单位,如果外卖店没有订单,则优先级会减少 1,最低减 到 0;而如果外卖店有订单,则优先级不减反加,每有一单优先级加 2。

如果某家外卖店某时刻优先级大于 5,则会被系统加入优先缓存中;如果 优先级小于等于 3,则会被清除出优先缓存。

给定 T 时刻以内的 M 条订单信息,请你计算 T 时刻时有多少外卖店在优先缓存中。

【输入格式】

第一行包含 3 个整数 N、M 和 T。

以下 M 行每行包含两个整数 ts 和 id,表示 ts 时刻编号 id 的外卖店收到 一个订单。

【输出格式】

输出一个整数代表答案。

【样例输入】

2 6 6

1 1

5 2

3 1

6 2

2 1

6 2

【样例输出】

1

【样例解释】

6 时刻时,1 号店优先级降到 3,被移除出优先缓存;2 号店优先级升到 6,加入优先缓存。所以是有 1 家店 (2 号) 在优先缓存中。

【评测用例规模与约定】

对于 80% 的评测用例,1 ≤ N, M, T ≤ 10000。

对于所有评测用例,1 ≤ N, M, T ≤ 100000,1 ≤ ts ≤ T,1 ≤ id ≤ N。

package provincialGames_10_2019;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class G外卖店优先级 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			Set <Integer> set = new HashSet<Integer>();
			int N = input.nextInt();  //N 家外卖店,编�? 1 �? N
			int M = input.nextInt();  //给定 T 时刻以内�? M 条订单信�?
			int T = input.nextInt();
			int[][] orders = new int[M][2];  //给定 T 时刻以内�? M 条订单信�?
			for (int i = 0; i < M; i++) {
				for (int j = 0; j < 2; j++) {
					orders[i][j] = input.nextInt();
				}
			}
			
			int[] priority = new int[N];
			int[] sign = new int[N];
			for (int i = 1; i <= T; i++) {
				for (int j = 0; j < M; j++) {
					if (orders[j][0] == i) {
						priority[orders[j][1] - 1] += 2;
						if (priority[orders[j][1] - 1] > 5 && !set.contains(orders[j][1] - 1)) {
							set.add(orders[j][1] - 1);
						}
						sign[orders[j][1] - 1] = 1;
					}
				}
				for (int j = 0; j < N; j++) {
					if (sign[j] == 0 && priority[j] > 0)
						priority[j]--;
					if (priority[j] <= 3) {
						set.remove(j);
					}
				}
				sign = new int[N];
			}
			System.out.println(set.size());
		} catch (Exception e) {
			input.close();
		}
	}
}

08-试题 H: 人物相关性分析

时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分

【问题描述】

小明正在分析一本小说中的人物相关性。他想知道在小说中 Alice 和 Bob 有多少次同时出现。

更准确的说,小明定义 Alice 和 Bob“同时出现”的意思是:在小说文本 中 Alice 和 Bob 之间不超过 K 个字符。

例如以下文本:

This is a story about Alice and Bob. Alice wants to send a private message to Bob.

假设 K = 20,则 Alice 和 Bob 同时出现了 2 次,分别是”Alice and Bob” 和”Bob. Alice”。前者 Alice 和 Bob 之间有 5 个字符,后者有 2 个字符。

注意:

1. Alice 和 Bob 是大小写敏感的,alice 或 bob 等并不计算在内。

2. Alice 和 Bob 应为单独的单词,前后可以有标点符号和空格,但是不能有字母。例如 Bobbi 並不算出现了 Bob。

【输入格式】

第一行包含一个整数 K。

第二行包含一行字符串,只包含大小写字母、标点符号和空格。长度不超过 1000000。

【输出格式】

输出一个整数,表示 Alice 和 Bob 同时出现的次数。

【样例输入】

20

This is a story about Alice and Bob. Alice wants to send a private message to Bob.

【样例输出】

2

【评测用例规模与约定】

对于所有评测用例,1 ≤ K ≤ 1000000。

H人物相关性分析_split分析

package provincialGames_10_2019;

import java.util.Scanner;
//This is a story about Alice and Bob. Alice wants to send a private message to Bob.
public class H人物相关性分析_split分析 {

	public static void main(String[] args) {
//		Scanner sc = new Scanner(System.in);
//		String str = sc.nextLine();
//		
//		String[] words = str.split("\\s+|\\.");  //字符串分割, 按照空格和.分割字符, 若是(.空格)分割后为空字符串。
//		for(int i = 0; i < words.length; i++) {
//			System.out.println(words[i]);
//		}
//		System.out.println("words:" + words.length);
//		
//		
//		String[] words2 = str.split("\\s");
//		for(int i = 0; i < words2.length; i++) {
//			System.out.println(words2[i]);
//		}
//		System.out.println("words2:" + words2.length);
//		
//		
//		String[] words3 = str.split("\\s+");
//		for(int i = 0; i < words3.length; i++) {
//			System.out.println(words3[i]);
//		}
//		System.out.println("words3:" + words3.length);
//		
//		
//		//String[] words2 = str.split(".");  //无效!!!
//		String[] words4 = str.split("\\.");
//		for(int i = 0; i < words4.length; i++) {
//			System.out.println(words4[i]);
//		}
//		System.out.println("words4:" + words4.length);
		
		
		String stra = "a--bb--ccc--dddd--eeeee";
		for(String x: stra.split("--", -1)) {
			System.out.println("-1:" + x);
		}
		for(String x: stra.split("--", 0)) {
			System.out.println("0:" + x);
		}
		for(String x: stra.split("--", 1)) {
			System.out.println("1:" + x);
		}
		for(String x: stra.split("--", 2)) {
			System.out.println("2:" + x);
		}
		for(String x: stra.split("--", 3)) {
			System.out.println("3:" + x);
		}
		
//		String address = "上海^上海市@闵行区#吴中路";
//		String[] splitAddress = address.split("\\^|@|#");
//		for(String x: splitAddress) {
//			System.out.println(x);
//		}
		
		
		
	}

}
/**
int indexOf(String str):返回指定字符串的索引位置

https://www.cnblogs.com/xiaoxiaohui2015/p/5838674.html
特殊情况有 * ^ : | . \
(1):split表达式,其实就是一个正则表达式。
 *  ^ | 等符号在正则表达式中属于一种有特殊含义的字符,如果使用此种字符作为分隔符,必须使用转义符即\\加以转义。
(2):如果使用多个分隔符则需要借助 | 符号,如二所示,但需要转义符的仍然要加上分隔符进行处理

对某些特殊字符,如果字符(串)正好是正则的一部分,则需要转义才能使用,

这些字符有 | , + , * , ^ , $ , / , | , [ , ] , ( , ) , - , . , \等, 
因它们是正则表达式中的一部分, 所以如果想用该字符本身, 这些字符需要进行转义才能表示它本身;


\\s表示 空格,回车,换行等空白符,
split("\\s+") 能实现 多个空格切割的效果
+号表示一个或多个的意思
*/

H人物相关性分析

package provincialGames_10_2019;

import java.util.Scanner;
//This is a story about Alice and Bob. Alice wants to send a private message to Bob.
public class H人物相关性分析 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			int K = input.nextInt();
			input.nextLine();
			String text = input.nextLine();
			
			String[] words = text.split("\\s+|\\.");  //字符串分割,按照空格和.分割字符,若是(.空格)分割后为空字符串。\\s  \\s+  \\.
			//String[] words = text.split("\\s|\\.");  加不加“+” , 效果相同
//			for(String x: words) {
//				System.out.println(x);
//			}
//			System.out.println(words.length);
			
			int[] wordsLength = new int[words.length];  //将分割的字符串的长度值存储,避免三重循环中调用String.length();
			for (int i = 0; i < words.length; i++) {
				wordsLength[i] = words[i].length();
			}
			
			int num = 0;
			
			/**
			
			i记录Alice的位置
			
			从Alice(i)的位置 开始往后找, 寻找Bob的位置
			
			j记录Bob的位置
			
			This is a story about Alice and Bob. Alice wants to send a private message to Bob.
			i == 5, j == 7
			
			用k记录i(Alice)与j(Bob)之间的单词个数
			
			int sum = 1;
			记录首个空格
			
			sum += wordsLength[k] + 1;
			每个单词与其后的一个空格, 组为一对。
			
			累加求解。
			
			*/
			
			//Alice ——> Bob的距离
			for (int i = 0; i < words.length; i++) {
				if (words[i].equals("Alice")) {
					for (int j = i + 1; j < words.length; j++) {
						int sum = 1;
						if (words[j].equals("Bob")) {
							for (int k = i + 1; k < j; k++) {
								//每个单词的长度加空格占据的长度
								sum += wordsLength[k] + 1;
							}
							if (sum <= K) {
								num++;
							}
						}
					}
				}
			}
			//Bob ——> Alice的距离
			for (int i = 0; i < words.length; i++) {
				if (words[i].equals("Bob")) {
					for (int j = i + 1; j < words.length; j++) {
						int sum = 1;
						if (words[j].equals("Alice")) {
							for (int k = i + 1; k < j; k++) {
								//每个单词的长度加空格占据的长度
								sum += wordsLength[k] + 1;
							}
							if (sum <= K) {
								num++;
							}
						}
					}
				}
			}
			System.out.println(num);
		} catch (Exception e) {
			input.close();
		}
	}

}
/**int indexOf(String str):返回指定字符串的索引位置

\\s表示 空格,回车,换行等空白符,
split("\\s+") 能实现 多个空格切割的效果
+号表示一个或多个的意思

String str = "ww-ll-ee-aa-bbbb-ccc";
String a = "-";
int b = 2;
for (String retval: str.split(a, b)){
	System.out.println(retval);
}
第二个参数就是,需要切割的份数.
例:
b <= 0(默认等于0) 切分后: String[] split =["ww", "ll", "ee", "aa", "bbbb", "ccc"]
b=1 --> 切分后: String[] split =["ww-ll-ee-aa-bbbb-ccc"]
b=2 --> 切分后: String[] split =["ww","ll-ee-aa-bbbb-ccc"]
b=3 --> 切分后: String[] split =["ww", "ll", "ee-aa-bbbb-ccc"]

*/

H人物相关性分析2

package provincialGames_10_2019;

import java.util.Scanner;
import java.util.Vector;
//This is a story about Alice and Bob. Alice wants to send a private message to Bob.
public class H人物相关性分析2 {

	static boolean jiancha(char C){
		if ((C>='a'&&C<='z')||(C>='A'&&C<='Z')) {
			return false;
		}
		return true;
	}
	public static void main(String[] args) {
		Vector<Integer> vectora = new Vector<Integer>();
		Vector<Integer> vectorb = new Vector<Integer>();
		int n;
		String string;
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		string = scanner.nextLine();
		string = scanner.nextLine();
		
		for (int i = 0; i+5 <= string.length(); i++) {
			if ((i==0 || jiancha(string.charAt(i-1)) == true) && (i+5==string.length() || jiancha(string.charAt(i+5)) == true)) {
				if (string.substring(i, i+5).equals("Alice")) {
					vectora.add(i);
				}
			}
		}
		for (int i = 0; i+3 <= string.length(); i++) {
			if ((i==0 || jiancha(string.charAt(i-1)) == true) && (i+3==string.length() || jiancha(string.charAt(i+3)) == true)) {
				if (string.substring(i, i+3).equals("Bob")) {
					vectorb.add(i);
				}
			}
		}
		int ans = 0;
		for (int i = 0; i < vectora.size(); i++) {
			int l = 0, r = -1;
			while(r+1 < vectorb.size() && vectora.get(i) > vectorb.get(r+1)  ) {
				r++;
			}
			while (l<=r && vectora.get(i) > vectorb.get(l)+n+3  ) {
				l++;
			}
			ans += r-l+1;
		}
		
		for (int i = 0; i < vectorb.size(); i++) {
			int l = 0, r = -1;
			while(r+1 < vectora.size() && vectorb.get(i) > vectora.get(r+1)  ) {
				r++;
			}
			while (l<=r && vectorb.get(i) >vectora.get(l)+n+5  ) {
				l++;
			}
			ans += r-l+1;
		}
		System.out.println(ans);
	
	}
}

H人物相关性分析3

package provincialGames_10_2019;

import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.text.DefaultEditorKit.InsertBreakAction;
//This is a story about Alice and Bob. Alice wants to send a private message to Bob.
public class H人物相关性分析3{
    static List<String> list = new ArrayList();//用来保存分割后的字符串
    static List<Integer> art = new ArrayList<>();//记录.出现的位置 
    static int number = 0;
    public static void print(){
        for(String b:list){
            System.out.println(b);
        }
    }
    
    public static void fun(String string)
    {
        int count = 0;
        char crt[] = string.toCharArray();//将字符串转为字符数组
        for(char b:crt)
        {
            if(b == ' ')
                count++;
            else if(b == '.')
            {
                art.add(count);
                count++;
            }
        }
    }
    
    public static void insert()//将'.'插入到list中。
    {
        int sum = 0;
        for(int i:art)
        {
            sum++;
            list.add(i+sum,".");//将指定的元素添加到指定的位置
        }
        
    }
    
    public static void lon(List<String> list2,int k)//判断两个之间长度是否小于k
    {
        int lonK = 0;
        for(String str1:list2)
        {
            lonK += str1.length();
        }
        if(lonK<=k)
        {
            number++;
        }
        
    }
    
 public static void ifmanzu(int k)//判断是否满组条件的个数
    {
        //list.sublList(0,3) //将字符串的0到3,取出来
        int begin = 0;
        List<String> list1 = list.subList(begin, list.size());
        while(true)
        {
            
            //System.out.println(list1);
            int a1 = list1.indexOf("Alice");//找出列表中Allice 第一次出现的位置
            int b1 = list1.indexOf("Bob");//找到列表中Bob第一次出现的位置
            if(a1==-1||b1==-1)//没有同时出现的了
            {
                break;
            }
            
            
            if(a1<b1)//表示a1在前面
            {
                lon(list1.subList(a1+1, b1), k);
                list1.remove(a1);//移除
            }
            else
            {
                lon(list1.subList(b1+1, a1), k);
                list1.remove(b1);
            }
            
        }    
        
    }

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        Scanner scanner1 = new Scanner(System.in);
        String string = scanner1.nextLine();
        
        fun(string);
        //String [] a = string.split("\\s+");//按空格截取
        String [] a = string.split("[ \\.]");
        for(String b:a)
        {
            list.add(b);
        }
        
        insert();//将'.'插入到list中
        
        ifmanzu(k);
        System.out.println(number);
        //System.out.println(art);
        
    }

}

09-试题 I: 后缀表达式

时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分

【问题描述】

给定 N 个加号、M 个减号以及 N + M + 1 个整数 A1, A2, · · · , AN+M+1,小明想知道在所有由这 N 个加号、M 个减号以及 N + M + 1 个整数凑出的合法的后缀表达式中,结果最大的是哪一个?

请你输出这个最大的结果。

例如使用1 2 3 + -,则 “2 3 + 1 -” 这个后缀表达式结果是 4,是最大的。

【输入格式】

第一行包含两个整数 N 和 M。

第二行包含 N + M + 1 个整数 A1, A2, · · · , AN+M+1。

【输出格式】

输出一个整数,代表答案。

【样例输入】

1 1

1 2 3

【样例输出】

4

【评测用例规模与约定】

对于所有评测用例,0 ≤ N, M ≤ 100000,−109 ≤ Ai ≤ 109。

package provincialGames_10_2019;

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

public class I后缀表达式 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			int add = input.nextInt();
			int reduce = input.nextInt();
			int totalLength = add + reduce + 1;
			int[] number = new int[totalLength];
			for (int i = 0; i < totalLength; i++) {
				number[i] = input.nextInt();
			}
			int sum = 0;
			if (reduce == 0) {
				for (int i = 0; i < totalLength; i++) {
					sum += number[i];
				}
			}
			if (add == 0) {
				Arrays.sort(number);
				if (number[0] < 0) {
					for (int i = 0; i <= reduce; i++) {
						if (number[i] > 0) 
							sum += number[i];
						else
							sum -= number[i];
					}
				} else {
					for (int i = 1; i <= reduce; i++) {
							sum += number[i];
					}
					sum -= number[0];
				}
			}
			if (add != 0 && reduce != 0) {
				int reduceNum = 0;
				for (int i = 0; i < totalLength; i++) {
					if (number[i] < 0) {
						reduceNum++;
					}
				}
				if (reduce >= reduceNum) {
					Arrays.sort(number);
					int temp = reduce;
					for (int i = 0; i < reduceNum; i++) {
						number[i] = -number[i];
						temp--;
					}
					Arrays.sort(number);
					for (int i = totalLength - 1; i >= temp; i--) {
						sum += number[i];
					}
					for (int i = temp - 1; i >= 0; i--) {
						sum -= number[i];
					}
				} else {
					Arrays.sort(number);
					sum += number[totalLength - 1];
					for (int i = 0; i < totalLength - 1; i++) {
						if (number[i] > 0)
							sum += number[i];
						else
							sum -= number[i];
					}
				}
			}
			System.out.println(sum);
		} catch (Exception e) {
			input.close();
		}
	}

}
/**
 * 1.如果只有+号,没有-号,则遍历数组累加即可;
 * 2.如果只有-号,没有+号,首先从小到大排序,然后分两种情况考虑:
 * (1).最小值是负数(也就是含有负数),例如[-2, -1, 3, 4, 5],四个减号,运算过程为5 - (-1) - (-2 - 3 - 4) = 5 + 1 - (-9)
 *  = 5 + 1 + 9 = 15,也就是说只要含有负数,负数转正数,全部相加即可
 * (2).最小值是正数(全部是正数),例如[1, 2, 3],两个减号,运算过程为3 - (1 - 2) = 3 + 2 - 1,也就是说运算规则为除了
 *         最小值以外的正数相加减去最小值 
 * 3.如果有+号,有-号,则讨论减号的个数与负数的个数,分两种情况讨论(实际分为三种):
 *( 1).减号个数大于等于负数个数(则将负数变正数,每一个负数变正数的过程中, 减号的数量需要减一,然后排序,遍历数组从大到小累加,
 * 直至剩下的数字个数和减号数量相同,然后再减去这些剩下的数字);
 * (2).减号个数小于负数个数,这个时候我们就应该使用+号,消除负数(比如[2, -5 , -6, + , -],运算过程为2 - ((-5) + (-6)) = 
 * 2 + 11 = 13),我们可以再分情况讨论:
 * (2.1).全是负数,如[-1, -2, -3, -4, -5],其中一个加号三个减号,运算过程为(-1 - ((-4) + (-5)) - (-3) - (-2) = -1 + 9 
 * + 3 + 2),则运算规律为首先排序选择其中的最大值,加上其他数字的绝对值就行(可以自行继续证明)。(2.2).有正数,有负数,
 * [-1, 19, 17, -4, -5],其中两个加号两个减号,则运算过程为(19 + 17 - ((-4) + (-5)) - (-1) = 19 + 17 + 9 + 1),则运算
 * 规律为首先排序选择其中的最大值,加上其他数字的绝对值就行(可以自行继续证明)。所有情况讨论完毕。
————————————————
版权声明:本文为CSDN博主「malimingwq」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/malimingwq/article/details/88953526
*/

10-试题 J: 灵能传输

时间限制: 5.0s 内存限制: 512.0MB 本题总分:25 分

【题目背景】

在游戏《星际争霸 II》中,高阶圣堂武士作为星灵的重要 AOE 单位,在游戏的中后期发挥着重要的作用,其技能”灵能风暴“可以消耗大量的灵能对一片区域内的敌军造成毁灭性的伤害。经常用于对抗人类的生化部队和虫族的刺蛇飞龙等低血量单位。

【问题描述】

你控制着 n 名高阶圣堂武士,方便起见标为 1, 2, · · · , n。每名高阶圣堂武士需要一定的灵能来战斗,每个人有一个灵能值 ai 表示其拥有的灵能的多少(ai 非负表示这名高阶圣堂武士比在最佳状态下多余了 ai 点灵能,ai 为负则表示这名高阶圣堂武士还需要 −ai 点灵能才能到达最佳战斗状态)。现在系统赋予了你的高阶圣堂武士一个能力,传递灵能,每次你可以选择一个 i ∈ [2, n − 1],若 ai ≥ 0 则其两旁的高阶圣堂武士,也就是 i − 1、i + 1 这两名高阶圣堂武士会从 i 这名高阶圣堂武士这里各抽取 ai 点灵能;若 ai < 0 则其两旁的高阶圣堂武士,也就是 i − 1, i + 1 这两名高阶圣堂武士会给 i 这名高阶圣堂武士 −ai 点灵能。形式化来讲就是 ai−1+ = ai , ai+1+ = ai , ai− = 2ai。

灵能是非常高效的作战工具,同时也非常危险且不稳定,一位高阶圣堂武士拥有的灵能过多或者过少都不好,定义一组高阶圣堂武士的不稳定度为 maxn i=1|ai |,请你通过不限次数的传递灵能操作使得你控制的这一组高阶圣堂武 士的不稳定度最小。

【输入格式】

本题包含多组询问。输入的第一行包含一个正整数 T 表示询问组数。

接下来依次输入每一组询问。

每组询问的第一行包含一个正整数 n,表示高阶圣堂武士的数量。

接下来一行包含 n 个数 a1, a2, · · · , an。

【输出格式】

输出 T 行。每行一个整数依次表示每组询问的答案。

【样例输入】

3

3

5 -2 3

4

0 0 0 0

3

1 2 3

【样例输出】

3

0

3

【样例说明】

对于第一组询问:

对 2 号高阶圣堂武士进行传输操作后 a1 = 3,a2 = 2,a3 = 1。答案为 3。

对于第二组询问:

这一组高阶圣堂武士拥有的灵能都正好可以让他们达到最佳战斗状态。

【样例输入】

3

4

-1 -2 -3 7

4

2 3 4 -8

5

-1 -1 6 -1 -1

【样例输出】

5

7

4

【样例输入】

见文件trans3.in。

【样例输出】

见文件trans3.ans。

【数据规模与约定】

对于所有评测用例,T ≤ 3,3 ≤ n ≤ 300000,|ai | ≤ 109。

评测时将使用 25 个评测用例测试你的程序,每个评测用例的限制如下:

                       

注意:本题输入量较大请使用快速的读入方式。

trans3.in:

3
5
6 -4 2 -7 3
10
-99 -53 43 80 -83 72 99 78 -63 -9
100
373837389 225627048 -847064399 487662607 579717002 903937892 -89313283 134706789 259978604 399131737 298183518 62083619 -444218530 403702220 358088455 -973959249 -637339048 -736509394 -552801709 -98262597 -532577703 -393599463 762744971 -683270041 716127816 -991756495 734780346 27919355 -421469435 258728334 844409214 -270792553 -490888330 133696186 843888283 -35439761 -73481392 -118968548 269164182 978558860 522378250 -979427259 -330256906 235192566 -652699569 -708569352 -778693386 241745676 583226906 121065292 -503683097 599394257 405122877 437067802 238539735 -957745973 -843677563 -690555937 908484805 940157941 524765035 730436972 -17856720 -530595388 -727773574 617781285 491720304 -779040285 -298295760 -699402143 230749576 404009775 126806094 -140842651 198136484 681875881 997449600 898972467 -239590302 -62193410 866009412 -401154712 -276085482 593177187 -236793216 487533624 75511548 -446699920 -869912037 -330666015 268937148 -430325605 -635949275 361887555 -855294881 87004526 782523543 -69083645 -965396597 -880697065 

trans3.ans:

5
88
381470940

https://blog.csdn.net/kajweb/article/details/89005516

猜你喜欢

转载自blog.csdn.net/weixin_44949135/article/details/108150820