HNCU程序设计周赛(2)题解

admin 发布于 2024-11-10 2192 字 520 次阅读


这场题目比上一场难亿点点

为什么湖南农大的都写b了你们没写出来

A:一亿二千三百四十五万六千七百八十九

#include<stdio.h>
int main()
{
    //这个题不就是你们上课学的switch吗
    //看到有人没写出来学长都伤心了
    //你用if语句也行
    int n;
    scanf("%d", &n);
    switch (n)
    {
    case 0: printf("零"); break;
    case 1: printf("一"); break;
    case 2: printf("二"); break;
    case 3: printf("三"); break;
    case 4: printf("四"); break;
    case 5: printf("五"); break;
    case 6: printf("六"); break;
    case 7: printf("七"); break;
    case 8: printf("八"); break;
    case 9: printf("九"); break;
    case 10:printf("十"); break;
    }
}

B.统计矩形的个数(sb写的)

易知正方形个数sum,易知所有矩形个数ans,长方形个数即为ans-sum

#include<stdio.h>
#define int long long//唯一真神
int min(int a,int b)
{
 if(a>b) return b;
 else return a;
}
signed main()
{
 int n, m;
 scanf("%lld %lld", &n, &m);
 int sum=0;
 int ans=0;
 for(int i=1;i<=min(n,m);i++)
 {
 sum+=(n+1-i)*(m+1-i);//自己手动模拟一下即可发现规律
 }
 for(int i=1;i<=n;i++)
 {
 ans+=m*(m+1)/2*(n+1-i);//自己手动模拟一下即可发现规律
 }
 printf("%lld %lld\n",sum,ans-sum);//不开long long 见祖宗啊
}

B.统计矩形的个数(聪明人写的)

#include<stdio.h>
#define int long long
int min(int a,int b)
{
    if(a>b) return b;
    else return a;
}
signed main()
{
	int n, m;
	scanf("%lld %lld", &n, &m);
    int sum=(1+n)*n/2*(1+m)*m/2;//所有矩形的数量
    //假设n为长
    //首先考虑一行的如果长为1 宽也为1 的话有n个矩形
    //如果长度为2 宽为1 有n-1个 自己可以手动模拟一下
    //一直到长度为n的有1个 这不就是个等差数列吗1-n的
    //对于m也是一样的先把(1+n)*n/2提出来在算个等差数列1-m的
    int ans1=0;//统计正方形数量也是一样的
    int mi=min(n,m);
    for(int i=1;i<=mi;i++)
        ans1+=(n-i+1)*(m-i+1);
    printf("%lld %lld",ans1,sum-ans1);
    
}

C:签到题

#include<stdio.h>
#include<string.h>
int main()
{
	char s[105],a[105],t=0;
	gets(s);
    //gets函数是C语言中用于从标准输入(stdin)读取字符串的函数。
    //它的功能是读取一行输入,直到遇到换行符为止,然后将换行符替换为字符串结束符'\0',
    //并将结果存储在提供的字符数组中。这个函数非常简单,只需要一个字符数组或字符指针作为参数。
	int n=strlen(s);
    //strlen函数是用来求字符串长度的函数,这个函数遇到‘\0’就会停止,且这个长度不包含‘\0’。
	for(int j=0;j<=(n-1)/2;j++){
	if(s[j]!=s[n-j-1])
    {
        t=1;
        break;
    }//如果已经判断了不是回文串可以直接退出循环了
	}
	if(t==1)
	printf("NO");
	else printf("YES");
 } 

D:找众数

#include<stdio.h>
#include<string.h>
int cnt[114515];//数组尽量开外面如果学了c++可以直接用vector
//数组cnt 用来统计每个数出现的多少次
int ans[114515];
//数组ans用来统计答案
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        cnt[x]++;
    }
    //统计最大数量有多少个
    int mx=0;
    int sum=0;//统计有多少个众数
    for(int i=1;i<=114514;i++)
        if(cnt[i]>mx)
            mx=cnt[i],sum=1;
        else if(cnt[i]==mx)
                sum++;
    printf("%d\n",sum);
    int t=sum;//记录一下有多少个众数防止等下减的时候变了
    for(int i=114514;i>0;i--)
        if(cnt[i]==mx)
            ans[sum]=i,sum--;
    for(int i=1;i<=t;i++)
        printf("%d ",ans[i]);
} 

E:小陆的答疑

c++先看着明天再补了学长累了

#include <bits/stdc++.h>

#pragma GCC optimize(2)

using namespace std;

#define int long long
#define endl '\n'
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl

typedef pair<int, int> PII;

