At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space. Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output:

SC3021234 CS301133

把时间转换为一个整数,找到sign in里面最小的和sign out里面最大的那个,输出他们的ID即是结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
#include <string>

int main() {
int m = 0, hh = 0, mm = 0, ss = 0, earily = 99999999, later = 0;
char s[20];
scanf("%d", &m);
std::string e, l;
for (int i = 0; i < m; i++) {
scanf("%s %d:%d:%d", s, &hh, &mm, &ss);
if (hh * 3600 + mm * 60 + ss < earily) {
earily = hh * 3600 + mm * 60 + ss;
e = s;
}
scanf("%d:%d:%d", &hh, &mm, &ss);
if (hh * 3600 + mm * 60 + ss > later) {
later = hh * 3600 + mm * 60 + ss;
l = s;
}
}
printf("%s %s", e.c_str(), l.c_str());

return 0;
}

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

用字符串保存输入的数,并将这个树的各个位数进行累加求和,输出这个和的各个位数的英文即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>
#include <cstring>
#include <numeric>

char s[][7] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int flag = 0;

void print(int sum) {
if (sum == 0) return;
print(sum / 10);
if (flag) printf(" ");
flag = 1;
printf("%s", s[sum % 10]);
}

int main() {
char s[110];
scanf("%s", s);
int sum = std::accumulate(s, s + strlen(s), 0) - strlen(s) * '0';
if (sum == 0) printf("zero");
else print(sum);
return 0;
}

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] … ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01.

Output

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line. The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output “0 1” in a line.

Sample Input

2 1
01 1 02

Sample Output

0 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

vector<vector<int>> v(100);

int flag = 0;

void bfs() {
queue<int> q;
q.push(1);
while (!q.empty()) {
int n = q.size(), cnt = 0;
for (int i = 0; i < n; i++) {
int root = q.front();
q.pop();
if (v[root].size() == 0) cnt++;
for (int j = 0; j < v[root].size(); j++) {
q.push(v[root][j]);
}
}
if (flag) printf(" ");
flag = 1;
printf("%d", cnt);
}
}

int main() {
int n = 0, m = 0, id = 0, k = 0, t = 0;
scanf("%d %d", &n, &m);

for (int i = 0; i < m; i++) {
scanf("%d %d", &id, &k);
for (int j = 0; j < k; j++) {
scanf("%d", &t);
v[id].push_back(t);
}
}
bfs();
return 0;
}

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output

2 4

使用深搜遍历图的所有路径,在所有的路径里找到那条最优的路径。这种暴力破解的方法会耗时些,处理的不好可能会运行超时。

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
#include <cstdio>

int final_len = 10000, final_team, routes, isVisited[500], teams[500], cities[500][500];

void dfs(int cur, int dest, int len, int team, int n) {
if (cur == dest) {
if (final_len > len) {
final_len = len;
routes = 1;
final_team = team;
} else if (final_len == len) {
routes += 1;
final_team = (final_team > team ? final_team : team);
}
return;
}
for (int i = 0; i < n; i++) {
if (!isVisited[i] && cities[cur][i] != 0) {
isVisited[i] = 1;
dfs(i, dest, len + cities[cur][i], team + teams[i], n);
isVisited[i] = 0;
}
}
}

int main() {
int n = 0, m = 0, c1 = 0, c2 = 0;
scanf("%d %d %d %d", &n, &m, &c1, &c2);
for (int i = 0; i < n; i++) {
scanf("%d", &teams[i]);
}
for (int i = 0; i < m; i++) {
int a = 0, b = 0, l = 0;
scanf("%d %d %d", &a, &b, &l);
cities[a][b] = cities[b][a] = l;
}

isVisited[c1] = 1;
dfs(c1, c2, 0, teams[c1], n);
printf("%d %d", routes, final_team);
return 0;
}

另一种方法是使用Dijkstra算法来求解, Dijkstra利用贪心算法的思想,每次都找到距离起点最近的点,所以就没有那么耗时了。

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
#include <cstdio>

const int inf = 999999;
int recur[501], c[501][501], dist[501], visit[501], cnt[501], recurs[501];

int find_index(int n) {
int min = -1;
for (int i = 0; i < n; i++) {
if (!visit[i] && (min == -1 || dist[i] < dist[min])) {
min = i;
}
}
return min;
}

