题意简述
已知 Dick、Jane、Spot、Puff、Yertle 的年龄关系如下。
- Dick 的年龄是 12 岁(即 Dick 出生至少 年且不超过 年),Jane 的年龄是 岁。
- 当 Puff 出生时,Spot 的年龄是 岁。
- 当 Yertle 出生时,Puff 的年龄是 岁。
- 当 Yertle 出生时,Spot 的年龄是 岁。
- Spot、Puff 和 Yertle 的年龄相加,等于 Dick 和 Jane 的年龄和。
现在给定四个非负整数 ,求 Spot、Puff、Yertle 的年龄。
题目分析
由题意得:设 Yertle 的年龄是 岁,则 Puff、Spot 的年龄分别是 、 岁。
所以,,解得 。所以 Yertle、Puff、Spot 的年龄分别是 、、 岁。但需要注意,输出时需要按照题目中的规则输出。
代码
#include <iostream>
using namespace std;
int s,p,y,j;
int main(){
while (cin >> s >> p >> y >> j){
int tmp = j - y - p + 12,x = tmp / 3;
if (tmp % 3 == 0){
cout << x + y << ' ' << x + p << ' ' << x << endl;
}
else if (tmp % 3 == 1){
if (s + p == y) cout << x + y + 1<< ' ' << x + p << ' ' << x << endl;
else cout << x + y << ' ' << x + p + 1 << ' ' << x << endl;
}
else{
cout << x + y + 1 << ' ' << x + p + 1 << ' ' << x << endl;
}
}
return 0;
}