竞赛课第7周(模拟退火求函数最值问题)

文章讲述了如何利用模拟退火算法求解函数F(x)=6x^7+8x^6+7x^3+5x^2-yx在0到100范围内的最小值问题,以及如何通过编程找出奶牛产奶量处于中间位置的数值。提供了C++代码示例。
摘要由CSDN通过智能技术生成

宝宝,文章都被你看光了,可以给我一个赞吗?拜托拜托❤️

或者点个关注也行谢谢宝宝💕

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
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值