UVA10257 Dick and Jane题解

· · 题解

题意简述

已知 Dick、Jane、Spot、Puff、Yertle 的年龄关系如下。

现在给定四个非负整数 s,p,y,j,求 Spot、Puff、Yertle 的年龄。

题目分析

由题意得:设 Yertle 的年龄是 x 岁,则 Puff、Spot 的年龄分别是 (x+p)、(x+y) 岁。

所以,x+(x+p)+(x+y)=12+j,解得 x=4+\frac{j-p-y}{3}。所以 Yertle、Puff、Spot 的年龄分别是 (4+\frac{j-p-y}{3})、(p+4+\frac{j-p-y}{3})、(y+4+\frac{j-p-y}{3}) 岁。但需要注意,输出时需要按照题目中的规则输出。

代码

#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;
}