1. 程式人生 > >B - Average Numbers CodeForces - 134A(水題,思維)

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)/~