907A. Masha and Bears
人的体积为VV,车的大小为sizesize,人能钻进车的条件是V≤sizeV≤size,人对车满意的条件是2V≥size2V≥size. 现知道 熊爸爸能钻进最大的车并且满意 熊妈妈能钻进中等的车并且满意 熊宝宝能钻进最小的车并且满意 Masha能钻进最小的车并且只对它满意 给定四人的体积(保证V1>V2>V3),要求给出三辆车的大小。
思路: 假设三辆车大小分别为a,b,c,则有 V1≤a≤2V1//熊爸爸 V2≤b≤2V2//熊妈妈 V3≤c≤2V3//熊宝宝 V4≤c≤2V4//Masha 2V4
#include <bits/stdc++.h> using namespace std; int main() { int x1, x2, x3, x4; scanf("%d %d %d %d", &x1, &x2, &x3, &x4); int ans1 = 2 * x1; int ans2 = 2 * x2; int ans3; if (x3 == x4) { ans3 = x3; } else if (x3 > x4) { if (x3 <= 2 * x4) { ans3 = x3; } else { ans3 = -1; } } else { if (x4 <= 2 * x3) { ans3 = x4; } else { ans3 = -1; } } if (ans3 == -1) { puts("-1"); } else { if (2 * x4 < ans2) { printf("%d\n%d\n%d\n", ans1, ans2, ans3); } else { puts("-1"); } } return 0; }运行结果:
50 30 10 10 100 60 10思路: 每辆车对熊来说有下面三种可能: 不能进去的车V>C; 能进去但不喜欢的车V<2V
#include <stdio.h> #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)>(b)?(b):(a)) int main() { int father, mother, son, masha; int car1, car2, car3; scanf("%d %d %d %d", &father, &mother, &son, &masha); bool flag = false; for (car3 = max(son, masha); car3 <= min(2 * son, 2 * masha) && false == flag; ++car3) { for (car2 = max(mother, max(car3 + 1, 2 * masha + 1)); car2 <= 2 * mother && false == flag; ++car2) { for (car1 = max(father, max(car2 + 1, 2 * masha + 1)); car1 <= 2 * father && false == flag; ++car1) { flag = true; printf("%d\n%d\n%d", car1, car2, car3); } } } if (flag == false) { printf("-1"); } return 0; }运行结果:
50 30 10 10 50 30 10
更多内容请关注微信公众号
