ACM probloms->1003

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);

}
發佈了30 篇原創文章 · 獲贊 39 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章