1 魔术卡片

AC代码

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
freopen("hide.in", "r", stdin);
freopen("hide.out", "w", stdout);

int a1, b1, a2, b2;
cin >> a1 >> b1 >> a2 >> b2;

if (a1 < b1)
{
swap(a1, b1);
}

if (a2 < b2)
{
swap(a2, b2);
}

if((a1 >= a2 && b1 >= b2) || (a1 <= a2 && b1 <= b2))
{
cout << 1;
}
else
{
cout << 2;
}

return 0;
}

2 均分计算

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int main(int argc, const char * argv[])
{
freopen("average.in", "r", stdin);
freopen("average.out", "w", stdout);

int n;
cin >> n;
int a[n];
double score, sum = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i];
sum += a[i];
}

sort(a, a + n);

sum -= (a[0] + a[n - 1]);

score = sum / (n - 2);

printf("%.2f", score);

return 0;
}

3 出发能量

AC代码

#include<cstdio>

int main()
{
freopen("start.in", "r", stdin);
freopen("start.out", "w", stdout);

int n;
scanf("%d", &n);

int sum = 0;
int ans = 0;
for(int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
sum += x;
if(sum < 0)
{
ans += (-1 * sum);
sum = 0;
}
}

printf("%d", ans);

return 0;
}

4 衰减

AC代码

#include<bits/stdc++.h>
using namespace std;

int n, ans[10], ansN, tot;

void dfs(int num)
{
if(1 == num)
{
tot++;
printf("%d-", n);

for(int i=1; i<ansN; i++)
{
printf("%d-", ans[i]);
}
printf("%d\n", ans[ansN]);

return;
}

num >>= 1;
for(int i=1; i<=num; i++)
{
ans[++ansN] = i;
dfs(i);
--ansN;
}
}

int main()
{
freopen("down.in","r",stdin);
freopen("down.out","w",stdout);

scanf("%d", &n);
dfs(n);
printf("%d", tot);

return 0;
}

5 拆除桥墩

AC代码

#include<bits/stdc++.h>
using namespace std;

const int MAXINF=0x7fffffff;
const int MININF=0x80000000;

int n,m,l,d[50002];

bool check(int dmin)
{
int s=0, tot=0;
for(int i=1; i<=n; i++)
{
s += d[i];
if(s < dmin)
{
tot++;
}
else
{
s = 0;
}
}

return (tot <= m);
}

int main()
{
freopen("remove.in", "r", stdin);
freopen("remove.out", "w", stdout);
cin >> l >> n >> m;
for(int i=1; i<=n; i++)
{
cin >> d[i];
}

d[n+1] = l-d[n];
for(int i=n; i>=2; i--)
{
d[i] = d[i] - d[i-1];
}

n++;

int left = MAXINF, right = 0;
for(int i=1; i<=n; i++)
{
left = min(left, d[i]);
right += d[i];
}

while(left + 1 < right)
{
int mid = (left + right) >> 1;
if(check(mid))
{
left = mid;
}
else
{
right = mid;
}
}

cout << left;

return 0;
}

6 卜克游戏

AC代码
#include<bits/stdc++.h>
using namespace std;

int f[3002],P[3002],S[3002],n;

int main()
{
freopen("poker.in","r",stdin);
freopen("poker.out","w",stdout);

cin>>n;
for(int i=1;i<=n;i++)
{
cin>>P[i];
}

for(int i=1;i<=n;i++)
{
cin>>S[i];
S[i]+=S[i-1];
}

for(int i=1;i<=n;i++)
{
f[i]=f[i-1];
for(int j=i-1;j>=1;j--)
{
if(P[i]==P[j])
{
f[i]=max(f[i],S[i]-S[j-1]+f[j-1]);
}
}
}

cout<<f[n];

return 0;
}

2018年南海区小学生程序设计竞赛详细答案_c代码