2019.08.10 贝壳笔试编程题汇总

1、求数组里最小的差值的两个紧挨着的数

方法:暴力法

注意题目中给出的数值的范围用int会溢出,这里用long long

#include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<long long>temp;
vector<long long>result;
vector<long long>ret;
for (int i = 0; i < n; ++i){
long long j;
cin >> j;
temp.push_back(j);
}
long long m = (temp[1] - temp[0]) > 0 ? (temp[1] - temp[0]) : -(temp[1] - temp[0]);
ret.push_back(temp[0]);
ret.push_back(temp[1]);
result.push_back(m);
for (int i = 1; i < n - 1; ++i){
long long k = (temp[i + 1] - temp[i]) > 0 ? (temp[i + 1] - temp[i]) : -(temp[i + 1] - temp[i]);
if (k < result[result.size() - 1]){
result.push_back(k);
ret.push_back(temp[i]);
ret.push_back(temp[i + 1]);
}
}
cout << ret[ret.size() - 2] << " " << ret[ret.size() - 1] << endl;
}

2、最长严格递增子序列的长度

方法:动态规划

//dp
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int binary(int a[], int number, int low, int high){
int mid;
while (low <= high){
mid = (low + high) / 2;
if (number >= a[mid])
low = mid + 1;
else
high = mid - 1;
}
return low;
}
int dp(int a[], int n){
int len;
int*list = new int[n + 2]();
int count = 1;
list[count] = a[1];
for (int i = 2; i <= n; i++){
if (list[count] > a[i]){
len = binary(list, a[i], 1, count);
list[len] = a[i];
}
else if (list[count] < a[i])
list[++count] = a[i];
}
return count;
}
int main(){
int n;
cin >> n;
int*a = new int[n + 1]();
for (int i = 1; i <= n; i++){
cin >> a[i];
}
cout << dp(a, n) << endl;
return 0;
}

3、举重,90%体重那个(数组里的数代表体重,只有当轻的人的体重大于等于重的人的体重的90%才能进行比赛,求一共有多少场比赛)

方法:暴力法

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int N;
cin >> N;
vector<int>Wei;
int count = 0;
for (int i = 0; i < N; ++i){
int weight;
cin >> weight;
Wei.push_back(weight);
}
sort(Wei.begin(), Wei.end());
for (int i = 0; i < N-1; ++i){
for (int j = i + 1; j < N; ++j){
if(double(Wei[i]) >= 0.9*Wei[j])
++count;
}
}
cout << count << endl;

return 0;
}

4、调整数组(只能在某个位置上增加),使得数组先递增后递减、或者纯递增、或者纯递减,然后求最小的增加的次数

 方法:

贪心,枚举中间的转折点,然后从左到右和从右到左依次更新花费;
如果当前元素比前一个元素小,那么贪心当前的最小花费就是比前一个元素大一;
同理从后面往前遍历也是如此,然后最后取最小值。

#include<iostream>
#include<algorithm>
//#include<bits/stdc++.h> #include<bits/stdc++.h>包含了目前c++所包含的所有头文件
//eg:#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath>
//#include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set>
//笔试平台可以使用,方便,VS中并不支持,一般G++ 4.4以上就支持这个头文件了。但是会降低编译速度
using namespace std;
const int maxn = 5e3 + 233;
const double eps = 1e-8;
typedef long long LL;
int T, n, m, sum, ans;
LL a[maxn];
LL Lmax[maxn], Rmin[maxn];
LL Lsum[maxn], Rsum[maxn];//sum[i]表示当前元素以前所有总共增加的数
LL minCostIncThenDec(int n)
{
Lmax[0] = a[0];
Lsum[0] = 0;
for (int i = 1; i<n; ++i)
{
Lmax[i] = max(Lmax[i - 1] + 1, a[i]);//取给出的数组中元素与从左往右递增该处所需最小的的值的较大的那个,使之满足递增。
Lsum[i] = Lsum[i - 1] + Lmax[i] - a[i];//计算增量
}
Rmin[n - 1] = a[n - 1];
Rsum[n - 1] = 0;

for (int i = n - 2; i >= 0; --i)
{
Rmin[i] = max(Rmin[i + 1] + 1, a[i]);
Rsum[i] = Rsum[i + 1] + Rmin[i] - a[i];
}
LL ans = (1LL)*INT_MAX;
LL tmp;
for (int i = 0; i<n; ++i)
{
if (Lmax[i] > Rmin[i])
{
tmp = Lsum[i] + Rsum[i] - Rmin[i] + a[i];
}
else if (Lmax[i] < Rmin[i])
{
tmp = Lsum[i] + Rsum[i] - Lmax[i] + a[i];
}
else
{
tmp = Lsum[i] + Rsum[i] - Lmax[i] + a[i];
}
ans = min(ans, tmp);
}
return ans;
}
void input()
{
//freopen("in.txt","r",stdin);
cin >> n;
for (int i = 0; i<n; ++i)
{
cin >> a[i];
}
}
void solve()
{
cout << minCostIncThenDec(n) << endl;
}
int main()
{
input();
solve();
return 0;
}

猜你喜欢

转载自www.cnblogs.com/giraffeqiu/p/11334113.html