前面填空参考https://blog.csdn.net/2301_76891851/article/details/143448411
后面大题code:
停车场停车
c
#include <bits/stdc++.h>
#define int long long
using namespace std;
int tmp;
signed main()
{
int n;cin>>n;
if(n<15)cout<<"0";
else{
cout<<(n/15)*2;
}
return 0;
}
非零数减一
c
#include <bits/stdc++.h>
#define int long long
using namespace std;
int tmp;
signed main()
{
char c='0';
int n;cin>>n;
string a=to_string(n);
for(int i=0;i<a.length();i++){
if(c<a[i])c=a[i];
}
cout<<c;
return 0;
}
减法运算
c
#include <bits/stdc++.h>
#define int long long
using namespace std;
int tmp;
signed main()
{
string res1="",res2="";
string a;cin>>a;
int index=a.find("-");
for(int i=0;i<index;i++)res1+=a[i];
for(int i=index+1;i<a.length();i++)res2+=a[i];
int cnt1=stoll(res1);int cnt2=stoll(res2);
cout<<cnt1-cnt2;
return 0;
}
间隔为k
c
#include <bits/stdc++.h>
#define int long long
using namespace std;
int mx=0;
int arr[100010];
signed main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>arr[i];
int k;cin>>k;
int r=n-2*k-2;
for(int i=1;i<=r;i++){
int sum=0;//sum要在这里进行初始化
for(int j=i;j<=i+2*k-2;j+=2){
sum+=arr[j];
}
if(sum>mx){
mx=max(mx,sum);
}
}
cout<<mx;
return 0;
}
钩子
c
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (n < 3) {
cout << 0 << endl;
return 0;
}
vector<int> decreasing(n, 1);
vector<int> increasing(n, 1);
// 计算每个位置之前的最长递减子序列
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (a[j] > a[i]) {
decreasing[i] = max(decreasing[i], decreasing[j] + 1);
}
}
}
// 计算每个位置之后的最长递增子序列
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
if (a[j] > a[i]) {
increasing[i] = max(increasing[i], increasing[j] + 1);
}
}
}
int max_length = 0;
for (int i = 0; i < n; i++) {
if (decreasing[i] > 1 && increasing[i] > 1) {
max_length = max(max_length, decreasing[i] + increasing[i] - 1);
}
}
cout << max_length << endl;
return 0;
}