HDU5595:GTW likes math

GTW likes math

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 673    Accepted Submission(s): 327


Problem Description
After attending the class given by Jin Longyu, who is a specially-graded teacher of Mathematics, GTW started to solve problems in a book titled “From Independent Recruitment to Olympiad”. Nevertheless, there are too many problems in the book yet GTW had a sheer number of things to do, such as dawdling away his time with his young girl. Thus, he asked you to solve these problems.

In each problem, you will be given a function whose form is like f(x)=ax2+bx+c . Your assignment is to find the maximum value and the minimum value in the integer domain [l,r] .
 

Input
The first line of the input file is an integer T , indicating the number of test cases. ( T1000 )

In the following T lines, each line indicates a test case, containing 5 integers, a,b,c,l,r . ( |a|,|b|,|c|100,|l||r|100 ), whose meanings are given above.
 

Output
In each line of the output file, there should be exactly two integers, max and min , indicating the maximum value and the minimum value of the given function in the integer domain [l,r] , respectively, of the test case respectively.
 

Sample Input
   
   
   
   
1 1 1 1 1 3
 

Sample Output
  
  
  
  
13 3
Hint
$f_1=3,f_2=7,f_3=13,max=13,min=3$



这道题一开始想到用for循环,暴力去写。。。但是又一想到之前校内赛时有一道题也是已知函数表达式,求最值,当时用暴力写出来是错的。。。于是,这道题就用公式写,结果一直wa。。。没办法又用for去写,就过了。。。

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

double mi(double a, double b){
    if(a>b) return b;
    else return a;
}
double ma(double a, double b){
    if(a>b) return a;
    else return b;
}
int main()
{
    double a, b, c, l, r, minn, maxn,  y , y1, y2;
    int t;
    while(scanf("%d", &t) == 1){
        while(t--){
            scanf("%lf%lf%lf%lf%lf", &a, &b, &c, &l, &r);
            y1 = a*l*l+b*l+c;
            y2 = a*r*r+b*r+c;
            minn = y1;
            maxn = y2;
            for(int i = l; i <= r; i++){
                y = a*i*i+b*i+c;
                minn = mi(y, minn);
                maxn = ma(y, maxn);
            }
            printf("%.0lf %.0lf\n", maxn, minn);
        }
    }
    return 0;
}


你可能感兴趣的:(Math,C语言,ACM,HDU,BestCoder)