#include<iostream.h>
#include<math.h>
//------------------------------------------------------------------------------------
main()
{
unsigned a,b,c,a1,b1,turnnum,firstturnnum = 0;
//说明:
/*
1)a枚向上,b枚向下,每次翻c枚,turnnum表示最少翻动次数,firstturnnum第一次翻动a或b的枚数,
2)a1,b1分别表示第一次翻动后向上和向下枚数
3)我第一次给出答案的时候,未考虑:c>=(a+b)/2的情况,后来有几位朋友提出互补也就是叶不归朋友提出的定理1,使问题得到解决
4)DarwinZhang朋友提出的a>m>b或 b>m>a的情况,本程序可以给出答案
*/
char ch = 'a';
bool t1 = false,t2 = false;
while(ch != 'q')
{
cout<<"a = ";
cin>>a;
cout<<endl;
cout<<"b = ";
cin>>b;
cout<<endl;
cout<<"c = ";
cin>>c;
cout<<endl;
if((c == 0)||(c >= a + b))
{
cout<<"INPUT ERROR!";
continue;
}
if(c >=(a + b)/2)
{
c = a + b - c;
}
if(!fmod(a,c)&&(!fmod(b,c)))
{
turnnum = (a > b?b:a) / c;
t1 = true;
}
else
if(!fmod(a,c))
{
turnnum = a / c;
t1 = true;
}
else
if(!fmod(b,c))
{
turnnum = b / c;
t1 = true;
}
else
for(int k = 0;(k <= c)&&(k <= a)&&(c - k <= b);k++)
{
a1 = a + c - 2 * k;
b1 = b - c + 2 * k;
cout<<"("<<a1<<","<<b1<<"),";
if(!fmod(a1,c)&&(!fmod(b1,c)))
{
turnnum = (a1 > b1?b1:a1) / c + 1;
if(!t2)firstturnnum = k;
t2 = true;
}
else
if(!fmod(a1,c))
{
turnnum = a1 / c + 1;
if(!t2)firstturnnum = k;
t2 = true;
}
else
if(!fmod(b1,c))
{
turnnum = b1 / c + 1;
if(!t2)firstturnnum = k;
t2 = true;
}
}
cout<<endl;
if(t1)
{
cout<<"turn num is : "<<turnnum<<endl;
}
else
if(t2)
{
cout<<"turn num is : "<<turnnum<<endl;
cout<<"first turn num is : "<<firstturnnum<<endl;
cout<<"a1 = "<<a<<" + "<<c<<" - 2 * "<<firstturnnum<<" = "<<a + c - 2 * firstturnnum<<endl;
cout<<"b1 = "<<b<<" - "<<c<<" + 2 * "<<firstturnnum<<" = "<<b - c + 2 * firstturnnum<<endl;
cout<<"(a1/c,b1/c) = ("<<(float)(a + c -2 * firstturnnum)/c<<","<<(float)(b - c + 2 * firstturnnum)/c<<")"<<endl;
}
else
cout<<"can not finish!"<<endl;
cout<<"quit ? yes = q ";
cin>>ch;
}
}