CCF备战过程中的经典习题摘录

一些闲话

其实我对CCF这类考试不太感冒……但既然学校不收钱,我就去玩玩吧。
上回参加CCF我用的C++,因为不熟悉套路加上感冒惨败而归。这回打算试试Java,把专业的参考书也带上。

本文会持续更新(应该吧~)。恕我不附带题目,题目在CCF官网或百度都能很容易地找到。


正文

第十次CCF认证第二题,学生排队

这题本身不难,但它让我意识到,熟悉Java的那些库是多么重要

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
public class Main {
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int number = input.nextInt();
int time = input.nextInt();
int move, person, oldIndex, newIndex;
//用链式线性表来方便地实现移动(先删除再插入)操作
//比移动数组什么的好写多了
LinkedList<Integer> linkedList = new LinkedList<>();
for(int i = 0; i < number; i++) {
linkedList.add(i+1);
}
//通过观察发现,新位置和老位置又很明显的规律
for(int i = 0; i < time; i++) {
person = input.nextInt();
move = input.nextInt();
oldIndex = linkedList.indexOf(person);
//规律就那么简单~~
newIndex = oldIndex + move;
//先删除再插入
linkedList.remove(oldIndex);
linkedList.add(newIndex, person);
}
for(int i = 0; i < number; i++) {
System.out.print(linkedList.get(i));
if(i < number-1)
System.out.print(' ');
}
}
}

第十次CCF认证第二题,Markdown

这题是要模拟Markdown转换成html……这题我就拿了90分,不知道问题在哪,先挂上来。
字符串操作真是麻烦…………

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
public class Main {
//处理标题
static String changeTitle(String line) {
int counter = 0;
char[] charArray = line.toCharArray();
//数一数有几个#
while(charArray[counter] == '#') {
counter++;
}
//然后把#砍了再trim()
String res = line.substring(counter).trim();
return "<h" + counter + ">" + res + "</h" + counter + ">";
}
//列表还好说,部分处理放main函数里了
static String changeList(String line) {
return "<li>" + line.substring(1).trim() + "</li>\n";
}
//处理强调标记
static String changeEm(String line) {
StringBuilder temp = new StringBuilder(line);
//直接替换,一次换两个_
while(temp.toString().indexOf('_') != -1) {
temp.replace(temp.toString().indexOf('_'), temp.toString().indexOf('_')+1, "<em>");
temp.replace(temp.toString().indexOf('_'), temp.toString().indexOf('_')+1, "</em>");
}
return temp.toString();
}
//处理链接,挺麻烦的
static String changeLink(String line) {
char[] array = line.toCharArray();
StringBuilder temp = new StringBuilder(line);
StringBuilder link = new StringBuilder("");
StringBuilder text = new StringBuilder("");
String aaa = "";
for(int i = 0; i < array.length; i++) {
if(array[i] == '[') {
int j = i+1;
//先记下text和link
for(j = i+1; j < array.length && array[j] != ']'; j++) {
text.append(array[j]) ;
}
for(j = j+2; j < array.length && array[j] != ')'; j++) {
link.append(array[j]);
}
//System.out.println(link);
//System.out.println(text);
//然后拼接字符串,替换!
temp.replace(i, j+1, "<a href=" + '"' + link + '"' + ">" + text + "</a>");
}
}
return temp.toString();
}
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
String line = null;
StringBuilder newLine = new StringBuilder("");
StringBuilder allLine = new StringBuilder("");
while(sc.hasNextLine()) {
line = changeLink(changeEm(sc.nextLine()));
//System.out.println(line);
if(line.length() == 0)
continue;
//这个if是方便测试用的,提交代码时删除
if(line.startsWith("END"))
break;
//三个区块不重叠不嵌套,那就分别处理
//处理标题
if(line.startsWith("#")) {
//每获取新的一行,先上行内处理
newLine.append(changeTitle(sc.nextLine()));
//处理无序列表
}else if(line.startsWith("*")) {
newLine.append(changeList(sc.nextLine()));
//先把所有列表条目给加在一起
while(sc.hasNextLine()) {
line = changeLink(changeEm(sc.nextLine()));
if(line.length() == 0)
break;
if(line.startsWith("*")) {
newLine.append(changeList(line));
}
else
break;
}
//最后加上ul标签
newLine = new StringBuilder("<ul>\n" + newLine + "</ul>");
//处理段落,比较简单
}else {
newLine.append(line);
while(sc.hasNextLine()) {
line = changeLink(changeEm(sc.nextLine()));
if(line.length() == 0)
break;
else {
newLine.append("\n");
newLine.append(line);
}
}
newLine = new StringBuilder("<p>" + newLine + "</p>");
}
allLine.append(newLine);
allLine.append("\n");
//System.out.print("newLine: ");
//System.out.println(newLine);
newLine = new StringBuilder("");
}
//System.out.println("allLine: ");
System.out.println(allLine);
sc.close();
}
}