7-1 Good in C (20分)
When your interviewer asks you to write "Hello World" using C, can you do as the following figure shows?
Each input file contains one test case. For each case, the first part gives the 26 capital English letters A-Z, each in a 7×5 matrix of C
's and .
's. Then a sentence is given in a line, ended by a return. The sentence is formed by several words (no more than 10 continuous capital English letters each), and the words are separated by any characters other than capital English letters.
It is guaranteed that there is at least one word given.
For each word, print the matrix form of each of its letters in a line, and the letters must be separated by exactly one column of space. There must be no extra space at the beginning or the end of the word.
Between two adjacent words, there must be a single empty line to separate them. There must be no extra line at the beginning or the end of the output.
思路:
(1)用三个数组把每个字母的图像存起来。
(2)然后按照每个字符进行读入,有非大写字母便分割输出。
(3)把单词放入vector中,三层循环,7行循环,单词内字母循环,5列循环。
#include
using namespace std;
char word[30][10][10];
int flag = 0;
int main() {
for (int i = 0; i < 26; i++)
for (int j = 0; j < 7; j++) {
for (int k = 0; k < 5; k++)
scanf("%c", &word[i][j][k]);
getchar();
}
string str;
while (true) {
char c;
if (scanf("%c", &c) == EOF)break;
//else if (c == '#')break;
else if (c >= 'A'&&c <= 'Z')str.push_back(c);
else if(str.size()>0){
if (flag)printf("\n");
for (int i = 0; i < 7; i++) {
for (int j = 0; j < str.size(); j++) {
for (int k = 0; k < 5; k++) {
int pos = str[j] - 'A';
printf("%c", word[pos][i][k]);
}
if (j != str.size() - 1)printf(" ");
}
printf("\n");
}
str = string();
flag = 1;
}
}
return 0;
}
7-2 Block Reversing (25分)
Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K nodes at the end of the list, the rest of the nodes are still considered as a block). Your job is to reverse all the blocks in L. For example, given L as 1→2→3→4→5→6→7→8 and K as 3, your output must be 7→8→4→5→6→1→2→3.
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the size of a block. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer, and Next
is the position of the next node.
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
00100 8 3
71120 7 88666
00000 4 99999
00100 1 12309
68237 6 71120
33218 3 00000
99999 5 68237
88666 8 -1
12309 2 33218
71120 7 88666
88666 8 00000
00000 4 99999
99999 5 68237
68237 6 00100
00100 1 12309
12309 2 33218
33218 3 -1
思路:(1)结构体+数组下标模拟链表
(2) 遍历链表,每K个用栈逆置。(reverse函数也可)
#include
#include
#include
using namespace std;
struct node{
int id,v,next;
};
node list[100005];
int start, n, k;
stacksta;
vectorans;
int main() {
scanf("%d %d %d", &start, &n, &k);
for (int i = 0; i < n; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
list[a].id = a;
list[a].v = b;
list[a].next = c;
}
//printf("\n\n");
for (int it = start; it != -1; it = list[it].next) {
sta.push(list[it]);
if (sta.size() == k) {
while (!sta.empty()){
ans.push_back(sta.top());
sta.pop();
}
}
}
while (!sta.empty()) {
ans.push_back(sta.top());
sta.pop();
}
for (int i = ans.size() - 1; i >= 0; i--)
if (i == 0)printf("%05d %d -1", ans[i].id, ans[i].v);
else printf("%05d %d %05d\n", ans[i].id, ans[i].v, ans[i - 1].id);
return 0;
}
7-3 Summit (25分)
A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.
Now given a set of tentative arrangements, your job is to tell the organizers whether or not each area is all set.
Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 200), the number of heads in the summit, and M, the number of friendship relations. Then M lines follow, each gives a pair of indices of the heads who are friends to each other. The heads are indexed from 1 to N.
Then there is another positive integer K (≤ 100), and K lines of tentative arrangement of rest areas follow, each first gives a positive number L (≤ N), then followed by a sequence of L distinct indices of the heads. All the numbers in a line are separated by a space.
For each of the K areas, print in a line your advice in the following format:
if in this area everyone is a direct friend of everyone, and no friend is missing (that is, no one else is a direct friend of everyone in this area), print Area X is OK.
.
if in this area everyone is a direct friend of everyone, yet there are some other heads who may also be invited without breaking the ideal arrangement, print Area X may invite more people, such as H.
where H
is the smallest index of the head who may be invited.
if in this area the arrangement is not an ideal one, then print Area X needs help.
so the host can provide some special service to help the heads get to know each other.
Here X
is the index of an area, starting from 1 to K
.
8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
2 4 6
3 3 2 1
Area 1 is OK.
Area 2 is OK.
Area 3 is OK.
Area 4 is OK.
Area 5 may invite more people, such as 3.
Area 6 needs help.
思路:(1)判断是否连通,是否强连通。
(2)邻接矩阵表示图,2层循环判断连接;再利用两层循环判断是否还有节点可以加入。
#include
#include
#include
using namespace std;
int n, m,k,t;
int net[300][300];
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
net[a][b] = 1;
net[b][a] = 1;
}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
if (i > 0)printf("\n");
scanf("%d", &t);
vectorque(t);
for (int j = 0; j < t; j++)scanf("%d", &que[j]);
bool flag = true;
for (int j = 0; j < t; j++)
for (int k = j + 1; k < t; k++)
if (!net[que[j]][que[k]]) { flag = false; break; }
if (!flag) {
printf("Area %d needs help.", i + 1);
continue;
}
for (int j = 1; j <= n; j++) {
int cnt = 0;
for (int k = 0; k < t; k++)
if (net[j][que[k]])cnt++;
else break;
if (cnt == t) {
printf("Area %d may invite more people, such as %d.", i + 1, j);
flag = false; break;
}
}
if (flag)printf("Area %d is OK.",i+1);
}
return 0;
}
7-4 Cartesian Tree (30分)
A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesian tree is shown by the figure.
Your job is to output the level-order traversal sequence of the min-heap Cartesian tree.
Each input file contains one test case. Each case starts from giving a positive integer N (≤30), and then N distinct numbers in the next line, separated by a space. All the numbers are in the range of int.
For each test case, print in a line the level-order traversal sequence of the min-heap Cartesian tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.
10
8 15 3 4 1 5 12 10 18 6
1 3 5 8 4 6 15 10 12 18
思路:(1)建树:最小的节点一定是根,然后递归建树。然后层寻遍历
#include
#include
#include
#include
using namespace std;
struct node {
int v;
node *left, *right;
};
int inorder[100], n;
node *tree;
queueque;
void build(int l,int r,node* &root) {
if (l > r) return;
int mm = 65536, it = 0;
for(int i=l;i<=r;i++)
if (inorder[i] < mm) {
mm = inorder[i];
it = i;
}
root = (node*)malloc(sizeof(node));
root->v = inorder[it];
root->left = root->right = NULL;
build(l,it-1,root->left);
build(it + 1, r, root->right);
}
void level_travel() {
que.push(tree);
while (!que.empty()) {
node* tp = que.front();
que.pop();
if (tp->left != NULL)que.push(tp->left);
if (tp->right != NULL)que.push(tp->right);
if (tp->v == tree->v)printf("%d", tp->v);
else printf(" %d", tp->v);
}
}
int main() {
scanf("%d",&n);
for (int i = 0; i < n; i++)scanf("%d", &inorder[i]);
build(0, n - 1, tree);
level_travel();
return 0;
}