B - Average Numbers CodeForces - 134A(水题,思维)

You are given a sequence of positive integers a1, a2, …, an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).

Input
The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, …, an (1 ≤ ai ≤ 1000). All the elements are positive integers.

Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.

If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.

Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4

就是水题,从头到尾遍历加和,然后一个一个遍历就好了。这个题有很多人一开始wa了,不知道是什么情况。在判断这个是否为平均数的时候,我才用的方法不是除以一个数,而是让另一个数乘以一个数这样来判断的。因为这样可以省去小数的麻烦。可能这样就可以了。
代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

const int maxx=2e5+10;
int a[maxx];
int b[maxx];
int n;

int main()
{
	while(cin>>n)
	{
		int sum=0;
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			sum+=a[i];
		}
		for(int i=0;i<n;i++)
		{
			if(a[i]*(n-1)==sum-a[i])
			{
				b[cnt++]=i+1;
			}
		}
		cout<<cnt<<endl;
		for(int i=0;i<cnt;i++) cout<<b[i]<<" ";
		cout<<endl;
	}
}

努力加油a啊。(o)/~

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/83036952