深信服提前批笔试题
当时没心情考了,只想出去看世界杯,把题目记了下来
1、小强想要抓兔子,但狡兔三窟。·························
这题应该用递归做:
#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”,可能是因为我题目没截全,不知道有啥用。
#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”不懂,数据保证只有唯一解是一定有唯一解得意思吗?
#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、最长重复子串的长度。
#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的一个排列,至少需要几次翻转能使得数组从小到大排列。
数逆序对。
#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;
}