int main() {

for (int i = 0; i <= 500; i++) {
for (int j = 0; j <= 500; j++) {
c[i][j] = inf;
}
dist[i] = inf;
}

int n = 0, m = 0, c1 = 0, c2 = 0, a = 0, b = 0, l = 0;
scanf("%d %d %d %d", &n, &m, &c1, &c2);
for (int i = 0; i < n; i++) {
scanf("%d", &recur[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &a, &b, &l);
c[a][b] = c[b][a] = l;
}

dist[c1] = 0;
cnt[c1] = 1;
recurs[c1] = recur[c1];
while (true) {
int index = find_index(n);
if (index == -1) break;
visit[index] = 1;
for (int i = 0; i < n; i++) {
if (!visit[i]) {
if (dist[i] > dist[index] + c[index][i]) {
dist[i] = dist[index] + c[index][i];
cnt[i] = cnt[index];
recurs[i] = recurs[index] + recur[i];
} else if (dist[i] == dist[index] + c[index][i]) {
cnt[i] += cnt[index];
if (recurs[i] < recurs[index] + recur[i]) {
recurs[i] = recurs[index] + recur[i];
}
}
}
}
}
printf("%d %d", cnt[c2], recurs[c2]);
return 0;
}

This time, you are supposed to find A+B where A and B are two polynomials.

Input

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=1000.

Output

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 2 1.5 1 2.9 0 3.2

把多项式的指数作为数组的下标,把系数进行累加,统计数组中系数不为0的个数就是最后多项式的项数。

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
#include <cstdio>

int main() {
double ploy[1001] = {0.0};
int k = 0, n = 0, cnt = 0;
double an = 0;
for (int j = 0; j < 2; j++) {
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d %lf", &n, &an);
ploy[n] += an;
}
}

for (int i = 0; i <= 1000; i++) {
if (ploy[i] != 0.0) cnt++;
}

printf("%d", cnt);
for (int i = 1000; i >= 0; i--) {
if (ploy[i] != 0.0) {
printf(" %d %.1f", i, ploy[i]);
}
}
return 0;
}

Calculate a + b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input

-1000000 9

Sample Output

-999,991

将两个数的和格式化输出,由于两个数的和没有超过整型范围,所以可以直接用整型来处理。如果一个数的位数低于4位数,则不需要格式化;否则,可以把这个数的最低三位截掉输出,并处理前面剩下的几位数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdio>
#include <cstdlib>

void format(int x) {
if (abs(x) < 1000) {
printf("%d", x);
return;
}
format(x / 1000);
printf(",%03d", abs(x % 1000));
}