inline void solve() {
    double weight1 = 1.0, weight2 = 1.0, weight3 = 1.0, weight4 = 1.0;
    double day_weights[4], night_weights[4];
    double study_weights[4], non_study_weights[4];
    double energetic_weights[4], exhausted_weights[4];
    char time[10], type[10], state[10];

    for (int i = 0; i < 4; i++) scanf("%lf", &day_weights[i]);
    for (int i = 0; i < 4; i++) scanf("%lf", &night_weights[i]);

    for (int i = 0; i < 4; i++) scanf("%lf", &study_weights[i]);
    for (int i = 0; i < 4; i++) scanf("%lf", &non_study_weights[i]);

    for (int i = 0; i < 4; i++) scanf("%lf", &energetic_weights[i]);
    for (int i = 0; i < 4; i++) scanf("%lf", &exhausted_weights[i]);

    scanf("%s %s %s", time, type, state);

    if (strcmp(time, "白天") == 0) {
        weight1 *= day_weights[0];
        weight2 *= day_weights[1];
        weight3 *= day_weights[2];
        weight4 *= day_weights[3];
    } else if (strcmp(time, "晚上") == 0) {
        weight1 *= night_weights[0];
        weight2 *= night_weights[1];
        weight3 *= night_weights[2];
        weight4 *= night_weights[3];
    }

    if (strcmp(type, "学习") == 0) {
        weight1 *= study_weights[0];
        weight2 *= study_weights[1];
        weight3 *= study_weights[2];
        weight4 *= study_weights[3];
    } else if (strcmp(type, "非学习") == 0) {
        weight1 *= non_study_weights[0];
        weight2 *= non_study_weights[1];
        weight3 *= non_study_weights[2];
        weight4 *= non_study_weights[3];
    }

    if (strcmp(state, "精力充沛") == 0) {
        weight1 *= energetic_weights[0];
        weight2 *= energetic_weights[1];
        weight3 *= energetic_weights[2];
        weight4 *= energetic_weights[3];
    } else if (strcmp(state, "精疲力尽") == 0) {
        weight1 *= exhausted_weights[0];
        weight2 *= exhausted_weights[1];
        weight3 *= exhausted_weights[2];
        weight4 *= exhausted_weights[3];
    }

    double max_weight = weight1;
    const char* answer = "不知道";
    if (weight2 > max_weight) {
        max_weight = weight2;
        answer = "这是原神";
    }
    if (weight3 > max_weight) {
        max_weight = weight3;
        answer = "这是明日方舟";
    }
    if (weight4 > max_weight) {
        max_weight = weight4;
        answer = "自己去查";
    }

    printf("%s\n", answer);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

F:wwwwvvv

超级简单的模拟

#include<stdio.h>
#include<string.h>
int main()
{
    char s[105];
    int ans=0;
    gets(s);
    //gets函数是C语言中用于从标准输入(stdin)读取字符串的函数。
    //它的功能是读取一行输入,直到遇到换行符为止,然后将换行符替换为字符串结束符'\0',
    //并将结果存储在提供的字符数组中。这个函数非常简单,只需要一个字符数组或字符指针作为参数。
    int n=strlen(s);
    //strlen函数是用来求字符串长度的函数,这个函数遇到‘\0’就会停止,且这个长度不包含‘\0’。
    //接下来就按题目模拟就行
    for(int i=0;i<n;i++)
        if(s[i]=='v')ans++;
        else ans+=2;
    printf("%d",ans);
            
 } 

G:正方体

学长之前也写过

#include<stdio.h>
#include<string.h>
int main()
{
     int n;
     scanf("%d",&n);
    int now=0;
    while(n--)
    {
        now++;
        int a[6];//数组小可以开里面大的话尽量开外面视情况而定
        int cnt=0;
        for(int i=1;i<=12;i++)
        {
            int x;
            scanf("%d",&x);
            if(x)//只有当有数字的时候才记录 其实也就是读6个数而已
                a[cnt]=x,cnt++;
        }
        if(a[0]==a[5]&&a[1]==a[3]&&a[2]==a[4])//不就是对位的相同吗
            printf("Yes!\n");
        else
            printf("No!\n");
        if(now==50)//注意每50个结果要加一个空行。
            printf("\n"),now=0;
    }
} 

H:小陆学长的硬币pro

简单的手玩题 就是自己在纸上模拟一下可以发现的 所以尽量还是带纸笔把 如果天赋异禀另说

#include <stdio.h>
#include <string.h>
char s[200005];
int min(int x, int y)//外部函数一个学了把
{
    if (x > y)
        return y;
    return x;
}
int main()
{
    //纯 手玩题
    gets(s);
    int len = strlen(s);
    int sum = 0, mi = 1e9, cnt = 0;
    for (int i = 0; i < len; i++)
    {
        if (s[i] == 'B')
            sum += cnt, mi = min(cnt, mi), cnt = 0;
        else
            cnt++;
    }
    sum += cnt;
    mi = min(mi, cnt);
    //简单的手玩题
    //手玩一下你可以发现如果第一个字母为B的话可以一直用BA直到把全部A覆盖完
    //如果结尾为B的话最后一段的A用AB其他的用BA去覆盖A
    //如果不是以B结尾的话 只能舍弃某一段A
    if (s[0] == 'B' || s[len - 1] == 'B')
        printf("%d", sum);
    else
        printf("%d", sum - mi);
}

I:压轴题(大概??)

学会的话差不多入门了

#include<stdio.h>
long long a[100005];
int main()
{
    int n;
    int m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int l,r,x;
        scanf("%d%d%d",&l,&r,&x);
        a[l]+=x;
        a[r+1]-=x;
    }//这其实是个简单差分
    for(int i=1;i<=n;i++)
        a[i]+=a[i-1];//求修改后的数组
    for(int i=1;i<=n;i++)
        a[i]+=a[i-1];//求一次前缀和
    int k;
    scanf("%d",&k);
    while(k--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%lld\n",a[r]-a[l-1]);//上场周赛出了前缀和
    }
}

记得补题!!!!!

最后更新于 2024-11-13