Find a multiple

题目链接

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

前言:

这道题就不写题解了,之前做过差不多的,可以参考我的这篇文章https://blog.csdn.net/qq_45328552/article/details/99473883

我只说一下,我错误的原因。(做过原题还错,桑心,不过还好,弄懂了错误的原因)

1、没有看懂题意

题意是这样的:输入一个数n,给你n个数,让你找出一个区间,这个区间模上n等于0。然后输出找到区间的长度,并且输出区间内的数。

QAQ,英语不好。把区间的长度看成输出有几个这样的区间了。看来在理解样例的情况下,还要把样例代入题中,认真确定题意才行。

print the number of the chosen numbers in the first line 。
在第一行输出被选择数字的数量。
number数字,数目
the number of ...的数目

2、没有考虑到数组下标为0的情况,这个题要考虑下标为0的情况,因为当下标为0时,前缀和的余数就是0,这是刚好能被n除尽的一种情况,要从0开始算起。这种情况只要在预处理是,让sum[0]=0,p[sum[0]]=-1,遍历的时候从0开始就可以了。

AC代码:

//#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

typedef long long ll;

const int N=1e4+10;

ll a[N];
ll sum[N];//前缀和数组
ll p[N];//标记数组 
 
int main()
{
//	freopen("input.txt","r",stdin);
	memset(p,-1,sizeof p);
	memset(sum,0,sizeof sum);
 
   	ll n;
   	cin>>n;
   	for(ll i=1;i<=n;i++)
   	{
   		scanf("%lld",&a[i]);
		sum[i]=(sum[i-1]+a[i])%n;	
	}
	ll ans=0;
	ll length;
	for(ll i=0;i<=n;i++)
	{
		if(p[sum[i]]==-1 ) p[sum[i]]=i;
		else
		{
			ll length=i-p[sum[i]];
			printf("%lld\n",length);
			for(int j=p[sum[i]]+1;j<=i;j++)
				printf("%lld\n",a[j]);
			break;
		}
	}
	 	
	return 0;
}

士有百折不回至真心,才有万变不穷之妙用。—《菜根谭》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值