pku 3370

pku 3370
http://acm.pku.edu.cn/JudgeOnline/problem?id=3370
鸟巢原理
&&fflush害死人的错
 1 #include < iostream >
 2 using namespace std;
 3 #define Max_N  100001
 4 int  c,n,i,j,tem;
 5 char t;
 6 int  candy[Max_N];
 7 int  sum[Max_N];
 8 int  b[Max_N];
 9 int  main()
10 {
11     
12     sum[ 0 ] = 0 ;
13      while ( 1 ){
14         scanf( " %d%d " , & c, & n);
15         getchar();
16          if (!c && !n)break;
17          if (!c){printf( " no sweets\n " );continue;}
18         memset(candy, 0 ,sizeof(candy));
19         memset(b, 0 ,sizeof(b));
20          for (i = 1 ;i <= n;i ++ ){
21              while (t = getchar()){ // 通过getchar的方法来读数
22                  if (t == '  '||t=='\n')break;
23                 candy[i] = 10 * candy[i] + t - ' 0';}
24              if (!(candy[i]%c)){ // 如果读到的数能被c整除,就已经可以了
25                 printf( " %d\n " ,i);
26                 break;}
27             sum[i] = (sum[i - 1 ] + candy[i])%c; // 如果当前读到的所有数之和也满足情况,这样也行
28              if (!sum[i]){
29                  for (j = 1 ;j <= i;j ++ ){
30                     printf( " %d " ,j);
31                      if (j! = i)putchar( '  ');
32                      else  putchar( ' \n');
33                 }
34                 break;}
35         }
36          for (j = i + 1 ;j <= n;j ++ )scanf( " %d " , & tem);
37          if (i > n) // 处理剩下的一众情况
38              for (i = 1 ;i <= n;i ++ ){
39                  if (!b[sum[i]])
40                     b[sum[i]] = i;
41                  else  {
42                      for (j = b[sum[i]] + 1 ;j <= i;j ++ ){
43                         printf( " %d " ,j);
44                          if (j! = i)putchar( '  ');
45                          else  putchar( ' \n');
46                     }
47                     break;}}
48
49     }        
50     return  0 ;
51 }

你可能感兴趣的:(pku 3370)