mirror of
https://github.com/xlucn/PAT.git
synced 2026-10-03 00:23:15 +08:00
add a10{42,44,48,50,51}.md
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
---
|
||||
layout: post
|
||||
date: 2020-04-23 10:31:04 +0800
|
||||
title: "PAT Advanced 1042. Shuffling Machine (20) (C语言实现)"
|
||||
categories: Advanced
|
||||
tags: []
|
||||
permalink: Advanced/1042.html
|
||||
---
|
||||
|
||||
## 题目
|
||||
|
||||
{% include_relative html/a1042.md %}
|
||||
|
||||
## 思路
|
||||
|
||||
{% include_relative analysis/a1042.md %}
|
||||
|
||||
## 代码
|
||||
|
||||
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1042.c),欢迎交流
|
||||
|
||||
```c
|
||||
{% raw %}#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int count, order[54], deck[54], pos;
|
||||
char types[] = "SHCDJ";
|
||||
|
||||
scanf("%d", &count);
|
||||
for(int i = 0; i < 54; i++)
|
||||
scanf("%d", order + i);
|
||||
|
||||
for(int i = 0; i < 54; i++)
|
||||
{
|
||||
pos = i;
|
||||
for(int j = 0; j < count; j++)
|
||||
pos = order[pos] - 1; /* the input order[] was 1 based */
|
||||
deck[pos] = i;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 54; i++)
|
||||
printf("%c%d%c", types[deck[i] / 13],
|
||||
deck[i] % 13 + 1,
|
||||
i == 53 ? '\0' : ' ');
|
||||
|
||||
return 0;
|
||||
}
|
||||
{% endraw %}```
|
||||
@@ -0,0 +1,63 @@
|
||||
---
|
||||
layout: post
|
||||
date: 2020-04-25 01:46:01 +0800
|
||||
title: "PAT Advanced 1044. Shopping in Mars (25) (C语言实现)"
|
||||
categories: Advanced
|
||||
tags: [subsequence]
|
||||
permalink: Advanced/1044.html
|
||||
---
|
||||
|
||||
## 题目
|
||||
|
||||
{% include_relative html/a1044.md %}
|
||||
|
||||
## 思路
|
||||
|
||||
{% include_relative analysis/a1044.md %}
|
||||
|
||||
## 代码
|
||||
|
||||
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1044.c),欢迎交流
|
||||
|
||||
```c
|
||||
{% raw %}#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int N, M, D[100000] = {0}, pair[100000][2] = {{0}};
|
||||
|
||||
scanf("%d %d", &N, &M);
|
||||
for(int n = 0; n < N; n++)
|
||||
scanf("%d", D + n);
|
||||
|
||||
int i = 0, j = 0, sum = D[0], min = 100000000, count = 0;
|
||||
while(1)
|
||||
{
|
||||
if(sum <= min && sum >= M)
|
||||
{
|
||||
/* reset if found lower solution */
|
||||
if(sum < min)
|
||||
{
|
||||
min = sum;
|
||||
count = 0;
|
||||
}
|
||||
/* record */
|
||||
pair[count][0] = i;
|
||||
pair[count][1] = j;
|
||||
count ++;
|
||||
}
|
||||
/* Find closest solution */
|
||||
if(sum <= M && j < N - 1)
|
||||
sum += D[++j];
|
||||
else if(i < N - 1)
|
||||
sum -= D[i++];
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
for(int n = 0; n < count; n++)
|
||||
printf("%d-%d\n", pair[n][0] + 1, pair[n][1] + 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
{% endraw %}```
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
layout: post
|
||||
date: 2020-04-25 20:00:38 +0800
|
||||
title: "PAT Advanced 1048. Find Coins (25) (C语言实现)"
|
||||
categories: Advanced
|
||||
tags: []
|
||||
permalink: Advanced/1048.html
|
||||
---
|
||||
|
||||
## 题目
|
||||
|
||||
{% include_relative html/a1048.md %}
|
||||
|
||||
## 思路
|
||||
|
||||
{% include_relative analysis/a1048.md %}
|
||||
|
||||
## 代码
|
||||
|
||||
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1048.c),欢迎交流
|
||||
|
||||
```c
|
||||
{% raw %}#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int N, M, counts[501] = {0}, coin;
|
||||
|
||||
scanf("%d %d", &N, &M);
|
||||
for(int i = 0; i < N; i++)
|
||||
{
|
||||
scanf("%d", &coin);
|
||||
counts[coin]++;
|
||||
}
|
||||
|
||||
for(int i = 1; 2 * i - 1 < M; i++)
|
||||
{
|
||||
if((i * 2 == M && counts[i] > 1)
|
||||
|| (i * 2 != M && M - i < 501 && counts[i] && counts[M - i]))
|
||||
{
|
||||
printf("%d %d", i, M - i);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("No Solution");
|
||||
return 0;
|
||||
}
|
||||
{% endraw %}```
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
layout: post
|
||||
date: 2020-04-25 23:58:47 +0800
|
||||
title: "PAT Advanced 1050. String Subtraction (20) (C语言实现)"
|
||||
categories: Advanced
|
||||
tags: [string processing]
|
||||
permalink: Advanced/1050.html
|
||||
---
|
||||
|
||||
## 题目
|
||||
|
||||
{% include_relative html/a1050.md %}
|
||||
|
||||
## 思路
|
||||
|
||||
{% include_relative analysis/a1050.md %}
|
||||
|
||||
## 代码
|
||||
|
||||
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1050.c),欢迎交流
|
||||
|
||||
```c
|
||||
{% raw %}#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char c, s1[10001];
|
||||
int s2[128] = {0};
|
||||
|
||||
scanf("%[^\n]%c", s1, &c);
|
||||
while((c = getchar()) != '\n')
|
||||
s2[(int)c] = 1;
|
||||
|
||||
for(char *p = s1; *p; p++)
|
||||
if(!s2[(int)(*p)])
|
||||
putchar(*p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
{% endraw %}```
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
layout: post
|
||||
date: 2020-04-26 13:31:23 +0800
|
||||
title: "PAT Advanced 1051. Pop Sequence (25) (C语言实现)"
|
||||
categories: Advanced
|
||||
tags: []
|
||||
permalink: Advanced/1051.html
|
||||
---
|
||||
|
||||
## 题目
|
||||
|
||||
{% include_relative html/a1051.md %}
|
||||
|
||||
## 思路
|
||||
|
||||
{% include_relative analysis/a1051.md %}
|
||||
|
||||
## 代码
|
||||
|
||||
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1051.c),欢迎交流
|
||||
|
||||
```c
|
||||
{% raw %}#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int M, N, K, stack[7] = {0}, top, pushed, popped, wanted;
|
||||
|
||||
scanf("%d %d %d", &M, &N, &K);
|
||||
for(int i = 0; i < K; i++)
|
||||
{
|
||||
top = -1;
|
||||
pushed = 0;
|
||||
popped = 0;
|
||||
/* push */
|
||||
stack[++top] = ++pushed;
|
||||
scanf("%d", &wanted);
|
||||
for( ; ; )
|
||||
{
|
||||
if(top != -1 && stack[top] == wanted)
|
||||
{
|
||||
/* pop */
|
||||
stack[top--] = 0;
|
||||
popped++;
|
||||
/* read */
|
||||
if(popped < N)
|
||||
{
|
||||
scanf("%d", &wanted);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
/* push */
|
||||
if(pushed < N && top < M - 1)
|
||||
stack[++top] = ++pushed;
|
||||
else
|
||||
break;
|
||||
}
|
||||
printf("%s\n", popped < N ? "NO" : "YES");
|
||||
while(getchar() != '\n') ;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
{% endraw %}```
|
||||
@@ -0,0 +1,22 @@
|
||||
<!--
|
||||
2020-04-23 10:31:04 +0800
|
||||
|
||||
-->
|
||||
|
||||
实现随机洗牌。
|
||||
|
||||
核心就是一个不断的映射:
|
||||
|
||||
- 位置1 = 打乱顺序(原始位置)
|
||||
- 位置2 = 打乱顺序(位置1)
|
||||
- 位置3 = 打乱顺序(位置2)
|
||||
- ... ...
|
||||
- 位置n = 打乱顺序(位置n-1)
|
||||
|
||||
我的实现里用了比较简化的代码,主要有两点:
|
||||
- 针对每张牌一次性进行多次位置变换,即
|
||||
- 最终位置 = 打乱(打乱(...打乱(原始位置)...))
|
||||
- 使用一点数学计算花色,而不是创建一个字符串数组。这点只是我有洁癖,代码的可读性有点损失
|
||||
- 打乱的时候变的是0到53的数字
|
||||
- 数字/13是花色,依次为`SHCDJ`
|
||||
- 数字%13是大小
|
||||
@@ -0,0 +1,14 @@
|
||||
<!--
|
||||
2020-04-25 01:46:01 +0800
|
||||
subsequence
|
||||
-->
|
||||
|
||||
这是一个子数列问题,具体要得到子数列和尽量接近某个值的所有可能情况。
|
||||
|
||||
用不太严谨的数学语言说就是要:
|
||||
|
||||
$$ \{(i,j)\vert\sum_{n=i}^{j}D_n=min\{\sum_{n=m}^{l}{D_n}\vert\sum_{n=m}^{l}{D_n}\ge M\}\} $$
|
||||
|
||||
类似地题目参考b1030和a1007两题,遍历部分是可以达到$\mathcal{O}(N)$的时间复杂度。
|
||||
|
||||
本题则需要额外记录目前能达到的超过`M`的最小子列和,以及相应的所有子数列。如果按照我的方法,则需要一个和输入数列几乎一样大的数列来记录子数列的首尾。
|
||||
@@ -0,0 +1,8 @@
|
||||
<!--
|
||||
2020-04-25 20:00:38 +0800
|
||||
|
||||
-->
|
||||
|
||||
可能是最简单的甲级题目之一了。
|
||||
|
||||
从诸多面值的硬币中找到正好凑齐交付金额的两枚硬币。鉴于面额的范围(1-500)很小,可以开一个数组记录不同面额硬币的数量,比较方便。
|
||||
@@ -0,0 +1,10 @@
|
||||
<!--
|
||||
2020-04-25 23:58:47 +0800
|
||||
string processing
|
||||
-->
|
||||
|
||||
这道题和乙级一些题很类似,如b1033。就是要去除`s1`中所有在`s2`中出现的字符。
|
||||
|
||||
虽然题目给的`s2`可能会很大,依旧只需统计某个字符*是否*出现,用`int[128]`的数组即可。
|
||||
|
||||
题目提到的“做到快并不简单”说的好像不是针对题目的测试点?可能说的是一般应用情景下的问题。测试点做到很快是很简单的。
|
||||
@@ -0,0 +1,20 @@
|
||||
<!--
|
||||
2020-04-26 13:31:23 +0800
|
||||
|
||||
-->
|
||||
|
||||
验证一个序列是否可以用堆栈生成。
|
||||
|
||||
我的方法是模拟堆栈的操作,看能否实现所给序列的输出。这是比较直观的思路,并且效率也可以,不知道有没有其它的思路。
|
||||
|
||||
(目前不是很满意代码的实现,感觉逻辑比较罗嗦,有重复代码,不知能不能简化)
|
||||
|
||||
一个极其粗糙的伪代码(细致的需要十几行,直接看后面真代码吧)就是:
|
||||
|
||||
```
|
||||
直至(得到所给序列)或者(无法再入栈):
|
||||
如果栈顶与所需数字相同:
|
||||
出栈,读下一个所需数字
|
||||
否则入栈
|
||||
根据出栈数量判断是否成功
|
||||
```
|
||||
@@ -0,0 +1,112 @@
|
||||
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1042.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
|
||||
|
||||
## 题目
|
||||
|
||||
Shuffling is a procedure used to randomize a deck of playing cards. Because
|
||||
standard shuffling techniques are seen as weak, and in order to avoid "inside
|
||||
jobs" where employees collaborate with gamblers by performing inadequate
|
||||
shuffles, many casinos employ **automatic shuffling machines**. Your task is
|
||||
to simulate a shuffling machine.
|
||||
|
||||
The machine shuffles a deck of 54 cards according to a given random order and
|
||||
repeats for a given number of times. It is assumed that the initial status of
|
||||
a card deck is in the following order:
|
||||
|
||||
|
||||
|
||||
S1, S2, ..., S13,
|
||||
H1, H2, ..., H13,
|
||||
C1, C2, ..., C13,
|
||||
D1, D2, ..., D13,
|
||||
J1, J2
|
||||
|
||||
|
||||
where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for
|
||||
"Diamond", and "J" for "Joker". A given order is a permutation of distinct
|
||||
integers in [1, 54]. If the number at the $i$ -th position is $j$ , it means
|
||||
to move the card from position $i$ to position $j$ . For example, suppose we
|
||||
only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5,
|
||||
3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the
|
||||
shuffling again, the result will be: C1, H5, S3, J2, D13.
|
||||
|
||||
### Input Specification:
|
||||
|
||||
Each input file contains one test case. For each case, the first line contains
|
||||
a positive integer $K$ ( $\le 20$ ) which is the number of repeat times. Then
|
||||
the next line contains the given order. All the numbers in a line are
|
||||
separated by a space.
|
||||
|
||||
### Output Specification:
|
||||
|
||||
For each test case, print the shuffling results in one line. All the cards are
|
||||
separated by a space, and there must be no extra space at the end of the line.
|
||||
|
||||
### Sample Input:
|
||||
|
||||
|
||||
|
||||
2
|
||||
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47
|
||||
|
||||
|
||||
### Sample Output:
|
||||
|
||||
|
||||
|
||||
S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5
|
||||
|
||||
|
||||
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
实现随机洗牌。
|
||||
|
||||
核心就是一个不断的映射:
|
||||
|
||||
- 位置1 = 打乱顺序(原始位置)
|
||||
- 位置2 = 打乱顺序(位置1)
|
||||
- 位置3 = 打乱顺序(位置2)
|
||||
- ... ...
|
||||
- 位置n = 打乱顺序(位置n-1)
|
||||
|
||||
我的实现里用了比较简化的代码,主要有两点:
|
||||
- 针对每张牌一次性进行多次位置变换,即
|
||||
- 最终位置 = 打乱(打乱(...打乱(原始位置)...))
|
||||
- 使用一点数学计算花色,而不是创建一个字符串数组。这点只是我有洁癖,代码的可读性有点损失
|
||||
- 打乱的时候变的是0到53的数字
|
||||
- 数字/13是花色,依次为`SHCDJ`
|
||||
- 数字%13是大小
|
||||
|
||||
## 代码
|
||||
|
||||
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1042.c),欢迎交流
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int count, order[54], deck[54], pos;
|
||||
char types[] = "SHCDJ";
|
||||
|
||||
scanf("%d", &count);
|
||||
for(int i = 0; i < 54; i++)
|
||||
scanf("%d", order + i);
|
||||
|
||||
for(int i = 0; i < 54; i++)
|
||||
{
|
||||
pos = i;
|
||||
for(int j = 0; j < count; j++)
|
||||
pos = order[pos] - 1; /* the input order[] was 1 based */
|
||||
deck[pos] = i;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 54; i++)
|
||||
printf("%c%d%c", types[deck[i] / 13],
|
||||
deck[i] % 13 + 1,
|
||||
i == 53 ? '\0' : ' ');
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,138 @@
|
||||
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1044.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
|
||||
|
||||
## 题目
|
||||
|
||||
Shopping in Mars is quite a different experience. The Mars people pay by
|
||||
chained diamonds. Each diamond has a value (in Mars dollars M$). When making
|
||||
the payment, the chain can be cut at any position for only once and some of
|
||||
the diamonds are taken off the chain one by one. Once a diamond is off the
|
||||
chain, it cannot be taken back. For example, if we have a chain of 8 diamonds
|
||||
with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3
|
||||
options:
|
||||
|
||||
1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
|
||||
2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
|
||||
3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).
|
||||
|
||||
Now given the chain of diamond values and the amount that a customer has to
|
||||
pay, you are supposed to list all the paying options for the customer.
|
||||
|
||||
If it is impossible to pay the exact amount, you must suggest solutions with
|
||||
minimum lost.
|
||||
|
||||
### Input Specification:
|
||||
|
||||
Each input file contains one test case. For each case, the first line contains
|
||||
2 numbers: $N$ ( $\le 10^5$ ), the total number of diamonds on the chain, and
|
||||
$M$ ( $\le 10^8$ ), the amount that the customer has to pay. Then the next
|
||||
line contains $N$ positive numbers $D_1 \cdots D_N$ ( $D_i\le 10^3$ for all
|
||||
$i=1, \cdots , N$ ) which are the values of the diamonds. All the numbers in a
|
||||
line are separated by a space.
|
||||
|
||||
### Output Specification:
|
||||
|
||||
For each test case, print `i-j` in a line for each pair of `i` $\le$ `j` such
|
||||
that $D$ `i` \+ ... + $D$ `j` = $M$ . Note that if there are more than one
|
||||
solution, all the solutions must be printed in increasing order of `i`.
|
||||
|
||||
If there is no solution, output `i-j` for pairs of `i` $\le$ `j` such that $D$
|
||||
`i` \+ ... + $D$ `j` $> M$ with ( $D$ `i` \+ ... + $D$ `j` $- M$ ) minimized.
|
||||
Again all the solutions must be printed in increasing order of `i`.
|
||||
|
||||
It is guaranteed that the total value of diamonds is sufficient to pay the
|
||||
given amount.
|
||||
|
||||
### Sample Input 1:
|
||||
|
||||
|
||||
|
||||
16 15
|
||||
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
|
||||
|
||||
|
||||
### Sample Output 1:
|
||||
|
||||
|
||||
|
||||
1-5
|
||||
4-6
|
||||
7-8
|
||||
11-11
|
||||
|
||||
|
||||
### Sample Input 2:
|
||||
|
||||
|
||||
|
||||
5 13
|
||||
2 4 5 7 9
|
||||
|
||||
|
||||
### Sample Output 2:
|
||||
|
||||
|
||||
|
||||
2-4
|
||||
4-5
|
||||
|
||||
|
||||
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
这是一个子数列问题,具体要得到子数列和尽量接近某个值的所有可能情况。
|
||||
|
||||
用不太严谨的数学语言说就是要:
|
||||
|
||||
$$ \{(i,j)\vert\sum_{n=i}^{j}D_n=min\{\sum_{n=m}^{l}{D_n}\vert\sum_{n=m}^{l}{D_n}\ge M\}\} $$
|
||||
|
||||
类似地题目参考b1030和a1007两题,遍历部分是可以达到$\mathcal{O}(N)$的时间复杂度。
|
||||
|
||||
本题则需要额外记录目前能达到的超过`M`的最小子列和,以及相应的所有子数列。如果按照我的方法,则需要一个和输入数列几乎一样大的数列来记录子数列的首尾。
|
||||
|
||||
## 代码
|
||||
|
||||
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1044.c),欢迎交流
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int N, M, D[100000] = {0}, pair[100000][2] = {{0}};
|
||||
|
||||
scanf("%d %d", &N, &M);
|
||||
for(int n = 0; n < N; n++)
|
||||
scanf("%d", D + n);
|
||||
|
||||
int i = 0, j = 0, sum = D[0], min = 100000000, count = 0;
|
||||
while(1)
|
||||
{
|
||||
if(sum <= min && sum >= M)
|
||||
{
|
||||
/* reset if found lower solution */
|
||||
if(sum < min)
|
||||
{
|
||||
min = sum;
|
||||
count = 0;
|
||||
}
|
||||
/* record */
|
||||
pair[count][0] = i;
|
||||
pair[count][1] = j;
|
||||
count ++;
|
||||
}
|
||||
/* Find closest solution */
|
||||
if(sum <= M && j < N - 1)
|
||||
sum += D[++j];
|
||||
else if(i < N - 1)
|
||||
sum -= D[i++];
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
for(int n = 0; n < count; n++)
|
||||
printf("%d-%d\n", pair[n][0] + 1, pair[n][1] + 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,97 @@
|
||||
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1048.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
|
||||
|
||||
## 题目
|
||||
|
||||
Eva loves to collect coins from all over the universe, including some other
|
||||
planets like Mars. One day she visited a universal shopping mall which could
|
||||
accept all kinds of coins as payments. However, there was a special
|
||||
requirement of the payment: for each bill, she could only use exactly two
|
||||
coins to pay the exact amount. Since she has as many as $10^5$ coins with her,
|
||||
she definitely needs your help. You are supposed to tell her, for any given
|
||||
amount of money, whether or not she can find two coins to pay for it.
|
||||
|
||||
### Input Specification:
|
||||
|
||||
Each input file contains one test case. For each case, the first line contains
|
||||
2 positive numbers: $N$ ( $\le 10^5$ , the total number of coins) and $M$ (
|
||||
$\le 10^3$ , the amount of money Eva has to pay). The second line contains $N$
|
||||
face values of the coins, which are all positive numbers no more than 500. All
|
||||
the numbers in a line are separated by a space.
|
||||
|
||||
### Output Specification:
|
||||
|
||||
For each test case, print in one line the two face values $V_1$ and $V_2$
|
||||
(separated by a space) such that $V_1 + V_2 = M$ and $V_1 \le V_2$ . If such a
|
||||
solution is not unique, output the one with the smallest $V_1$ . If there is
|
||||
no solution, output `No Solution` instead.
|
||||
|
||||
### Sample Input 1:
|
||||
|
||||
|
||||
|
||||
8 15
|
||||
1 2 8 7 2 4 11 15
|
||||
|
||||
|
||||
### Sample Output 1:
|
||||
|
||||
|
||||
|
||||
4 11
|
||||
|
||||
|
||||
### Sample Input 2:
|
||||
|
||||
|
||||
|
||||
7 14
|
||||
1 8 7 2 4 11 15
|
||||
|
||||
|
||||
### Sample Output 2:
|
||||
|
||||
|
||||
|
||||
No Solution
|
||||
|
||||
|
||||
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
可能是最简单的甲级题目之一了。
|
||||
|
||||
从诸多面值的硬币中找到正好凑齐交付金额的两枚硬币。鉴于面额的范围(1-500)很小,可以开一个数组记录不同面额硬币的数量,比较方便。
|
||||
|
||||
## 代码
|
||||
|
||||
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1048.c),欢迎交流
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int N, M, counts[501] = {0}, coin;
|
||||
|
||||
scanf("%d %d", &N, &M);
|
||||
for(int i = 0; i < N; i++)
|
||||
{
|
||||
scanf("%d", &coin);
|
||||
counts[coin]++;
|
||||
}
|
||||
|
||||
for(int i = 1; 2 * i - 1 < M; i++)
|
||||
{
|
||||
if((i * 2 == M && counts[i] > 1)
|
||||
|| (i * 2 != M && M - i < 501 && counts[i] && counts[M - i]))
|
||||
{
|
||||
printf("%d %d", i, M - i);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("No Solution");
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,69 @@
|
||||
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1050.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
|
||||
|
||||
## 题目
|
||||
|
||||
Given two strings $S_1$ and $S_2$ , $S = S_1 - S_2$ is defined to be the
|
||||
remaining string after taking all the characters in $S_2$ from $S_1$ . Your
|
||||
task is simply to calculate $S_1 - S_2$ for any given strings. However, it
|
||||
might not be that simple to do it **fast**.
|
||||
|
||||
### Input Specification:
|
||||
|
||||
Each input file contains one test case. Each case consists of two lines which
|
||||
gives $S_1$ and $S_2$ , respectively. The string lengths of both strings are
|
||||
no more than $10^4$ . It is guaranteed that all the characters are visible
|
||||
ASCII codes and white space, and a new line character signals the end of a
|
||||
string.
|
||||
|
||||
### Output Specification:
|
||||
|
||||
For each test case, print $S_1 - S_2$ in one line.
|
||||
|
||||
### Sample Input:
|
||||
|
||||
|
||||
|
||||
They are students.
|
||||
aeiou
|
||||
|
||||
|
||||
### Sample Output:
|
||||
|
||||
|
||||
|
||||
Thy r stdnts.
|
||||
|
||||
|
||||
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
这道题和乙级一些题很类似,如b1033。就是要去除`s1`中所有在`s2`中出现的字符。
|
||||
|
||||
虽然题目给的`s2`可能会很大,依旧只需统计某个字符*是否*出现,用`int[128]`的数组即可。
|
||||
|
||||
题目提到的“做到快并不简单”说的好像不是针对题目的测试点?可能说的是一般应用情景下的问题。测试点做到很快是很简单的。
|
||||
|
||||
## 代码
|
||||
|
||||
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1050.c),欢迎交流
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char c, s1[10001];
|
||||
int s2[128] = {0};
|
||||
|
||||
scanf("%[^\n]%c", s1, &c);
|
||||
while((c = getchar()) != '\n')
|
||||
s2[(int)c] = 1;
|
||||
|
||||
for(char *p = s1; *p; p++)
|
||||
if(!s2[(int)(*p)])
|
||||
putchar(*p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,115 @@
|
||||
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1051.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
|
||||
|
||||
## 题目
|
||||
|
||||
Given a stack which can keep $M$ numbers at most. Push $N$ numbers in the
|
||||
order of 1, 2, 3, ..., $N$ and pop randomly. You are supposed to tell if a
|
||||
given sequence of numbers is a possible pop sequence of the stack. For
|
||||
example, if $M$ is 5 and $N$ is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the
|
||||
stack, but not 3, 2, 1, 7, 5, 6, 4.
|
||||
|
||||
### Input Specification:
|
||||
|
||||
Each input file contains one test case. For each case, the first line contains
|
||||
3 numbers (all no more than 1000): $M$ (the maximum capacity of the stack),
|
||||
$N$ (the length of push sequence), and $K$ (the number of pop sequences to be
|
||||
checked). Then $K$ lines follow, each contains a pop sequence of $N$ numbers.
|
||||
All the numbers in a line are separated by a space.
|
||||
|
||||
### Output Specification:
|
||||
|
||||
For each pop sequence, print in one line "YES" if it is indeed a possible pop
|
||||
sequence of the stack, or "NO" if not.
|
||||
|
||||
### Sample Input:
|
||||
|
||||
|
||||
|
||||
5 7 5
|
||||
1 2 3 4 5 6 7
|
||||
3 2 1 7 5 6 4
|
||||
7 6 5 4 3 2 1
|
||||
5 6 4 3 7 2 1
|
||||
1 7 6 5 4 3 2
|
||||
|
||||
|
||||
### Sample Output:
|
||||
|
||||
|
||||
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
|
||||
|
||||
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
验证一个序列是否可以用堆栈生成。
|
||||
|
||||
我的方法是模拟堆栈的操作,看能否实现所给序列的输出。这是比较直观的思路,并且效率也可以,不知道有没有其它的思路。
|
||||
|
||||
(目前不是很满意代码的实现,感觉逻辑比较罗嗦,有重复代码,不知能不能简化)
|
||||
|
||||
一个极其粗糙的伪代码(细致的需要十几行,直接看后面真代码吧)就是:
|
||||
|
||||
```
|
||||
直至(得到所给序列)或者(无法再入栈):
|
||||
如果栈顶与所需数字相同:
|
||||
出栈,读下一个所需数字
|
||||
否则入栈
|
||||
根据出栈数量判断是否成功
|
||||
```
|
||||
|
||||
## 代码
|
||||
|
||||
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1051.c),欢迎交流
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int M, N, K, stack[7] = {0}, top, pushed, popped, wanted;
|
||||
|
||||
scanf("%d %d %d", &M, &N, &K);
|
||||
for(int i = 0; i < K; i++)
|
||||
{
|
||||
top = -1;
|
||||
pushed = 0;
|
||||
popped = 0;
|
||||
/* push */
|
||||
stack[++top] = ++pushed;
|
||||
scanf("%d", &wanted);
|
||||
for( ; ; )
|
||||
{
|
||||
if(top != -1 && stack[top] == wanted)
|
||||
{
|
||||
/* pop */
|
||||
stack[top--] = 0;
|
||||
popped++;
|
||||
/* read */
|
||||
if(popped < N)
|
||||
{
|
||||
scanf("%d", &wanted);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
/* push */
|
||||
if(pushed < N && top < M - 1)
|
||||
stack[++top] = ++pushed;
|
||||
else
|
||||
break;
|
||||
}
|
||||
printf("%s\n", popped < N ? "NO" : "YES");
|
||||
while(getchar() != '\n') ;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user