您的位置:首页 > 其它

数学规律,一元二次方程求最大值。

2015-12-05 21:55 465 查看
E - E
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

Description

A group of N Internet Service Provider companies (ISPs) use a private communication channel that has a maximum capacity of C traffic units per second. Each company transfers T traffic units per second through
the channel and gets a profit that is directly proportional to the factor T(C - T*N). The problem is to compute the smallest value of T that maximizes the total profit the N ISPs can get from using the channel.
Notice that N, C, T, and the optimalT are integer numbers.

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with a line containing two integers N and C (0 ≤ N, C ≤ 109).

Output

For each case, print the case number and the minimum possible value of T that maximizes the total profit. The result should be an integer.

Sample Input

6

1 0

0 1

4 3

2 8

3 27

25 1000000000

Sample Output

Case 1: 0

Case 2: 0

Case 3: 0

Case 4: 2

Case 5: 4

Case 6: 20000000

题意:求一元二次方程T*(C-N*T)取得最大值时,T的值是多少。

ac代码:

//坑死,关键还是对题目的理解。
#include<stdio.h>
#include<string.h>
#include<math.h>
int main(){
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++){
long long N,C;
scanf("%lld%lld",&N,&C);
if(N==0){
printf("Case %d: %d\n",i,0);
continue ;
}
long long n,m;
float mid;
mid=(float)C/2/N;
n=floor(mid);//向下取整
m=ceil(mid);//向上取整
long long ans;
long long temp1=n*(C-N*n),temp2=m*(C-N*m);
if(temp1>=temp2){//尽量减少使用实数进行大小的判断。
ans=n;
}
else
ans=m;
printf("Case %d: %lld\n",i,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: