第二次训练课总结

· · 题解

T1:

解题思路:当二月的时候我们判断一下这一年是不是闰年(如果不是整百的数时,是四的倍数就是闰年;如果是整百数的话,还要是四百的倍数)。最后判断大小月的天数

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int y , m;
    cin >> y >> m;
    if(m == 2){
        if(y % 100 != 0 && y % 4 == 0){
            cout << 29;
        }else if(y % 100 == 0 && y % 400 == 0){
            cout << 29;
        }else{
            cout << 28;
        }
    }else if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
        cout << 31;
    }else{
        cout << 30;
    }
    return 0;
}

T2:

解题思路:这题必须遵循特定规则计算最终的加权平均成绩GPA。如果卷面得分在90分及以上,那么他的 GPA(加权平均成绩) 就是满分4.0。 如果卷面得分在60 ~ 89 之间,那么他每比90分少1分,那么他的分数就会在4.0的基础上减少0.1。 如果卷面得分不到60分,如果他的分数为x,那么老师会把他的分数调整为sqrt(x)*10(向下取整) ,再计算他的 GPA。如果经过调整该学生的得分依旧没满60,那么他就挂科了,GPA就是0分。

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin >> a;
    if(a < 60){
        a = (int)(sqrt(a) * 10);
    }
    if(a < 60){
        cout << "0.0";
        return 0;
    }
    double res = 4.0;
    res -= 0.1 * max((90 - a) , 0);
    printf("%.1lf",res);
    return 0;
}

T3:

解题思路:这题题目中说他的地毯必须是整数块的,所以想要恰好填满的话,a和b一定就是c的倍数。

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long a, b, c;
    cin >> a >> b >> c;
    if(a % c == 0 && b % c == 0){
        cout << (a / c) * (b / c);
    }else{
        cout << -1;
        return 0;
    }
    return 0;
}

T4:

解题思路:现在每个安装包都有依赖的安装包,现在给我们谁依赖谁让我们求1号安装包最少要安装多少个安装包(也就是1号安装包所有的依赖安装包)。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 5010;
bool g[N][N];
bool vis[N];
int ans , n;
void dfs(int step){
    if(vis[step]){
        return ;
    }    
    vis[step] = true;
    ans ++;
    for(int i = 1;i <= n;i ++){
        if(g[step][i]){
            dfs(i);
        }
    }
    return ;
}
int main(){
    cin >> n;
    for(int i = 1;i <= n;i ++){
        int m;
        cin >> m;
        for(int j = 1;j <= m;j ++){
            int x;
            cin >> x;
            g[i][x] = 1;
        }

    }
    dfs(1);
    cout << ans;
    return 0;
}

T5:

解题思路:保护伞的区别实际上就是在于半径不同。在第 n[i] 项,枚举自身以外的所有点。判断自身和自身以外的所有点的距离中最大的距离,因为要让我们把圆的面积尽做得可能的小,还要把另外所有点都包上,就要找到最大的距离当做圆的半径。

代码:

#include<bits/stdc++.h>
using namespace std;
double s[1010];
struct node{
    int x , y;
}a[1010];
int main(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++){
        cin >> a[i].x >> a[i].y;
    }
    double ans = INT_MAX;
    for(int i = 1;i <= n;i ++){
        s[i] = -1;
        for(int j = 1;j <= n;j ++){
            if(j != i){
                s[i] = max(s[i] , sqrt(abs(a[i].x - a[j].x) * abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y) * abs(a[i].y - a[j].y)));
            }
        }
        ans * 1.0;
        ans = min(ans , s[i]);
    }
    cout << fixed << setprecision(4) << ans * ans * 3.1415926535;
    return 0;
}

T6:

解题思路:题目是想让我们求山的起伏的最长的长度(即不间断的一上一下的最长长度),那么可以推出山峰的左边是单调不递减的,山峰的右边是单调不递增的。所以一座山的宽度 = 山峰左边不递减的长度 + 山峰右边不递增的长度 - 山峰(山峰算作左边不递减的终点,又算作右边不递增的起点),也就是枚举每一个点当成山峰山的宽度是多少。

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 10100;
int a[N], pre[N], nex[N];
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++){
        cin >> a[i];
    }
    for (int i = 1; i <= n; i ++) {
        if (a[i] >= a[i - 1]){
            pre[i] = pre[i - 1] + 1;
        }else{
            pre[i] = 1;
        }
    }
    for (int i = n; i >= 1; i --) {
        if (a[i] >= a[i + 1]){
            nex[i] = nex[i + 1] + 1;
        }else{
            nex[i] = 1;
        }
    }
    int res = 0;
    for (int i = 1; i <= n; i ++) {
        res = max(res, pre[i] + nex[i] - 1);
    }
    cout << res;
    return 0;
}

T7:

解题思路:现在题目给定一个N位数说可以让我们从中删除任意的K位数。我们可以最终一定留下一个N-K的位数。如果剩下的N-K的数字想要越小,显然高位越小,所以我们要先从高位考虑,要尽量的使高位最小。最终按照删除之后的新顺序,当成新数,所以后面的数字想要当高位,必须删除它前面的数字。

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 300;
bool st[N];
char res[N];

int main() {
    string ss;
    cin >> ss;
    int k;
    cin >> k;
    int cnt = 0;
    res[++ cnt] = ss[0];
    for (int i = 1; i < ss.size(); i ++) {
        while (cnt > 0 && k > 0 && ss[i] < res[cnt]) {
            k --;
            cnt --;
        }
        res[++ cnt] = ss[i];
    }
    while (k -- ) {
        cnt --;
    }
    int l = 1;
    while (l <= cnt && res[l] == '0') {
        l ++;
    }
    if (l > cnt) {
        cout << 0;
    } else {
        for (int i = l; i <= cnt; i ++) {
            cout << res[i];
        }
    }
    return 0;
}