int main() {
int a = 0, b = 0;
scanf("%d %d", &a, &b);
format(a + b);
return 0;
}

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (<=50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers in the range [0, 109]. After the input of sets, a positive integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:

3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3

Sample Output:

50.0%
33.3%

把数据放到set当中,由于set是有序的并且是值是唯一的,所以可以很方便的求出两个集合的集合度

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
#include <cstdio>
#include <set>
#include <vector>

using namespace std;

double simulate_rate(const set<int> &a, const set<int> &b) {
int cnt = 0;
auto i = a.begin(), j = b.begin();
while (i != a.end() && j != b.end()) {
if (*i == *j) {
cnt++;
i++;
j++;

} else if (*i < *j) {
i++;

} else {
j++;

}

}

return cnt * 100.0 / (a.size() + b.size() - cnt);
}

int main() {
int n = 0, m = 0, t = 0, k = 0, a = 0, b = 0;
scanf("%d", &n);
vector<set<int>> v(n + 1);
for (int i = 1; i <= n; i++) {
scanf("%d", &m);
for (int j = 0; j < m; j++) {
scanf("%d", &t);
v[i].insert(t);

}

}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d %d", &a, &b);
printf("%.2f%%\n", simulate_rate(v[a], v[b]));

}
return 0;
}

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region’s culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made. Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (<=1000), the number of different kinds of mooncakes, and D (<=500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:

3 200
180 150 100
7.5 7.2 4.5

Sample Output:

9.45

用贪心的思想去解决,给月饼按照单价去排序,就可以计算给定需求量的最大收益了

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
#include <cstdio>
#include <algorithm>

struct mookcake {
double amount, price;
} mookcakes[1010];

int main() {
int n = 0, d = 0, index = 0;
scanf("%d %d", &n, &d);
for (int i = 0; i < n; i++)
scanf("%lf", &mookcakes[i].amount);
for (int i = 0; i < n; i++)
scanf("%lf", &mookcakes[i].price);

std::sort(mookcakes, mookcakes + n, [](mookcake a, mookcake b) {
if (a.price / a.amount > b.price / b.amount) return 1;
else if (a.price / a.amount - b.price / b.amount <= 0.00001) return 0;
else return -1;
});

double result = 0.0;
while (d > 0 && index < n) {
if (d >= mookcakes[index].amount) {
result += mookcakes[index].price;
d -= mookcakes[index].amount;
} else if (d < mookcakes[index].amount) {
result += d * (mookcakes[index].price / mookcakes[index].amount);
d = 0;
}
index++;
}
printf("%.2f", result);

return 0;
}

“一帮一学习小组”是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组。本题就请你编写程序帮助老师自动完成这个分配工作,即在得到全班学生的排名后,在当前尚未分组的学生中,将名次最靠前的学生与名次最靠后的异性学生分为一组。

输入格式:

输入第一行给出正偶数N(<=50),即全班学生的人数。此后N行,按照名次从高到低的顺序给出每个学生的性别(0代表女生,1代表男生)和姓名(不超过8个英文字母的非空字符串),其间以1个空格分隔。这里保证本班男女比例是1:1,并且没有并列名次。

输出格式:

每行输出一组两个学生的姓名,其间以1个空格分隔。名次高的学生在前,名次低的学生在后。小组的输出顺序按照前面学生的名次从高到低排列。

输入样例:

8
0 Amy
1 Tom
1 Bill
0 Cindy
0 Maya
1 John
1 Jack
0 Linda

输出样例:

Amy Jack
Tom Linda
Bill Maya
Cindy John

使用结构体存储每个学生的信息(性别和姓名),把学生的信息按输入的顺序存储到一个数组中,与此同时把学生的信息按照性别分别存储在两个双端队列male和female当中。 遍历学生信息数组的前半部分,如果当前学生的性别是男,则从male的前部弹出一个学生信息,并且输出female队列的最后一个学生信息;同理可以处理当前学生的性别为女的情况。

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
#include <cstdio>
#include <queue>

struct stu {
int gender;
char name[9];
} stus[51];

int main() {
int n = 0;
scanf("%d", &n);
std::deque<struct stu> male;
std::deque<struct stu> female;
for (int i = 0; i < n; i++) {
scanf("%d %s", &stus[i].gender, stus[i].name);
if (stus[i].gender == 0) female.push_back(stus[i]);
else male.push_back(stus[i]);
}
for (int i = 0; i < n / 2; i++) {
printf("%s ", stus[i].name);
if (stus[i].gender == 1) {
male.pop_front();
printf("%s\n", female.back().name);
female.pop_back();
} else {
female.pop_front();
printf("%s\n", male.back().name);
male.pop_back();
}
}
return 0;
}

The “eight queens puzzle” is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general N queens problem of placing N non-attacking queens on an N×N chessboard. (From Wikipedia - “Eight queens puzzle”.) Here you are NOT asked to solve the puzzles. Instead, you are supposed to judge whether or not a given configuration of the chessboard is a solution. To simplify the representation of a chessboard, let us assume that no two queens will be placed in the same column. Then a configuration can be represented by a simple integer sequence (Q1, Q2, …, QN), where Qi is the row number of the queen in the i-th column. For example, Figure 1 can be represented by (4, 6, 8, 2, 7, 1, 3, 5) and it is indeed a solution to the 8 queens puzzle; while Figure 2 can be represented by (4, 6, 7, 2, 8, 1, 9, 5, 3) and is NOT a 9 queens’ solution.

  #### Input Specification:

Each input file contains several test cases. The first line gives an integer K (1 < K <= 200). Then K lines follow, each gives a configuration in the format “N Q1 Q2 … QN”, where 4 <= N <= 1000 and it is guaranteed that 1 <= Qi <= N for all i=1, …, N. The numbers are separated by spaces.

Output Specification:

For each configuration, if it is a solution to the N queens problem, print “YES” in a line; or “NO” if not.

Sample Input:

4
8 4 6 8 2 7 1 3 5
9 4 6 7 2 8 1 9 5 3
6 1 5 2 6 4 3
5 1 3 5 2 4

Sample Output:

YES
NO
NO
YES

题目大意中给出的例子和图的关系我是没怎么看懂的。但N皇后问题,题目的陈述中已经有了,及相同行,相同列和对角线上面不能有其他皇后。row数组用来判断有每一行是否仅有一个皇后。isSameDiagonal函数判断是否有两个皇后在一条对角线上,这里我用的方法是斜率,即(横坐标差的绝对值)除(纵坐标差的绝对值)=1的两个点在一条直线上。这样就可以判断是否为N皇后问题了。

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
#include <cmath>
#include <cstdio>

using namespace std;

bool isSameDiagonal(int *q, int n, int current) { // same diagonal
for (int i = 1; i < current; i++) {
if (fabs(i - current) / fabs(q[i] - q[current]) == 1) {
return false;
}
}
return true;
}

bool isNQueues(int *q, int n) {
bool *row = new bool[n + 1]; // same row
for (int i = 2; i <= n; i++) {
row[q[i - 1]] = true;
if (row[q[i]] || !isSameDiagonal(q, n, i) || fabs(q[i] - q[i - 1]) <= 1) {
return false;
}
}
return true;
}

int main() {
int k = 0;
scanf("%d", &k);
int *q = new int[1010];
for (int c = 0; c < k; c++) {
int n = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &q[i]);
}
if (n >= 4 && isNQueues(q, n)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
0%