标签:
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#define MAX 10001
//[(物体数-1)÷抽屉数]+1
//给出N 个数,在其中找出m个数,使得m个数的和是N的倍数,输出m以及任意顺序这m个数,只要输出一种情况。
/*
可以把和求出来,然后对n取余,因为有n个和,对n取余,如果余数中没有出现0,
根据鸽巢原理,一定有两个数的余数相同,两个和想减就是n的倍数。如果余数出现0,自然就是n的倍数。
也就是说,n个数中一定存在一些数的和是n的倍数。求余输出即可。
*/
int num[MAX],locker[MAX];
int main()
{
int n;
while(~scanf("%d", &n) ) //number : n;
{
for(int i=1; i<=n; i++) //input number
scanf("%d", &num[i] );
int sum=0;
memset(locker, 0, sizeof(locker) ); //set 0 of 抽屉
int begin=0, end=0; //set start and end
for(int i=1; i<=n; i++)
{
sum = (sum+num[i]) % n; //前n个数求余
if(sum == 0) //是0就是从头开始到尾
{
begin=1;
end=i;
break;
}
else if( !locker[sum]) //不是件以余数为下标保存在抽屉中,抽屉内为该元素在在数组中地址
{
locker[sum]=i;
}
else //是,即sum[i]==sum[j],取i+1到j个数,所以begin要加一
{
begin=locker[sum]+1;
end=i;
break;
}
}
printf("%d\n", end-begin+1); //多少个数,注意加一
for(int i=begin; i<=end; i++) //输出
printf("%d\n", num[i] );
}
return 0;
}
/*
Description
The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
Input
The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
Output
In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.
If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input
5
1
2
3
4
1
Sample Output
2
2
3
*/
标签:
原文地址:http://www.cnblogs.com/sober-reflection/p/6aa834cf8e6e6816da458e80b908266a.html