宝宝,文章都被你看光了,可以给我一个赞吗?拜托拜托❤️
或者点个关注也行谢谢宝宝💕
2. (1)函数最值问题
(2)TSP旅行商问题
(3)最小圆覆盖
(4)最小球覆盖
试利用模拟退火算法选做1题
hdu2899函数最值问题
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2
100
200
Sample Output
-74.4291
-178.8534
【参考代码】
#include <bits/stdc++.h>
using namespace std;
//模拟退火算法求函数最值
const double eps = 1e-8;
double y;
double calculate(double x)
{
return 6*pow(x, 7.0) + 8*pow(x, 6.0) + 7*pow(x, 3.0)+ 5*x*x - y*x;
}
double solve()
{
double temperature = 100.0, delta = 0.98, x=50.0;
double nowpoint = calculate(x);
double ans = nowpoint;
while(temperature > eps)
{
int addOrMinus[2] = {1, -1};
//newx在0-100之间反复横跳
double newx = x + addOrMinus[rand() % 2] * temperature;
if(newx >= 0 && newx <= 100) //防止温度超出0-100的范围
{
double nextpoint = calculate(newx);
ans = min(ans, nextpoint);
if(nowpoint - nextpoint > eps)
{
x = newx;
nowpoint = nextpoint;
}
}
temperature *= delta;
}
return ans;
}
int main()
{
int time;
cin>>time;
while(time--)
{
cin>>y;
printf("%.4f\n", solve());
}
return 0;
}
/*
2
100
200
output
-74.4291
-178.8534
*/
3.谁在中间
一天,农夫乔伊像往常一样来到了他的牧场,他突然对他的奶牛产奶量产生了兴趣。他想知道产奶量处于中间的那头奶牛的产奶量是多少,处于中间的意思是说,其中有一半牛的产奶量比它多,另一半牛的产奶量比它少。
这个问题现在交由你来写程序完成!
Input
有多组测试数据,第一行一个正整数T(1<=T<=20),接下来T行,每行第一个正整数为奇数N不会超过10^5,接下来N数字代表第i(1<=i<=N)头牛的产奶量。
Output
输出产奶量处于中间的牛的产奶量。
Sample Input
1
5 1 2 4 5 3
Sample Output
3
样例说明:
1 2 4 5 3排序后处于中间的那头奶牛是3
【参考代码】
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
int t;
cin>>t; //t次输入
while(t--)
{
int n, number;
cin>>n; //n个数
for(int i=0; i<n; i++)
{
cin>>number;
a.push_back(number);
}
sort(a.begin(), a.end());
cout << a[n/2] << endl;
a.clear();
}
return 0;
}
/*
1
5 1 2 4 5 3
*/