poj 1484 Blowing Fuses

简介:

这道题不知道怎么设置的,一直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;
}


相关文章
|
8月前
Hopscotch(POJ-3050)
Hopscotch(POJ-3050)
|
测试技术
poj-1218 THE DRUNK JAILER 喝醉的狱卒
自己去看看原题; 题目大意: 就是一个狱卒喝醉了,他第一趟吧所有的监狱都带开,第二趟把能把二整除的监狱关闭,第三趟操作能把三整除的监狱; 求最后能逃跑的罪犯数 输入第一个数是代表 测试数据组数 每个数据代表狱卒来回的次数 当作开关问题即可 #include using names...
1018 0
POJ 2027 No Brainer
Problem Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indicating the number of data sets.
871 0
POJ 2262 Goldbach&#39;s Conjecture
Problem Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the foll...
1017 0
poj题目分类
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html
775 0