分别记Spot、Puff、Yertle的年龄为a1、a2、a3。由题意列方程组:
①a1 = s + a2 + b1(Spot was s years old when Puff was born)
②a2 = p + a3 + b2(Puff was p years old when Yertle was born)
③a1 = y + a3 + b3(Spot was y years old when Yertle was born)
④a1 + a2 + a3 = 12 + j(The sum of Spot's age, Puff's age, and Yertle's age equals the sum of Dick's age (12) and Jane's age (j))
其中b1,b2,b3为待定常数,它们只能取0或1。枚举b1, b2, b3即可。
#include<stdio.h>
int s,p,y,j;
bool solve(int b1, int b2, int b3)
{
int a1 = 0, a2 = 0, a3 = 0 ;
if(y + b3 == s + p + b1 + b2)
{
a2 = j - s - b1 + p + b2;
if(a2 % 3 == 0 )
{
a2 = a2 / 3 ;
a1 = s + a2 + b1;
a3 = a2 - p - b2;
if(a1 + a2 + a3 == j)
{
printf("%d %d %d/n" , a1, a2, a3);
return true ;
}
}
}
return false ;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1110.txt","r" ,stdin);
#endif
while(scanf("%d%d%d%d",&s,&p,&y,&j)!= EOF)
{
j += 12 ;
if(solve(0, 0, 0) || solve(0, 0, 1) ||
solve(0, 1, 0) || solve(0, 1, 1) ||
solve(1, 0, 0) || solve(1, 0, 1) ||
solve(1, 1, 0) || solve(1, 1, 1 ));
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}
int s,p,y,j;
bool solve(int b1, int b2, int b3)
{
int a1 = 0, a2 = 0, a3 = 0 ;
if(y + b3 == s + p + b1 + b2)
{
a2 = j - s - b1 + p + b2;
if(a2 % 3 == 0 )
{
a2 = a2 / 3 ;
a1 = s + a2 + b1;
a3 = a2 - p - b2;
if(a1 + a2 + a3 == j)
{
printf("%d %d %d/n" , a1, a2, a3);
return true ;
}
}
}
return false ;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1110.txt","r" ,stdin);
#endif
while(scanf("%d%d%d%d",&s,&p,&y,&j)!= EOF)
{
j += 12 ;
if(solve(0, 0, 0) || solve(0, 0, 1) ||
solve(0, 1, 0) || solve(0, 1, 1) ||
solve(1, 0, 0) || solve(1, 0, 1) ||
solve(1, 1, 0) || solve(1, 1, 1 ));
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}