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;
}