题解:UVA10257 Dick and Jane

· · 题解

前言

题目传送门

看懂题目的人,英语至少过了十级。

题目大意

问:Spot the dog、Puff the cat 和 Yertle the turtle 多大了?

题目简化

设 Spot the dog 是 X 岁,Puff the cat 是 Y 岁,Yertle the turtle 是 Z 岁。

XYZ 各是多少。

思路

根据以上题意,列出式子:

\begin{aligned} X+Y+Z&=(Z+y)+(Z+p)+Z\\ &=3 \times Z+p+y \end{aligned}

代入 X+Y+Z=12+j 中,得到:

3 \times Z+p+y=12+j\\

简化得:

Z=\frac{12+j-p-y}{3}

将上式代入 Y=Z+p,得到:

Y=\frac{12+j-p-y}{3}+p

将上式代入 X=Z+y,得到:

X=\frac{12+j-p-y}{3}+y

::::warning[警告1]{open} 求出了 XYZ,如果 (j-p-y) \bmod 3 \ne 0 需要特判。

特判时可能 j-p-y<0,注意先加再模,特判时写成这样:(j-p-y+300) \bmod 3。 :::: ::::warning[警告2]{open} 本题有多组数据! ::::

代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int s,p,y,j;
    while(cin>>s>>p>>y>>j){
        // 多组数据 
        int X=(j-y-p+12)/3+y;
        int Y=(j-y-p+12)/3+p;
        int Z=(j-y-p+12)/3;
        // X,Y,Z的答案 
        int tepan=(j-y-p+300)%3;
        // 特判

        // if(tepan==0)
        //     直接除,不需特判 
        if(tepan==1){
            // 比较复杂的情况 
            if(s+p==y){
                // 抵消
                X++; 
            }
            else
                Y++;
        }
        if(tepan==2){
            X++;
            Y++;
        }
        cout<<X<<" "<<Y<<" "<<Z<<endl;
    }
    return 0;
}