#include
#include
#include
double weight(char *s){
if (strcmp(s, "m") == 0) return 1000;
if (strcmp(s, "dm") == 0) return 100;
if (strcmp(s, "cm") == 0) return 10;
if (strcmp(s, "mm") == 0) return 1;
if (strcmp(s, "um") == 0) return 1e-3;
if (strcmp(s, "nm") == 0) return 1e-6;
return 1;
}
int main(){
double a, b, c, d;
char sa[3], sb[3], sc[3];
int T, cas;
scanf("%d", &T);
for (cas = 1; cas <= T; cas++){
scanf("%lf%s%lf%s%lf%s", &a, sa, &b, sb, &c, sc);
// printf("%s,%s,%s\n", sa, sb, sc);
a *= weight(sa);
b *= weight(sb);
d = c * a / b;
printf("Case %d: %0.2lfpx\n", cas, d);
}
return 0;
}
我是暴过的,就不贴代码了.
列出式子,或者画个图看看,大致可以看出随着点从负无穷到正无穷移动,距离和是先变小后增大的,是个单峰函数,于是可以用三分法求极值。
#include
#include
#include
#include
const int MAXN = 1e5;
const double eps = 1e-8;
struct Point{
double x, y;
};
Point a[MAXN];
int n;
int dblcmp(double a, double b){
if (a - b > eps) return 1;
if (b - a > eps) return -1;
return 0;
}
double dis(Point a, Point b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double TotDis(double x){
Point k = {x, 0};
double ret = 0;
for (int i = 0; i < n; i++)
ret += dis(k, a[i]);
return ret;
}
int main(){
int i, j, k, cas, T;
double low, high, midl, midr;
scanf("%d", &T);
for (cas = 1; cas <= T; cas++){
scanf("%d", &n);
for (j = 0; j < n; j++)
scanf("%lf %lf", &a[j].x, &a[j].y);
low = high = a[0].x;
for (i = 1; i < n; i++){
if (low > a[i].x) low = a[i].x;
if (high < a[i].x) high = a[i].x;
}
while(dblcmp(low, high) < 0){
midl = (high - low) / 3 + low;
midr = (high - low) * 2 / 3 + low;
double vl = TotDis(midl);
double vr = TotDis(midr);
if (vl > vr){
low = midl;
}else if (vl < vr){
high = midr;
}else{
low = midl;
high = midr;
}
}
printf("Case %d: %0.6lf\n", cas, (low + high) / 2);
}
return 0;
}