add advanced 1035, 1036, 1040, 1041

This commit is contained in:
Oliver Lew
2020-04-23 09:54:17 +08:00
parent 09b66c82bf
commit 253becc0ea
12 changed files with 675 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
---
layout: post
date: 2020-04-22 14:08:00 +0800
title: "PAT Advanced 1035. Password (20) (C语言实现)"
categories: Advanced
tags: [string processing]
permalink: Advanced/1035.html
---
## 题目
{% include_relative html/a1035.md %}
## 思路
{% include_relative analysis/a1035.md %}
## 代码
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1035.c),欢迎交流
```c
{% raw %}#include <stdio.h>
int main()
{
int N, count = 0, modified;
char username[1000][11], password[1000][11];
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
modified = 0;
scanf("%s %s", username[i], password[i]);
for(char *p = password[i]; *p; p++)
{
switch(*p)
{
case '1': *p = '@'; modified = 1; break;
case '0': *p = '%'; modified = 1; break;
case 'l': *p = 'L'; modified = 1; break;
case 'O': *p = 'o'; modified = 1; break;
default: break;
}
}
if(modified)
count++;
else /* mark unmodified password */
password[i][0] = '\0';
}
if(count)
{
printf("%d\n", count);
for(int i = 0; i < N; i++)
if(password[i][0] != '\0')
printf("%s %s\n", username[i], password[i]);
}
else
{
printf("There %s %d %s and no account is modified",
N > 1 ? "are" : "is", N, N > 1 ? "accounts" : "account");
}
return 0;
}
{% endraw %}```
+64
View File
@@ -0,0 +1,64 @@
---
layout: post
date: 2020-04-22 14:34:20 +0800
title: "PAT Advanced 1036. Boys vs Girls (25) (C语言实现)"
categories: Advanced
tags: [maxima and minima]
permalink: Advanced/1036.html
---
## 题目
{% include_relative html/a1036.md %}
## 思路
{% include_relative analysis/a1036.md %}
## 代码
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1036.c),欢迎交流
```c
{% raw %}#include <stdio.h>
typedef struct student {
char name[11];
char gender;
char ID[11];
} Student;
int main()
{
int N, grade, gradeF = -1, gradeM = 101;
Student students[101] = {0}, s;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%s %c %s %d", s.name, &s.gender, s.ID, &grade);
students[grade] = s;
if(s.gender == 'F')
gradeF = grade > gradeF ? grade : gradeF;
else
gradeM = grade < gradeM ? grade : gradeM;
}
if(gradeF != -1)
printf("%s %s\n", students[gradeF].name, students[gradeF].ID);
else
printf("Absent\n");
if(gradeM != 101)
printf("%s %s\n", students[gradeM].name, students[gradeM].ID);
else
printf("Absent\n");
if(gradeM == 101 || gradeF == -1)
printf("NA");
else
printf("%d", gradeF - gradeM);
return 0;
}
{% endraw %}```
+55
View File
@@ -0,0 +1,55 @@
---
layout: post
date: 2020-04-23 01:27:45 +0800
title: "PAT Advanced 1040. Longest Symmetric String (25) (C语言实现)"
categories: Advanced
tags: [string processing]
permalink: Advanced/1040.html
---
## 题目
{% include_relative html/a1040.md %}
## 思路
{% include_relative analysis/a1040.md %}
## 代码
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1040.c),欢迎交流
```c
{% raw %}#include <stdio.h>
#include <string.h>
int main()
{
int maxlen = 0, l, L;
char string[1002];
fgets(string, 1001, stdin);
L = strlen(string);
for(int i = 0; i < L; i++)
{
/* odd symmetric */
l = 0;
while(i - l >= 0
&& i + l < L
&& string[i - l] == string[i + l])
l++;
maxlen = (l * 2 - 1) > maxlen ? (l * 2 - 1) : maxlen;
/* even symmetric */
l = 0;
while(i - l >= 0
&& i + l + 1 < L
&& string[i - l] == string[i + l + 1])
l++;
maxlen = (l * 2) > maxlen ? (l * 2) : maxlen;
}
printf("%d", maxlen);
return 0;
}
{% endraw %}```
+46
View File
@@ -0,0 +1,46 @@
---
layout: post
date: 2020-04-23 01:50:40 +0800
title: "PAT Advanced 1041. Be Unique (20) (C语言实现)"
categories: Advanced
tags: []
permalink: Advanced/1041.html
---
## 题目
{% include_relative html/a1041.md %}
## 思路
{% include_relative analysis/a1041.md %}
## 代码
[Github最新代码](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1041.c),欢迎交流
```c
{% raw %}#include <stdio.h>
int main()
{
int N, counts[10001] = {0}, bets[100000] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", bets + i);
counts[bets[i]]++;
}
for(int i = 0; i < N; i++)
if(counts[bets[i]] == 1)
{
printf("%d", bets[i]);
return 0;
}
printf("None");
return 0;
}
{% endraw %}```
+8
View File
@@ -0,0 +1,8 @@
<!--
2020-04-22 14:08:00 +0800
string processing
-->
将密码中的几个字符转化为另一个字符,很简单的题目。
我的具体做法是边读边改,并且将无需更改的密码首字符置为`'\0'`,就是当做标记以便区分。后面输出就容易了。
+6
View File
@@ -0,0 +1,6 @@
<!--
2020-04-22 14:34:20 +0800
maxima and minima
-->
记录最大和最小,应该无需讲解了。
+8
View File
@@ -0,0 +1,8 @@
<!--
2020-04-23 01:27:45 +0800
string processing
-->
寻找字符串的对称部分,类似于回文数,但字符串的优势在于可以随时访问任意位置的字符,因此实现起来更简单。
注意:对称部分有可能是奇数个,也可能是偶数个,要考虑全面。
+8
View File
@@ -0,0 +1,8 @@
<!--
2020-04-23 01:50:40 +0800
-->
输出第一个独特的数字。
很简单,唯一要注意的是这里的*第一个*是指出现顺序。因此我实现中用了两个大数组。
+137
View File
@@ -0,0 +1,137 @@
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1035.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
## 题目
To prepare for PAT, the judge sometimes has to generate random passwords for
the users. The problem is that there are always some confusing passwords since
it is hard to distinguish `1` (one) from `l` (`L` in lowercase), or `0` (zero)
from `O` (`o` in uppercase). One solution is to replace `1` (one) by `@`, `0`
(zero) by `%`, `l` by `L`, and `O` by `o`. Now it is your job to write a
program to check the accounts generated by the judge, and to help the juge
modify the confusing passwords.
### Input Specification:
Each input file contains one test case. Each case contains a positive integer
$N$ ( $\le 1000$ ), followed by $N$ lines of accounts. Each account consists
of a user name and a password, both are strings of no more than 10 characters
with no space.
### Output Specification:
For each test case, first print the number $M$ of accounts that have been
modified, then print in the following $M$ lines the modified accounts info,
that is, the user names and the corresponding modified passwords. The accounts
must be printed in the same order as they are read in. If no account is
modified, print in one line `There are N accounts and no account is modified`
where `N` is the total number of accounts. However, if `N` is one, you must
print `There is 1 account and no account is modified` instead.
### Sample Input 1:
3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa
### Sample Output 1:
2
Team000002 RLsp%dfa
Team000001 R@spodfa
### Sample Input 2:
1
team110 abcdefg332
### Sample Output 2:
There is 1 account and no account is modified
### Sample Input 3:
2
team110 abcdefg222
team220 abcdefg333
### Sample Output 3:
There are 2 accounts and no account is modified
## 思路
将密码中的几个字符转化为另一个字符,很简单的题目。
我的具体做法是边读边改,并且将无需更改的密码首字符置为`'\0'`,就是当做标记以便区分。后面输出就容易了。
## 代码
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1035.c),欢迎交流
```c
#include <stdio.h>
int main()
{
int N, count = 0, modified;
char username[1000][11], password[1000][11];
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
modified = 0;
scanf("%s %s", username[i], password[i]);
for(char *p = password[i]; *p; p++)
{
switch(*p)
{
case '1': *p = '@'; modified = 1; break;
case '0': *p = '%'; modified = 1; break;
case 'l': *p = 'L'; modified = 1; break;
case 'O': *p = 'o'; modified = 1; break;
default: break;
}
}
if(modified)
count++;
else /* mark unmodified password */
password[i][0] = '\0';
}
if(count)
{
printf("%d\n", count);
for(int i = 0; i < N; i++)
if(password[i][0] != '\0')
printf("%s %s\n", username[i], password[i]);
}
else
{
printf("There %s %d %s and no account is modified",
N > 1 ? "are" : "is", N, N > 1 ? "accounts" : "account");
}
return 0;
}
```
+113
View File
@@ -0,0 +1,113 @@
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1036.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
## 题目
This time you are asked to tell the difference between the lowest grade of all
the male students and the highest grade of all the female students.
### Input Specification:
Each input file contains one test case. Each case contains a positive integer
$N$ , followed by $N$ lines of student information. Each line contains a
student's `name`, `gender`, `ID` and `grade`, separated by a space, where
`name` and `ID` are strings of no more than 10 characters with no space,
`gender` is either `F` (female) or `M` (male), and `grade` is an integer
between 0 and 100. It is guaranteed that all the grades are distinct.
### Output Specification:
For each test case, output in 3 lines. The first line gives the name and ID of
the female student with the highest grade, and the second line gives that of
the male student with the lowest grade. The third line gives the difference
$grade_F-grade_M$ . If one such kind of student is missing, output `Absent` in
the corresponding line, and output `NA` in the third line instead.
### Sample Input 1:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
### Sample Output 1:
Mary EE990830
Joe Math990112
6
### Sample Input 2:
1
Jean M AA980920 60
### Sample Output 2:
Absent
Jean AA980920
NA
## 思路
记录最大和最小,应该无需讲解了。
## 代码
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1036.c),欢迎交流
```c
#include <stdio.h>
typedef struct student {
char name[11];
char gender;
char ID[11];
} Student;
int main()
{
int N, grade, gradeF = -1, gradeM = 101;
Student students[101] = {0}, s;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%s %c %s %d", s.name, &s.gender, s.ID, &grade);
students[grade] = s;
if(s.gender == 'F')
gradeF = grade > gradeF ? grade : gradeF;
else
gradeM = grade < gradeM ? grade : gradeM;
}
if(gradeF != -1)
printf("%s %s\n", students[gradeF].name, students[gradeF].ID);
else
printf("Absent\n");
if(gradeM != 101)
printf("%s %s\n", students[gradeM].name, students[gradeM].ID);
else
printf("Absent\n");
if(gradeM == 101 || gradeF == -1)
printf("NA");
else
printf("%d", gradeF - gradeM);
return 0;
}
```
+77
View File
@@ -0,0 +1,77 @@
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1040.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
## 题目
Given a string, you are supposed to output the length of the longest symmetric
sub-string. For example, given `Is PAT&TAP symmetric?`, the longest symmetric
sub-string is `s PAT&TAP s`, hence you must output `11`.
### Input Specification:
Each input file contains one test case which gives a non-empty string of
length no more than 1000.
### Output Specification:
For each test case, simply print the maximum length in a line.
### Sample Input:
Is PAT&TAP symmetric?
### Sample Output:
11
## 思路
寻找字符串的对称部分,类似于回文数,但字符串的优势在于可以随时访问任意位置的字符,因此实现起来更简单。
注意:对称部分有可能是奇数个,也可能是偶数个,要考虑全面。
## 代码
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1040.c),欢迎交流
```c
#include <stdio.h>
#include <string.h>
int main()
{
int maxlen = 0, l, L;
char string[1002];
fgets(string, 1001, stdin);
L = strlen(string);
for(int i = 0; i < L; i++)
{
/* odd symmetric */
l = 0;
while(i - l >= 0
&& i + l < L
&& string[i - l] == string[i + l])
l++;
maxlen = (l * 2 - 1) > maxlen ? (l * 2 - 1) : maxlen;
/* even symmetric */
l = 0;
while(i - l >= 0
&& i + l + 1 < L
&& string[i - l] == string[i + l + 1])
l++;
maxlen = (l * 2) > maxlen ? (l * 2) : maxlen;
}
printf("%d", maxlen);
return 0;
}
```
+86
View File
@@ -0,0 +1,86 @@
### 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到[Github Pages](https://oliverlew.github.io/PAT)浏览最新内容([本篇文章链接](https://oliverlew.github.io/PAT/Advanced/1041.html))。此处文章目前已更新至与Github Pages同步。欢迎star我的[repo](https://github.com/OliverLew/PAT)。
## 题目
Being unique is so important to people on Mars that even their lottery is
designed in a unique way. The rule of winning is simple: one bets on a number
chosen from [ $1, 10^4$ ]. The first one who bets on a unique number wins. For
example, if there are 7 people betting on { 5 31 5 88 67 88 17 }, then the
second one who bets on 31 wins.
### Input Specification:
Each input file contains one test case. Each case contains a line which begins
with a positive integer $N$ ( $\le 10^5$ ) and then followed by $N$ bets. The
numbers are separated by a space.
### Output Specification:
For each test case, print the winning number in a line. If there is no winner,
print `None` instead.
### Sample Input 1:
7 5 31 5 88 67 88 17
### Sample Output 1:
31
### Sample Input 2:
5 888 666 666 888 888
### Sample Output 2:
None
## 思路
输出第一个独特的数字。
很简单,唯一要注意的是这里的*第一个*是指出现顺序。因此我实现中用了两个大数组。
## 代码
[最新代码@github](https://github.com/OliverLew/PAT/blob/master/PATAdvanced/1041.c),欢迎交流
```c
#include <stdio.h>
int main()
{
int N, counts[10001] = {0}, bets[100000] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", bets + i);
counts[bets[i]]++;
}
for(int i = 0; i < N; i++)
if(counts[bets[i]] == 1)
{
printf("%d", bets[i]);
return 0;
}
printf("None");
return 0;
}
```