今日「每日一题」来啦!历年真题题目:荒岛探测。还没有加入专属刷题群的小伙伴,记得扫码哟~(每天会在群里发蓝桥杯历年真题)
1、Java组
import java.util.*;
public class Main {
private static final double eps = 1e-6;
private static double xa;
private static double ya;
private static double xb;
private static double yb;
private static double L;
private static double A;
private static double ans;
private static double[] x = new double[6];
private static double[] y = new double[6];
private static double[] Y = new double[6];
private static double dist(double a, double b, double c, double d) {
return Math.sqrt((c - a) * (c - a) + (d - b) * (d - b));
}
private static double get(double now, int pos1, int pos2) {
if (Math.abs(x[pos1] - x[pos2]) <= eps && Math.abs(x[pos1] - now) <= eps) {
return 1002;
}
if (now <= Math.min(x[pos1], x[pos2]) || now >= Math.max(x[pos1], x[pos2])) {
return 1002;
}
return (y[pos2] - y[pos1]) / (x[pos2] - x[pos1]) * (now - x[pos2]) + y[pos2];
}
private static boolean ok1(double now) {
double xx = (xa + xb) / 2;
double yy = (ya + yb) / 2;
double a = L / 2;
double c = dist(xa, ya, xb, yb) / 2;
double b = Math.sqrt(a * a - c * c);
double mid = 1.0 - (now - xx) * (now - xx) / (a * a);
if (mid <= 0) {
return false;
}
mid = Math.sqrt(mid) * b;
Y[1] = yy - mid;
Y[2] = yy + mid;
return true;
}
private static boolean ok2(double now) {
int cnt = 2;
for (int i = 1; i <= 3; i++) {
int nex = i + 1 == 4 ? 1 : i + 1;
double mid = get(now, i, nex);
if (mid - 1000 > eps) {
continue;
}
if (cnt == 3 && Math.abs(mid - Y[cnt]) <= eps) {
continue;
}
Y[++cnt] = mid;
}
if (cnt != 4) {
return false;
}
return true;
}
private static double get_angle(double a, double b, double c, double d) {
double now = Math.acos((c - a) / dist(a, b, c, d));
if (d - b < 0) {
return 2 * Math.acos(-1) - now;
}
return now;
}
private static void init() {
A = get_angle(xa, ya, xb, yb);
double dis1 = dist(0, 0, xa, ya);
double dis2 = dist(0, 0, xb, yb);
double S1 = get_angle(0, 0, xa, ya) - A;
double S2 = get_angle(0, 0, xb, yb) - A;
xa = dis1 * Math.cos(S1);
ya = dis1 * Math.sin(S1);
xb = dis2 * Math.cos(S2);
yb = dis2 * Math.sin(S2);
}
private static void change(int pos) {
double dis = dist(0, 0, x[pos], y[pos]);
double S = get_angle(0, 0, x[pos], y[pos]) - A;
x[pos] = dis * Math.cos(S);
y[pos] = dis * Math.sin(S);
}
private static double calc(double now) {
if (!ok1(now)) {
return 0;
}
if (!ok2(now)) {
return 0;
}
double ma = Math.max(Math.min(Y[1], Y[2]), Math.min(Y[3], Y[4]));
double mi = Math.min(Math.max(Y[1], Y[2]), Math.max(Y[3], Y[4]));
if (mi - ma <= eps) {
return 0;
}
return mi - ma;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
xa = cin.nextDouble();
ya = cin.nextDouble();
xb = cin.nextDouble();
yb = cin.nextDouble();
L = cin.nextDouble();
init();
for (int i = 1; i <= 3; i++) {
x[i] = cin.nextDouble();
y[i] = cin.nextDouble();
change(i);
}
if (L <= eps || dist(xa, ya, xb, yb) >= L) {
System.out.print("0.00n");
return;
}
for (double i = -1000; i <= 1000; i += 0.001) {
ans += calc(i) * 0.001;
}
System.out.printf("%.2fn", ans);
}
}
2、C/C++组
using namespace std;
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
double xa, ya, xb, yb, L, A, ans;
double x[6], y[6], Y[6];
double dist(double a, double b, double c, double d){
return sqrt((c - a) * (c - a) + (d - b) * (d - b));
}
double get(double now, int pos1, int pos2){
if(fabs(x[pos1] - x[pos2]) <= eps && fabs(x[pos1] - now) <= eps) return inf;
if(now <= min(x[pos1], x[pos2]) || now >= max(x[pos1], x[pos2])) return inf;
return (y[pos2] - y[pos1]) / (x[pos2] - x[pos1]) * (now - x[pos2]) + y[pos2];
}
bool ok1(double now){
double xx = (xa + xb) / 2, yy = (ya + yb) / 2;
double a = L / 2 , c = dist(xa, ya, xb, yb) / 2;
double b = sqrt(a * a - c * c);
double mid = 1.0 - (now - xx) * (now - xx) / (a * a);
if(mid <= 0) return false;
mid = sqrt(mid) * b;
Y[1] = yy - mid, Y[2] = yy + mid;
return true;
}
bool ok2(double now){
int cnt = 2;
for(int i = 1; i <= 3; i++){
int nex = i + 1 == 4 ? 1 : i + 1;
double mid = get(now, i, nex);
if(mid - 1000 > eps) continue;
if(cnt == 3 && fabs(mid - Y[cnt]) <= eps) continue;
Y[++cnt] = mid;
}
if(cnt != 4) return false;
return true;
}
double get_angle(double a, double b, double c, double d){
double now = acos((c - a) / dist(a, b, c, d));
if(d - b < 0) return 2 * acos(-1) - now;
return now;
}
void init(){
A = get_angle(xa , ya , xb , yb);
double dis1 = dist(0, 0, xa, ya), dis2 = dist(0, 0, xb, yb);
double S1 = get_angle(0, 0, xa, ya) - A, S2 = get_angle(0, 0, xb, yb) - A;
xa = dis1 * cos(S1) , ya = dis1 * sin(S1);
xb = dis2 * cos(S2) , yb = dis2 * sin(S2);
}
void change(int pos){
double dis = dist(0, 0, x[pos], y[pos]);
double S = get_angle(0, 0, x[pos], y[pos]) - A;
x[pos] = dis * cos(S) , y[pos] = dis * sin(S);
}
double calc(double now){
if(!ok1(now)) return 0;
if(!ok2(now)) return 0;
double ma = max(min(Y[1], Y[2]), min(Y[3], Y[4]));
double mi = min(max(Y[1], Y[2]), max(Y[3], Y[4]));
if(mi - ma <= eps) return 0;
return mi - ma;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0) , cout.tie(0);
cin >> xa >> ya >> xb >> yb >> L;
init();
for (int i = 1 ; i <= 3 ; i ++) cin >> x[i] >> y[i] , change(i);
if(L <= eps || dist(xa, ya, xb, yb) >= L) return cout << "0.00n" , 0;
for(double i = -1000 ; i <= 1000 ; i += 0.001) ans += calc(i) * 0.001;
cout << setprecision(2) << fixed << ans << 'n';
return 0;
}
今日的「每日一题」就结束啦~
原文始发于微信公众号(蓝桥云课精选):【每日一题】蓝桥杯历年真题题解 -荒岛探测
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论