需求描述
已知等式n+1/n = 1/x1 + 1/x2 + 1/x3,n为质数,1<=x1,x2,x3<=1000
求整数x1,x2,x3的值并按从小到大的顺序输出。
#include<iostream>
using namespace std;
int s[1000];
int flag=0;
bool isrepeat(int a,int b,int c)//判断a,b,c是否已重复存在于s[]中
{
int fly=0;
for( int i=0; i<flag; i++ )
{
if( s[i] == a )
fly++;
if( s[i] == b )
fly++;
if( s[i] == c )
fly++;
}
if( fly == 3 )
return true;
return false;
}
void save(int a,int b,int c)//保存a,b,c中不重复的值
{
int t1=0,t2=0,t3=0;
for( int i=0; i<flag; i++ )
{
if( s[i] == a )
t1=1;
if( s[i] == b )
t2=1;
if( s[i] == c )
t3=1;
}
if( !t1 )
s[flag++] = a;
if( a != b && !t2 )
s[flag++] = b;
if( a != c && b != c && !t3 )
s[flag++] = c;
}
int main()
{
/* n+1/n = 1/x1 + 1/x2 + 1/x3 n为质数 1<=x1,x2,x3<=1000 */
double n;//质数
double x1,x2,x3;
int y1,y2,y3;
while( true )
{
cin>>n;
for( x1=1 ; x1<=1000; x1++ )
{
for( x2= 1; x2<=1000; x2++ )
{
for( x3=1; x3<=1000; x3++ )
{
if( ( n+1 )*( x1*x2*x3 ) == n*( x1*x2 + x1*x3 + x2*x3 ) && !isrepeat(x1,x2,x3) )/*(n+1)/n==(1/x1+1/x2+1/x3)*/
{
cout<<"x1= "<<x1<<" "<<"x2= "<<x2<<" "<<"x3= "<<x3<<endl;
save(x1,x2,x3);
}
if( (n+1)/n > (1/x1+1/x2+1/x3) )
break;
}
}
}
flag = 0;
s[1000] = 0;
}
return 0;
}
运行结果
2
x1= 1 x2= 3 x3= 6
x1= 1 x2= 4 x3= 4
x1= 2 x2= 2 x3= 2
3
x1= 1 x2= 4 x3= 12
x1= 1 x2= 6 x3= 6
x1= 2 x2= 2 x3= 3
5
x1= 1 x2= 6 x3= 30
x1= 1 x2= 10 x3= 10
x1= 2 x2= 2 x3= 5
7
x1= 1 x2= 8 x3= 56
x1= 1 x2= 14 x3= 14
x1= 2 x2= 2 x3= 7
11
x1= 1 x2= 12 x3= 132
x1= 1 x2= 22 x3= 22
x1= 2 x2= 2 x3= 11
13
x1= 1 x2= 14 x3= 182
x1= 1 x2= 26 x3= 26
x1= 2 x2= 2 x3= 13