深圳技术大学11月月赛(未补题版)

文章包含了三个C语言编程题目,涉及基础算法(如判断奇偶数和等差数列前n项和)、递归和二分查找方法。通过实例展示了如何在编程中解决这些高中水平的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

d题签到题

#include <stdio.h>
int main()
{
	int n;
	scanf("%d", &n);
	int a;
	while (n--)
	{
		scanf("%d", &a);
		if (a % 2 == 0)
		{
			printf("NO\n");
		}
		else
		{
			printf("YES\n");
		}
	}
	return 0;
}

f题考验高中知识,等差数列的前n项和,注意要用double类型

#include <stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    while (n--)
    {
        double ans;
        scanf("%lf", &ans);
        double a, b, c;
        scanf("%lf%lf%lf", &a, &b, &c);
        if (c > 0 && a<b)
        {
            int num = (b - a - 1) / c + 1;
            ans = ans + (num / 2.0) * (2 * a + (num - 1) * c);
        }
        else if (c < 0 && a>b)
        {
            int num = (a - b - 1) / (-c) + 1;
            ans = ans + (num / 2.0) * (2 * a + (num - 1) * c);
        }
        printf("%.0f\n", ans);
    }
    return 0;
}

c题我想吃烤鸭 一开始我没有想到二分(就是题做少了) 我本来想用h/n的特殊情况特殊判断的,但是这样的话程序非常复杂,(后面也没找到这120行的破代码的问题),换句话就是我比较傻一点当大模拟做了

这种大量可能性的可以考虑二分,简单粗暴

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll n, h;
ll a[105] = { 0 };

int check(ll k)
{
    ll sum = k;
    for (int i = n - 1; i > 0; i--)
    {
        if (a[i + 1] - a[i] > k)
        {
            sum += k;
        }
        else
        {
            sum += a[i + 1] - a[i];
        }
    }
    if (sum >= h)
        return 1;
    else
        return 0;
}

void solve()
{
    scanf("%lld%lld", &n, &h);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
    }
    ll l = 1, r = 1e18, ans = 0;
    while (l <= r)
    {
        ll mid = (l + r) / 2;
        if (check(mid))
            ans = mid, r = mid - 1;
        else
            l = mid + 1;
    }
    printf("%lld\n", ans);
    memset(a, 0, sizeof(a[0]) * 105);
}



int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		solve();
	}
	return 0;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值