csp 201909-2 小明种苹果(续)
30分 例子能过
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(void){
int i = 0,n,m,ans = 0,ansi = 0,h,D = 0,k = 0,ansj = 0;
int a[5] = {0};
scanf("%d",&n);bool b[n] = {false};
while(n--){
ansi = 0;
scanf("%d", &m);
for(i = 0;i < m; i++){
scanf("%d", &h);
if(h > 0){
if(i==0){
ansi = h;
}
else if(ansi != h){
D++;ansi = h;b[n] = true;
}
}
else ansi += h;
}
ans += ansi;
}
for(i = 0;i < n-2; i++){
if(b[i]&&b[i+1]&&b[i+2]) ansj++;
}
if(b[n-2]&&b[n-1]&&b[0]) ansj++;
if(b[n-1]&&b[0]&&b[1]) ansj++;
printf("%d %d %d\n", ans, D, ansj);
return 0;
}
/*
4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0
*/
n的问题,前面n–,后面检查连续用的还是n。
假如现在计算得出的果树数,和后面重新统计输入的正数 值相等,就不要再ansi+=h了,所以加了if(h<=0)限制。
50分
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(void){
int i,n,m,ans = 0,ansi = 0,h,D = 0,k ,ansj = 0;
scanf("%d",&n);int b[n] = {0};k=n;
while(n--){
scanf("%d%d", &m,&ansi);
for(i = 1;i < m; i++){
scanf("%d", &h);
if(h > 0&&ansi != h){
D++;ansi = h;b[n] = 1;
}
else if(h<=0) ansi += h;
}
ans += ansi;
}n=k;
for(i = 0;i < n-2; i++){
if(b[i]&&b[i+1]&&b[i+2]) ansj++;
}
if(b[n-2]&&b[n-1]&&b[0]) ansj++;
if(b[n-1]&&b[0]&&b[1]) ansj++;
printf("%d %d %d\n", ans, D, ansj);
return 0;
}
如果某棵树被记录为掉落的树,那么记录一次即可, 不能多次记录。否则就会出错,只得了50分,就是没有考虑到这点。加入flag用来记录,这棵树是否已经被记录过了。
100分
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(void){
int i,n,m,T = 0,h,D = 0,E = 0,a;
scanf("%d",&n);int b[n+2] = {0};
for(int j=0;j<n;j++){
bool flag=true;
scanf("%d%d", &m,&a);
for(i = 1;i < m; i++){
scanf("%d", &h);
if(h > 0&&h != a){
if(flag){
b[j] = 1;D++;
}
a = h;flag=false;
}
else if(h<=0) a += h;
}
T += a;
}
for(i = 0;i < n-2; i++){
if(b[i]&&b[i+1]&&b[i+2]) E++;
}
if(b[n-2]&&b[n-1]&&b[0]) E++;
if(b[n-1]&&b[0]&&b[1]) E++;
printf("%d %d %d", T, D, E);
return 0;
}