XMUOJ 2023C++实验课

XMUOJ 有的题真够恶心的。在此总结一下,同时造福后人。

厦大GPA

某位同学一共参加了4门考试,给定四门考试的总分,请问在最优情况下,4门考试绩点的和最高是多少?
分情况讨论:一门合格,两门合格,三门合格或者四门合格,只用考虑当前门合格的情况。(不然会超时)

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 100010

bool judge(double num, double a, double b){
    return num >= a && num <= b; 
}

double GPA(double num){
    if(num >= 90) return 4.0;
    else if(num >= 85 && num <= 89) return 3.7;
    else if(num >= 81 && num <= 84) return  3.3;
    else if(num >= 78 && num <= 80) return 3.0; 
    else if(num >= 75 && num <= 77) return 2.7; 
    else if(num >= 72 && num <= 74) return 2.3; 
    else if(num >= 68 && num <= 71) return 2.0; 
    else if(num >= 64 && num <= 67) return 1.7; 
    else if(num >= 60 && num <= 63) return 1.0; 
    else return 0.0; 
}

#define ll long long
double ans = 0; 
int n; 

int a[N], id = 0; 

int main(){
    while(cin >> n){
        ans = 0; 
        if(n <= 59){
            puts("0.0");
            continue; 
        }
        for(int i = 60; i <= 100; i++){
            if(i <= n){
                ans = max(ans, GPA(i)); 
            }
            for(int j = 60; j <= 100; j++){
                if(i + j <= n){
                    ans = max(ans, GPA(i) + GPA(j)); 
                }
                for(int k = 60; k <= 100; k++){
                    if(i + j + k <= n) ans = max(ans, GPA(i) + GPA(j) + GPA(k)); 
                    int h = n - i - j - k; 
                    if(h > 0 && i + j + k + h <= n){
                        ans = max(ans, GPA(i) + GPA(j) + GPA(k) + GPA(h)); 
                    }
                }
            }
        }
        printf("%.1lf\n", ans); 
    }
    return 0; 
}

山谷数

直接判断有没有山峰就好了,不用管那么多。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
    T x = 0, s = 1;
    char c = getchar(); 
    while(!isdigit(c)){ if(c == '-')  s = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
    a = x * s;
    return ; 
}

int y; 

int main(){
    while(scanf("%d", &y) != EOF){
        if(y == 0){
            puts("Yes");
            continue; 
        }
        int x = y; 
        vector <int> G; 
        while(x){
            G.push_back(x % 10); 
            x /= 10; 
        }
        bool flag = 1; 
        for(int i = 1; i < G.size() - 1; i++){
            if(G[i] >= G[i+1] && G[i] > G[i-1])
                flag = 0; 
        }
        puts(flag ? "Yes" : "No"); 
    }
    return 0; 
}

今年是2020年的第几天

注意2020年是闰年

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
    T x = 0, s = 1;
    char c = getchar(); 
    while(!isdigit(c)){ if(c == '-')  s = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
    a = x * s;
    return ; 
}

int y, m, d; 

int month[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 

int main(){
    while(cin >> y >> m >> d){
        int ans = 0; 
        for(int i = 1; i < m; i++)
            ans += month[i]; 
        ans += d; 
        cout << ans << endl; 
    }
}

矩阵扩展

描述

通过本课程的学习,我们对矩阵越来越熟悉。拷贝矩阵最外围一层元素,将原矩阵扩展为一个新的矩阵
注意是多测 = =

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
    T x = 0, s = 1;
    char c = getchar(); 
    while(!isdigit(c)){ if(c == '-')  s = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
    a = x * s;
    return ; 
}

ll a[50][50]; 
ll n, m; 

int main(){
    cin >> n >> m; 
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> a[i][j];
    for(int i = 1; i <= n; i++){
        a[i][0] = a[i][1]; 
        a[i][m+1] = a[i][m]; 
    }  
    for(int i = 1; i <= m; i++){
        a[0][i] = a[1][i]; 
        a[n+1][i] = a[n][i]; 
    }
    a[0][0] = a[0][1]; 
    a[n+1][m+1] = a[n][m]; 
    a[0][m+1] = a[0][m]; 
    a[n+1][0] = a[n][0]; 
    for(int i = 0; i <= n + 1; i++){
        for(int j = 0; j <= m + 1; j++){
            printf("%d", a[i][j]); 
            if(j != m + 1) printf(" "); 
        }
        cout << endl; 
    }
    return 0; 
}

统计字符的出现次数

输入字符串,字符串由英文字母、数字、标点符号和运算符号组成 (不含空格)。

统计每个字符在该字符串中出现次数,按出现次数从大到小的顺序,将字符和其出现次数输出。

用结构体排序即可。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 100010

map <char, int> g; 
struct node{
    char ch; 
    int num; 
} p[N]; 

int main(){
    string s; 
    while(cin >> s){
        int id = 0; 
        g.clear(); 
        for(int i = 0; i < s.length(); i++){
            if(!g[s[i]]) g[s[i]] = ++id; 
            p[g[s[i]]].ch = s[i];
            p[g[s[i]]].num++;  
        }
        sort(p + 1, p + id + 1, [](node a, node b) -> bool {return a.num == b.num ? a.ch > b.ch : a.num > b.num; }); 
        for(int i = 1; i <= id; i++)
            printf("%c:%d ", p[i].ch, p[i].num); 
        cout << endl;
        for(int i = 0; i < s.length(); i++)
            p[g[s[i]]].ch = p[g[s[i]]].num = 0;  
    }
}

三角形

描述

三角形需满足两边之和大于第三边。

假设有 \(N\) 根木棒,现在要挑选其中的三根,问能拼出的三角形的最大周长是多少?

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
    T x = 0, s = 1;
    char c = getchar(); 
    while(!isdigit(c)){ if(c == '-')  s = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
    a = x * s;
    return ; 
}

ll a[N]; 
int n; 

int main(){
    while(cin >> n){
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        sort(a + 1, a + n + 1); 
        ll ans = -1; 
        for(int i = 1; i <= n; i++){
            for(int j = i + 1; j <= n; j++){
                ll x = a[i] + a[j]; 
                auto it = lower_bound(a + 1, a + n + 1, x) - a; 
                it--;  
                if(it > j){
                    ans = max(ans, a[i] + a[j] + a[it]); 
                }
            }
        }
        cout << ans << endl; 
    } 
    
    return 0; 
}
posted @ 2023-03-22 21:41  雪之下,树之旁  阅读(764)  评论(0编辑  收藏  举报