题目:
Average Numbers
Problem Description
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 of the input is an integer t, which is the numbers of the test cases.
For each test case, the first line contains the integer n ( 2 ≤ n ≤ 2·10^5). The second line contains elements of the sequence a1, a2, ..., an ( 1 ≤ ai ≤ 1000). All the elements are positive integers.
Output
For each test case 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.
Sample Input
2
5
1 2 3 4 5
4
50 50 50 50
Sample Output
1
3
4
1 2 3 4
先吐槽一下,这个题我交了整整19次,估计被人看见要笑死了,虽然是水题一道但是水的我极为痛苦和不安呐,刚刚才通过的累死我了我快挂了,最后发现的问题是输出格式的问题我喉头一咸一口鲜血涌上几乎就要倒地而忘了。。。。终于对了,我真的有必要为这个问题写一个解题报告。。。。输出是硬伤啊,请注意最后输出,中间有空格,最后一个没有空格,万一有这个还没A的亲,千万不要犯这个错误,我早上就看这道题了下午出去找同学玩晚上回来还没A。。。还好刚刚A了不然今晚睡不好了是。就是又把通过率拉低了1%。。。
代码:
#include <stdio.h>
int t;
int a[2000000],b[2000000];
int op1[2000000]={0},op2[2000000]={0};
int i,j,k,m,n,l;
int sum;
float ave;
int main()
{
k=0;
sum=0;
scanf("%d",&t);
for (i=0;i<t;i++)
{
sum=0;
scanf("%d",&a[i]);
for (j=0;j<a[i];j++) b[j]=0;
for (j=0;j<a[i];j++)
{
scanf("%d",&b[j]);
sum=sum+b[j];
}
ave=(float)sum/a[i];
for (j=0;j<a[i];j++)
{
if (ave==b[j])
{
op1[i]=op1[i]+1;
op2[k]=j+1;
k=k+1;
}
}
}
l=0;
for (m=0;m<t;m++)
{
printf("%d\n",op1[m]);
if (op1[m]!=0)
{
for (n=l;n<op1[m]+l-1;n++)
printf("%d ",op2[n]);
printf("%d",op2[op1[m]+l-1]);
printf("\n");
}
l=op1[m]+l;
}
system("pause");
return 0;
}
本文是关于一道编程题的解题报告,题目要求找到序列中等于其他元素算术平均数的索引。作者经历了多次提交错误,最终发现问题是输出格式问题。解题代码使用C语言实现,通过计算平均数并比较数组元素来找出符合条件的索引。

338

被折叠的 条评论
为什么被折叠?



