枚举,由已知条件推得y大于k,小于等于2K
AC代码:
#include"iostream"
#include"cstring"
using namespace std;
const int maxn=20002;
int a[maxn];
int b[maxn];
int main()
{
int i,y;
int x,f,k;
while(cin>>k&&k)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
f=0;
for(y=k+1;y<=2*k;y++)
{
if(((k*y)%(y-k))==0)
{
a[f]=(k*y)/(y-k);
b[f]=y;
f++;
}
}
cout<<f<<endl;
for(int j=0;j<f;j++)
{
cout<<"1/"<<k<<" = "<<"1/"<<a[j]<<" + "<<"1/"<<b[j]<<endl;
}
}
return 0;
}
疑问(未解决)
同样的代码codeblock运行正常,VC6.0错误
#include"iostream" #include"cstring" using namespace std; const int maxn=10001; int a[maxn]; int b[maxn]; int main() { double k,i,y; int x,f; while(cin>>k&&k) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); f=0; for(y=k+1;y<=2*k;y++) { i=1/(1/k-1/y); x=i; if(x-i==0) { a[f]=x; b[f]=y; f++; } } cout<<f<<endl; for(int j=0;j<f;j++) { cout<<"1/"<<k<<" = "<<"1/"<<a[j]<<" + "<<"1/"<<b[j]<<endl; } } return 0; }
codeblock:
2
2
1/2 = 1/6 + 1/3
1/2 = 1/4 + 1/4
12
8
1/12 = 1/156 + 1/13
1/12 = 1/84 + 1/14
1/12 = 1/60 + 1/15
1/12 = 1/48 + 1/16
1/12 = 1/36 + 1/18
1/12 = 1/30 + 1/20
1/12 = 1/28 + 1/21
1/12 = 1/24 + 1/24
VC6.0
2
1
1/2 = 1/4 + 1/4
12
4
1/12 = 1/84 + 1/14
1/12 = 1/36 + 1/18
1/12 = 1/28 + 1/21
1/12 = 1/24 + 1/24