前面填空参考https://blog.csdn.net/2301_76891851/article/details/143448411
后面大题code:

停车场停车

#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;
}

非零数减一

#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;
}

减法运算

#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

#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;
}

钩子

#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;
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部