PAT乙级 1003 我要通过! (20分)

“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。 得到“答案正确”的条件是: 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符; 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串; 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。 现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。 输入格式: 每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。 输出格式: 每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出 YES,否则输出 NO。 输入样例: 8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA 输出样例: YES YES YES YES NO NO NO NO 思路 可以找到这样的规律:正确的字符串应该遵循 P 左侧的 A 个数乘 P 与 T 中间的 A 个数等于 T 右侧的 A 个数。且 P 和 T 的个数必须为1,中间 A 的个数不能为 0,除了 P,A,T 的其他的字符个数必须为 0。 #include <cstdio> #include <cstring> const int maxn = 101; int main() { int n, len; scanf("%d", &n); char str[maxn]; for (int i = 0; i < n; i++) { scanf("%s", str); len = strlen(str); int PNum = 0, TNum = 0, otherNum = 0, PIndex, TIndex; for (int j = 0; j < len; j++) { if (str[j] == 'P') { PNum++; PIndex = j; } else if (str[j] == 'T') { TNum++; TIndex = j; } else if (str[j] != 'A') { otherNum++; } } if (PNum != 1 || TNum != 1 || otherNum != 0 || (TIndex - PIndex) <= 1) { printf("NO\n"); continue; } int leftA = PIndex, midA = TIndex - PIndex - 1, rightA = len - TIndex - 1; if (leftA * midA == rightA) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }

March 30, 2020 · 2 min

PAT甲级 1104 Sum of Number Segments (20分)

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) and (0.4). Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0. ...

March 30, 2020 · 2 min

PAT甲级 1101 Quick Sort (25分)

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition? For example, given N=5 and the numbers 1, 3, 2, 4, and 5. We have: ...

March 29, 2020 · 3 min

PAT甲级 1010 Radix (25分)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number. Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given. Input Specification: Each input file contains one test case. Each case occupies a line which contains 4 positive integers: ...

March 29, 2020 · 3 min

PAT甲级 1085 Perfect Sequence (25分)

Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively. Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence. Input Specification: Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤10^5) is the number of integers in the sequence, and p (≤ 10^9 ) is the parameter. In the second line there are N positive integers, each is no greater than 10^9. ...

March 28, 2020 · 2 min

PAT甲级 1038 Recover the Smallest Number (30分)

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87. Input Specification: Each input file contains one test case. Each case gives a positive integer N (≤10^4) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space. ...

March 28, 2020 · 2 min

PAT甲级 1067 Sort with Swap(0, i) (25分)

Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way: Swap(0, 1) => {4, 1, 2, 0, 3} Swap(0, 3) => {4, 1, 2, 3, 0} Swap(0, 4) => {0, 1, 2, 3, 4} Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers. ...

March 27, 2020 · 2 min

PAT乙级 1033 旧键盘打字 (20分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 10^5 个字符的串。可用的字符包括字母 [a-z, A-Z]、数字 0-9、以及下划线 _(代表空格)、,、.、-、+(代表上档键)。题目保证第 2 行输入的文字串非空。 注意:如果上档键坏掉了,那么大写的英文字母无法被打出。 输出格式: 在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。 输入样例: 7+IE. 7_This_is_a_test. 输出样例: _hs_s_a_tst 注意点 读入失效键位的时候要注意,可能没有失效的键位,即第一行为空行的情况。这时候不能用 scanf 输入,而要用 cin.getline(str, maxn) 输入字符串。 #include <iostream> #include <cstring> using namespace std; const int maxn = 100001; int main() { int hash_table[256] = {0}; char wrong[257], input[maxn]; cin.getline(wrong, 257); int len = strlen(wrong); for (int i = 0; i < len; i++) { if (wrong[i] >= 'A' && wrong[i] <= 'Z') { wrong[i] = wrong[i] - 'A' + 'a'; } hash_table[wrong[i]] = 1; } scanf("%s", input); len = strlen(input); for (int i = 0; i < len; i++) { if (input[i] >= 'A' && input[i] <= 'Z') { int low = input[i] - 'A' + 'a'; if (hash_table[low] == 0 && hash_table['+'] == 0) { printf("%c", input[i]); } } else if (hash_table[input[i]] == 0) { printf("%c", input[i]); } } printf("\n"); return 0; }

March 26, 2020 · 1 min

Dubbo微服务影院系列(6):Dubbo服务开发(影片模块开发)

章节概要 掌握 API 网关服务聚合功能的实现 掌握 Mybatis-plus 自定义 SQL 实现 掌握 Dubbo 异步调用 服务聚合 服务聚合就是将多个服务调用封装 服务聚合可以简化前端调用方式 服务聚合提供更好的安全性、可扩展性 业务功能开发流程 根据接口文档思考功能列表 实现 API 接口和实体 服务提供者实现 影片模块创建 从 guns-user 拷贝一份重命名为 guns-film,修改相关配置 API 网关功能聚合 优点 六个接口,一次请求,同一时刻节省了5次HTTP请求 同一个接口对外暴露,降低了前后端分离开发的难度和复杂度 缺点: 一次获取数据过多,容易出现问题 如 gateway 中的影片模块的获取首页信息接口,将 filmServiceApi 的多个方法所获得的对象聚合为 FilmIndexVO 对象返回给前端,将原本需要的多个接口聚合为一个 /film/getIndex 接口,避免了前端对不同接口的多次调用。 @RequestMapping(value = "getIndex", method = RequestMethod.GET) public ResponseVO getIndex() { FilmIndexVO filmIndexVO = new FilmIndexVO(); // 获取banner信息 filmIndexVO.setBanners(filmServiceApi.getBanners()); // 获取热映的影片 filmIndexVO.setHotFilms(filmServiceApi.getHotFilms(true, 99, 99, 99, 99, 1, 8)); // 获取即将上映的影片 filmIndexVO.setSoonFilms(filmServiceApi.getSoonFilms(true, 99, 99, 99, 99, 1, 8)); // 获取票房排行榜 filmIndexVO.setBoxRanking(filmServiceApi.getBoxRanking()); // 获取人气榜单 filmIndexVO.setExpectRanking(filmServiceApi.getExpectRanking()); // 获取排行前100影片 filmIndexVO.setTop100(filmServiceApi.getTop()); return ResponseVO.success(IMG_PRE, filmIndexVO); } Mybatis-plus 自定义 SQL 实现 在相应的 Mapper 接口中添加方法 ...

March 22, 2020 · 3 min

PAT甲级 1033 To Fill or Not to Fill (25分)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go. Input Specification: Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space. ...

March 22, 2020 · 3 min