首页 > 代码库 > POJ 2356 (抽屉原理)

POJ 2356 (抽屉原理)

题目中说:随便输出一组。抽屉原理可以求出取出的数连续的情况。

#include <iostream>#include<cstdio>using namespace std;int a[10005],s[10005];int n,start,end,ok = 0;int main(){    while(scanf("%d",&n) != EOF )    {        for(int i = 1; i <= n; i++)        {            scanf("%d",&a[i]);            s[i] = (s[i-1] + a[i])%n;        }    for(int  i = 1; i <= n; i++)    {        if(s[i] == 0)        {            start = 0;            end = i;            ok = 1;            break;        }        for(int j = i+1;j <= n;j++)        {            if(s[i] == s[j])            {                start = i;                end = j;                ok = 1;                break;            }        }        if(ok)            break;//注意上面的break只是退出了j循环,没有退出i循环    }    if(ok)    {        printf("%d\n",end-start);        for(int i = start+1;i <= end;i++)            printf("%d\n",a[i]);    }    else        printf("0\n");    }    return 0;}

 

POJ 2356 (抽屉原理)