有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:
fun(A) sum = 0
for i = 1 to A.length
for j = i+1 to A.length
sum = sum + Floor((A[i]+A[j])/(A[i]*A[j]))
return sum
给出数组A,由你来计算fun(A)的结果。
例如:A = {1, 4, 1},fun(A) = [5/4] + [2/1] + [5/4] = 1 + 2 + 1 = 4。
#include<stdio.h>
#include<math.h>
int main(){
int i,j,k=0,l,n,p=0,m,a[110000];
int sum=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]==1)
{
k++;
}
else if(a[i]==2)
{
p++;
}
}
sum=(k-1)*k/2*2+(n-k)*k+(p-1)*p/2;
printf("%d",sum);
}