hdu 3809 (迭代法解方程)
题意:
x1 = x – sqrt(y)
y1 = y – sqrt(x)
给你x1和y1,求x,y,如果有多种,输出x最小的。
分析:
将方程转化为
x = x1 + sqrt(y);
y = y1 + sqrt(x);
可以发现x >= x1, y >= y1.
所以可以用迭代法无限逼近x, y
初始x=x1,y=y1;
x = x1 + sqrt(y);
y = y1 + sqrt(x);
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int T,cas=0;
double x1,y1;
cin>>T;
while(T--)
{
scanf("%lf %lf",&x1,&y1);
double x=x1,y=y1;
for(int i=0;i<40;i++)
{
x=x1+sqrt(y);
y=y1+sqrt(x);
}
printf("Case %d: ",++cas);
printf("%.6f %.6f\n",x,y);
}
return 0;
}

浙公网安备 33010602011771号