HDOJ 5595 GTW likes math (暴力)

本文介绍了一道数学问题,即给定二次函数f(x)=ax²+bx+c,在整数区间[l,r]内找到该函数的最大值和最小值。通过遍历区间内的每个整数点并计算函数值来寻找最值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GTW likes math

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


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$
 
题意:求函数在区间内的最值
思路:数据不大,暴力就好


ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define INF 0x7fffffff
#define LL __int64
using namespace std;
int a,b,c;
LL fun(double x)
{
    return a*x*x+b*x+c;
}
int main()
{
    int t,i;
    int low,high;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d",&a,&b,&c,&low,&high);
        LL ans1=-INF,ans2=INF;
        for(i=low;i<=high;i++)
        {
            LL k=fun(i);
            ans1=max(ans1,k);
            ans2=min(ans2,k);
        }
        printf("%I64d %I64d\n",ans1,ans2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值