在此代碼中如何避免截斷整數除法?我排序的數組是1 1 1 1 1 1
,所以a [0] = 1和a [n]應該是1/2 = 0.5。截斷整數除法
int main()
{
long long n,w;
scanf("%lld %lld", &n, &w);
long long arr[2*n];
for(long long i = 0; i < 2 * n; i++)
{
scanf("%lld", &arr[i]);
}
sort(arr,arr+2*n);
long long a = arr[0];
long long b = (float)(arr[n]/2); // <--- this part of code
cout << " a is " << a << endl;
cout << " b is " << b << endl;
long long m = min(a,b);
cout << " m is " << m << endl;
long long and = min(m * n + m * 2LL * n, w);
printf("%lld", ans);
return 0;
}
C或C++?他們是不同的語言。 –
該帖子無效C代碼。 – chux
將'long long'轉換爲'float'對於大數值肯定會失去精度。 – chux