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
#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;
#define K 1000
int getmaxlivedays(int cur_pos, int cur_days, int tot_holes, int tot_days, int days_catch[]) {
if (cur_pos == days_catch[cur_days])
return 0;
else {
if (cur_days + 1 >= tot_days)
return 1;
int left = 0, right = 0;
if (cur_pos - 1 >= 0)
left = getmaxlivedays(cur_pos - 1, cur_days + 1, tot_holes, tot_days, days_catch) + 1;
if (cur_pos + 1 < tot_holes)
right = getmaxlivedays(cur_pos + 1, cur_days + 1, tot_holes, tot_days, days_catch) + 1;
return
left > right ? left : right;
}
}
int main()
{
int n, k;
while (cin >> n >> k) {
int days_catch[K];
for (int i = 0; i < k; i++) {
int temp;
cin>>temp;
days_catch[i] = temp - 1;
}
int maxlivedays = 0;
for (int i = 0; i < n; i++) {
maxlivedays = max(maxlivedays, getmaxlivedays(i, 0, n, k, days_catch));
}
if (maxlivedays < k)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}

2、小明的个人网站开业了。小明决定给最先访问网站的10个用户派发礼品。·············

题目中有一句,“有50%的输入数据满足:1<=n<=1000”,可能是因为我题目没截全,不知道有啥用。

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
#include "stdafx.h"
#include <iostream>
#include <set>
using namespace std;
#define N 100005
int main()
{
int n;
while (cin>>n)
{
int num[N];
for (int i = 0; i < n; i++) {
cin >> num[i];
}
set<int> user;
for (int i = 0; i < n; i++) {
user.insert(num[i]);
if (user.size() == 10)
break;
}
cout << user.size() << endl;
set<int>::iterator ite1 = user.begin();
set<int>::iterator ite2 = user.end();
for (; ite1 != ite2;ite1++) {
cout << *ite1 << endl;
}
}
return 0;
}

3、小明帮数学老师出试卷,················,让这些题目的分数总和正好是100分。

还是有一句“有50%的输入数据满足:1<=n<=3”不懂,数据保证只有唯一解是一定有唯一解得意思吗?

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 "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
#define N 10
int main()
{
int n;
while (cin >> n) {
int score[N];
for (int i = 0; i < n; i++) {
cin >> score[i];
}
int sum = 0;
vector<int> num;
int j = 1 << n;
while (j > 0) {
j--;
for (int i = 0; i < n; i++) {
int label = (1 << i) & j;
bool tag = (1 << i) & j != 0;
if (((1 << i) & j) != 0) {
sum = sum + score[i];
num.push_back(i);
}
}
if (sum == 100)
break;
else {
sum = 0;
num.clear();
}
}
cout << num.size() << endl;
vector<int>:: iterator iter1 = num.begin();
vector<int>:: iterator iter2 = num.end();
for (; iter1 != iter2; iter1++) {
cout << *iter1 + 1 << endl;
}
}
return 0;
}

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
#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;
#define L 10005
bool isdupli(int start, int end, char s[L]) {
if (end <= start)
return false;
int len = end - start;
if (end + len > strlen(s))
return false;
int i = 0;
while (i < len) {
if (s[start + i] != s[end + i])
break;
i++;
}
if (i == len)
return true;
else
return false;
}
int main()
{
char s[L];
while (cin >> s) {
int length = 0;
for (int i = 0; i < strlen(s); i++) {
for (int j = strlen(s) - 1; j >= 0; j--) {
if (isdupli(i, j, s) == true) {
length = max(length, j - i);
}
}
}
cout << 2 * length << endl;
}
return 0;
}

5、给定1到n的一个排列,至少需要几次翻转能使得数组从小到大排列。
数逆序对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int n;
int num[9];
while (cin >> n) {
for (int i = 0; i < n; i++) {
cin >> num[i];
}
int count = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (num[i] > num[j])
count++;
}
}
cout << count << endl;
}
return 0;
}

————————————————

原文链接:https://blog.csdn.net/allenqian0531/article/details/81070842