这道题不知道怎么设置的,一直RE,网上发现大家也一样,不过他们是没有让输入结束就 break;的原因,我这是开始都输入完毕才开始处理也不行。。。
不知道为什么。。。改了一下 opt ,不用数组就A了。。。开始这个题目设置的有点bug。。。opt数组改大点,比如到1000,也A了
AC的代码:
#include <stdio.h> #include <string.h> int main() { int devCon[25]; int opt; int flag[25]; int n,m,c; int i; int max,sum; int count=0; while(scanf("%d%d%d",&n,&m,&c)) { if(n==0 && m==0 && c==0) return 0; //init memset(flag,0,sizeof(flag)); sum=0; max=-1; //sum=0,max=-1; for(i=1;i<=n;i++) scanf("%d",&devCon[i]); for(i=1;i<=m;i++) { scanf("%d",&opt); if(flag[opt]==0) { sum+=devCon[opt]; flag[opt]=1; } else { sum-=devCon[opt]; flag[opt]=0; } if(sum>max) max=sum; } printf("Sequence %d\n",++count); if(max<=c) //没被烧 { printf("Fuse was not blown.\n"); printf("Maximal power consumption was %d amperes.\n",max); } else //保险丝烧掉了 printf("Fuse was blown.\n"); printf("\n"); } return 0; }
AC代码之二:
#include <iostream> #include <string.h> int devCon[25]; int opt[1000]; int flag[25]; int main() { int n,m,c; int i; int max,sum; int count=0; while(scanf("%d%d%d",&n,&m,&c)) { if(n==0 && m==0 && c==0) return 0; //init printf("Sequence %d\n",++count); memset(flag,0,sizeof(flag)); sum=0; max=-1; //sum=0,max=-1; //输入 for(i=1;i<=n;i++) scanf("%d",&devCon[i]); for(i=1;i<=m;i++) scanf("%d",&opt[i]); //开始处理 for(i=1;i<=m;i++) { if(flag[opt[i]]==0) { sum+=devCon[opt[i]]; flag[opt[i]]=1; } else { sum-=devCon[opt[i]]; flag[opt[i]]=0; } if(sum>c) break; if(sum>max) max=sum; } if(i>m) //没被烧 { printf("Fuse was not blown.\n"); printf("Maximal power consumption was %d amperes.\n",max); } else //保险丝烧掉了 printf("Fuse was blown.\n"); printf("\n"); } return 0; }