杭电2014复试上机真题
ps:题是别的地方copy过来的,代码是自己的
第一题:
编写一个小型的html浏览器。主要目标是将一段文字以某种方式进行格式化。要求:遇到<br>则换行,遇到<hr>则换行,除非已经在一行的起始位置,然后输出80个‘-’,然后再换行。如果输入单词使得单行长度超过80则换行输出这个单词。所有输入中的换行符,空格,和回车换行在输出中都被视作一个空格。输出的最后一行以换行符结束(这里存疑,sample output没看到换行,oj里输出换行就通过了)。大意如此,建议看一下原文。
原题:
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) <br> which is a linebreak and <hr> 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.
#include<stdio.h>
#include<string.h>
//已通过hduoj所有测试用例
int main(){
char a[120];
int curlen = 0;
while(scanf("%s",a) != EOF){
if(strcmp(a,"<br>") == 0){ //判断<br>
printf("\n");
curlen = 0;
continue;
}
if(strcmp(a,"<hr>") == 0){ //判断<hr>
if(curlen != 0){
printf("\n");
}
for(int i = 0;i < 80;i++){
printf("-");
}
printf("\n");
curlen = 0;
continue;
}
curlen += strlen(a);
if(curlen + 1 > 80){
printf("\n");
printf("%s",a);
curlen = strlen(a);
}
else if(curlen - strlen(a) == 0){
printf("%s",a);
}
else{
printf(" %s",a);
curlen++;
}
}
printf("\n");
return 0;
}
/*
说一下空格和标点符号的处理,input中标点符号(逗号,句号)是一定连在单词后边的,所以判断
加入某单词一行长度是否大于80时,单词的长度要包括后边的标点符号(如果有)。input中的回车
换行,制表符(可以视为多个空格)和空格都会被视为一个空格,但是空格不会被scanf读取进去,
所以每输出一个单词字符串,我们就要手动输出空格,有两种方式:一是单词后边输出,而是单词前
边输出,本题隐含的是(我认为)在一个单词A快到80的边界时,如果下一个单词B超过界限,则下行
输出,这时AB中间的空格是不会输出在上一行的。所以选择单词前输出空格,因此判断单词长度是否
超过80时,需要计算:1(空格)+ 单词长 + 1(标点符号如果有,1位),同时在行起始位置时,不
需要输出空格。
*/
第二题:
给定一个仅包含“A”-“Z”的字符串,我们可以使用以下方法对其进行编码:
1。每个包含k个字符的子字符串应该被编码到“kX”,其中“X”是这个子字符串中唯一的字符。
2。如果子字符串的长度为1,则“1”应被忽略。
输入
第一行包含一个整数N(1 <= N <= 100),表示测试用例的数量。下一个N行包含N个字符串。每个字符串只包含“A”-“Z”,长度小于10000。
输出
对于每个测试用例,将编码的字符串输出到一行中。
#include<stdio.h>
#include<string.h>
int main(){
char a[10000];
int n;
scanf("%d",&n);
for(int i = 0;i < n;i++){
scanf("%s",a);
char* p = a;
char front = *p; //记录前一个字母
int num = 1; //记录重复字母的个数
p++;
while(*p != '\0'){
if(*p == front){
num++;
}
else{
if(num == 1){
printf("%c",front);
}
else{
printf("%d%c",num,front);
}
num = 1;
front = *p;
}
p++;
}
if(num){ //不要忘记p越界前,可能有未输出的字符
printf("%d%c",num,front);
}
printf("\n");
}
return 0;
}