Skip to content

Commit 1c337ca

Browse files
authored
Update 爬山算法.c
1 parent 67f2cd1 commit 1c337ca

File tree

1 file changed

+61
-54
lines changed

1 file changed

+61
-54
lines changed

爬山算法/爬山算法.c

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,69 @@
1-
#include<cstring>
2-
#include<string>
3-
#include<iostream>
4-
#include<queue>
5-
#include<cstdio>
6-
#include<algorithm>
7-
#include<map>
8-
#include<cstdlib>
9-
#include<cmath>
10-
#include<vector>
1+
#include <cmath>
2+
#include <cstdio>
3+
#include <cstring>
4+
#include <iostream>
5+
#include <algorithm>
6+
#define N 10010
7+
#define eps 1e-8
8+
#define pro 0.97
119

12-
//#pragma comment(linker, "/STACK:1024000000,1024000000");
13-
1410
using namespace std;
15-
16-
#define INF 0x3f3f3f3f
17-
18-
double y;
19-
20-
double getsum(double x)
11+
int n;
12+
struct Point
2113
{
22-
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x;
14+
double x,y;
15+
friend ostream& operator << (ostream &_,Point a)
16+
{
17+
printf("%.3lf %.3lf\n",a.x,a.y);
18+
return _;
19+
}
20+
Point(){}
21+
Point(double _x,double _y):x(_x),y(_y){}
22+
Point operator + (const Point &a)
23+
{return Point(x+a.x,y+a.y);}
24+
Point operator - (const Point &a)
25+
{return Point(x-a.x,y-a.y);}
26+
Point operator * (double rate)
27+
{return Point(x*rate,y*rate);}
28+
double operator * (const Point &a)
29+
{return x*a.x+y*a.y;}
30+
};
31+
struct GTY
32+
{
33+
Point p;
34+
double g;
35+
friend istream& operator >> (istream &_,GTY &a)
36+
{
37+
scanf("%lf%lf%lf",&a.p.x,&a.p.y,&a.g);
38+
return _;
39+
}
40+
}pt[N];
41+
double Get_Dis(Point a,Point b)
42+
{
43+
return sqrt((a-b)*(a-b));
2344
}
24-
25-
int main()
45+
Point Get_Direction(Point a)
2646
{
27-
int T;
28-
scanf("%d",&T);
29-
while(T--)
47+
Point p(0,0);
48+
for(int i=1;i<=n;i++)
3049
{
31-
scanf("%lf",&y);
32-
double delte=0.98;
33-
double t=100;
34-
double x=0;
35-
double ans=getsum(x);
36-
while(t>1e-8)
37-
{
38-
int flag=1;
39-
while(flag)
40-
{
41-
flag=0;
42-
double temp=x+t;
43-
if(temp>=0&&temp<=100&&getsum(temp)<ans&&fabs(ans-getsum(temp))>=1e-8)
44-
{
45-
x=temp;
46-
ans=getsum(temp);
47-
flag=1;
48-
}
49-
temp=x-t;
50-
if(temp>=0&&temp<=100&&getsum(temp)<ans&&fabs(ans-getsum(temp))>=1e-8)
51-
{
52-
x=temp;
53-
ans=getsum(temp);
54-
flag=1;
55-
}
56-
}
57-
t*=delte;
58-
}
59-
printf("%.4f\n",ans);
50+
Point tmp=(pt[i].p-a)*(pt[i].g/Get_Dis(pt[i].p,a));
51+
p=p+tmp;
6052
}
61-
return 0;
53+
return p;
54+
}
55+
Point Climb_Hill(Point now,double basement)
56+
{
57+
if(basement<eps)return now;
58+
Point to=Get_Direction(now);
59+
to=to*basement;
60+
return Climb_Hill(now+to,basement*pro);
61+
}
62+
int main()
63+
{
64+
scanf("%d",&n);
65+
for(int i=1;i<=n;i++)cin>>pt[i];
66+
Point Base_Point(1999.5,329.5);
67+
Point ans=Climb_Hill(Base_Point,100000);
68+
cout<<ans;
6269
}

0 commit comments

Comments
 (0)