天津大学2024-2025 预推免 第一批机试题目纯暴力题解

发布于:2025-09-07 ⋅ 阅读:(27) ⋅ 点赞:(0)

题目A:流量计费

#include <bits/stdc++.h>
using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
		int m;
		cin>>m;
		int num=0;
		int temp;
		for(int i=1;i<=m;i++){
			cin>>temp;
			num+=temp;
		}
		if(t==1){
			if(num<=500)cout<<10;
			else{
				cout<<10+(num-500);
			} 
		}
		else{
			if(num<=500)cout<<10<<endl;
			else{
				cout<<10+(num-500)<<endl;
			} 
		}
	}
}

题目B:买水果

#include <bits/stdc++.h>
using namespace std;

/*
8 7 1
8 6 1
a从小到大{
    o从大到小
}
o从大到小{
    a从大到小
}
*/
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        int ans=0;
        cin>>n>>m;
        vector<int> a(n+5,0);
        vector<int> o(m+5,0);
        for(int i=0;i<n;i++)cin>>a[i];
        for(int i=0;i<m;i++)cin>>o[i];
        sort(a.begin(),a.end(),greater<int>());
        sort(o.begin(),o.end(),greater<int>());
        for(int i=0;i<n;i++){
            for(int j=ans;j<m;j++){
                if(a[j]>o[i]){
                    ans++;
                    break;
                }
            }
        }
        if(!t) cout<<ans;
        else cout<<ans<<endl;
    }
	return 0;
}

题目C:星期几?

#include <bits/stdc++.h>
using namespace std;

int rl[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
map<int,string> week={
    {1,"Monday"},
    {2,"Tuesday"},
    {3,"Wednesday"},
    {4,"Thursday"},
    {5,"Friday"},
    {6,"Saturday"},
    {0,"Sunday"}
};

bool run(int y){
    if(y%400==0||(y%4==0&&y%100!=0)) return true;
    return false;
}
int to_days(string s){
    int days=0;
    string ty=s.substr(0,4);
    int y=stoi(ty);
    string tm=s.substr(5,2);
    int m=stoi(tm);
    string td=s.substr(8,2);
    int d=stoi(td);
    for(int i=1900;i<y;i++){
        if(run(i)) days+=366;
        else days+=365;
    }
    for(int j=1;j<m;j++){
        days+=rl[j];
        if(run(y)&&j==2) days+=1;
    }
    days+=d;
    return days;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int pre_ans=to_days(s)%7;
        if(t) cout<<week[pre_ans]<<endl;
        else cout<<week[pre_ans];
    }
    return 0;
}

题目D:完美子序列

#include <bits/stdc++.h>
using namespace std;

/*
2
9 10
19 18 16 18 6 6 11 13 14
5 8
17 19 13 18 14

暴力:让每个位置都充当一次起点,向后尝试增加长度
每个位置{
    逐一增加长度
}
*/

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vector<int> a(n+5,0);
        for(int i=0;i<n;i++) cin>>a[i];
        int ans=0;
        for(int i=0;i<n;i++){
            int cha=0,end=i;
            int maxx=a[i],minn=a[i];
            while(cha<=m){   //新end加进来未知是否符合要求
                cha=max(cha,abs(maxx-minn));  
                if(cha<=m){
                    end+=1;
                    maxx=max(maxx,a[end]);
                    minn=min(minn,a[end]);
                }
            }
            ans=max(ans,end-i+1-1); //+1长度;-1最后end一定多1
        }
        if(!t) cout<<ans;
        else cout<<ans<<endl;
    }
    return 0;
}

题目E:最长子串

#include <bits/stdc++.h>
using namespace std;

/*
2
abbccdde
abcedfg

*/

bool check(string s){
    map<char,int> mp;
    int len=s.length();
    for(int i=0;i<len;i++){
        mp[s[i]]+=1;
        if(mp[s[i]]>2) return false;
    }
    for(auto v:mp){
        if(v.second!=2) return false;
    }
    return true;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int len=s.length();
        int ans=0;
        for(int l=0;l<len;l++){
            for(int r=l+1;r<len&&r<l+52;r+=2){
                string now=s.substr(l,r-l+1);
                if(check(now)) ans=max(ans,r-l+1);
            }
        }
        if(t) cout<<ans<<endl;
        else cout<<ans;
    }
    return 0;
}

网站公告

今日签到

点亮在社区的每一天
去签到