杭电 2014 年计算机复试真题

写在前面

此题目是根据 CSDN 博客粥粥同学发布的内容进行收集整理,记录了本人的解题过程和一些想法。仅供大家参考,如有错误,欢迎大家指出!


第一题

Problem Description

If you ever tried to read a html document on a Macintosh, you know how hard it is if no Netscape is installed. Now, who can forget to install a HTML browser? This is very easy because most of the times you don’t need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do? Your task is to write a small html-browser. It should only
display the content of the input-file and knows only the html commands (tags)
which is a linebreak and


which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line.

Input

The input consists of a text you should display. This text consists of words and HTML tags separated by one or more spaces, tabulators or newlines. A word is a sequence of letters, numbers and punctuation. For example, “abc,123” is one word, but “abc, 123” are two words, namely “abc,” and “123”. A word is always shorter than 81 characters and does not contain any ‘<’ or ‘>’. All HTML
tags are either <br> or <hr>.

Output

You should display the the resulting text using this rules:
. If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line.
. If you read a <br> in the input, start a new line.
. If you read a <hr> in the input, start a new line unless you already are at the beginning of a line, display 80 characters of ‘-’ and start a new line (again). The last line is ended by a newline character.

Sample Input

Hallo, dies ist eine
ziemlich lange Zeile, die in Html
aber nicht umgebrochen wird.
<br>
Zwei <br> <br> produzieren zwei Newlines.
Es gibt auch noch das tag <hr> was einen Trenner darstellt.
Zwei <hr> <hr> produzieren zwei Horizontal Rulers.
Achtung mehrere Leerzeichen irritieren

Html genauso wenig wie

mehrere Leerzeilen.

Sample Output

Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen
wird.
Zwei

produzieren zwei Newlines. Es gibt auch noch das tag
--------------------------------------------------------------------------------
was einen Trenner darstellt. Zwei
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html
genauso wenig wie mehrere Leerzeilen.

解题思路

题目大意:处理 HTML 标签<br><hr>,处理格式的题

参考源码

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
#include <cstring>
#include <iostream>
using namespace std;
int main() {
string s;
int c = 0;
while (cin >> s) {
if (s == "<br>") { //换行
cout << endl;
c = 0;
continue;
} else if (s == "<hr>") { //间隔线
if (c)
cout << endl
<< "--------------------------------------------------------------------------------"
<< endl;
else
cout << "--------------------------------------------------------------------------------"
<< endl;
c = 0;
continue;
} else {
if (!c) {
c = s.length();
cout << s;
} else if (c + s.length() + 1 > 80) { //超过80个字符
cout << endl << s;
c = s.length();
} else {
cout << " " << s;
c += s.length() + 1;
}
}
}
if (c) cout << endl;
return 0;
}

第二题

Problem Description

Given a string containing only ‘A’ - ‘Z’, we could encode it using the following method:

1.Each sub-string containing k same characters should be encoded to “kX” where “X” is the only character in this sub-string.
2.If the length of the sub-string is 1, ‘1’ should be ignored.

Input

The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only ‘A’ - ‘Z’ and the length is less than

Output

For each test case, output the encoded string in a line.

Sample Input

2
ABC
ABBCCC

Sample Output

ABC
A2B3C

解题思路

遍历字符串进行比较,若相同,则 count 加 1,否则输出

参考源码

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
#include <cstring>
#include <iostream>
using namespace std;
int main() {
int n, c;
string s, cs;
cin >> n;
getchar();
while (n--) {
cin >> s;
c = 1;
for (int i = 0; i < s.length(); i++) {
if (s[i] == s[i + 1])
c++; //相同则加1
else {
if (c > 1) cs += to_string(c); //大于1则添加数字
cs += s[i];
c = 1;
}
}
cout << cs << endl;
cs.clear();
}
return 0;
}

相关内容