ACM Ex.1003
<MAX SUM problem>:
Sum Problem II;
Input:
Firstline ->Cases number;
Otherlines -> first is the numbers of digital, others are the digital array.
Output:
Case#:
Sum(start pos) (end pos)
Example: sample input “2
22 3
51 2 3 4 -6 ”
sample output “
case1:
51 2
Case2:
101 4
Key points:
a). string library function study.(in glibc)
Meet Bugs:
a).using the strtok()...
C Code:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
void main(void)
{
FILE*fp1;
FILE*fp2;
fp1=fopen("test1","r");
fp2=fopen("test2","r+");
int a=0;
int b=0;
intlen=100;
charch[len];
char*chtmp;
memset(ch,0,len);
intcase_nbr=0;
fgets(ch,len,fp1);
sscanf(ch,"%d",&case_nbr);
printf("case_nbr=%d\n",case_nbr);
intnum=0;
int i=1;
intmax=0;
intsum=0;
intnbr=0;
intstart=1;
intstop=0;
while(case_nbr--)
{
fgets(ch,len,fp1);
sscanf(ch,"%d",&nbr);
printf("nbr=%d\n",nbr);
strtok(ch,"");
while(nbr--)
{
chtmp=strtok(NULL,"");
sscanf(chtmp,"%d",&num);
printf("%d \n",num);
sum+=num;
if(sum>max)
{
max=sum;
//printf("%d \n",max);
stop=i;
//printf("%d \n",stop);
}
i++;
}
printf("max=%dstart=%d stop=%d\n",max,start,stop);
sum=0;
max=0;
i=1;
}
fclose(fp1);
fclose(fp2);
}