To Be NUMBER ONE

Problem Description
One is an interesting integer. This is also an interesting problem. You are assigned with a simple task.<br>Find N (3 &lt;= N &lt;= 18) different positive integers Ai (1 &lt;= i &lt;= N), and<br><center><img src=http://acm.hdu.edu.cn/data/images/C344-1008-1.png></center><br><br>Any possible answer will be accepted.
 
Input
No input file.
 
Output
Your program’s output should contain 16 lines:
The first line contain 3 integers which are the answer for N = 3, separated by a single blank.
The following 15 lines are the answer for N = 4 to 18, as the same format.
The sample output is the first two lines of a possible output.
 
Sample Output
2 3 6
2 4 6 12
题目要注意,分解出来的数要小于个数加1的平方;
算法思想 :

对分母进行分解:

n可以分解成 1/n = 1/(n+1) + 1/(n+1)*n

例如 从2 3 6 开始

不能有相同的 所以只能从 2分解成 3 6 前面已经含有了,然后只能从3开始改,每次从最小的开始分解。

下一项变成 2 4 6 12

下一项不能分解 2因为会多出来6 所以应当选择 2 5 6 12 20

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{int a[400],i,j,flag;
memset(a,0,sizeof(a));
a[2]=1;
a[3]=1;
a[6]=1;
printf("2 3 6 ");
i=4;


while(i<=18)
{i++;
for(j=2;j<i*i;j++)
{
if(a[j]&&!a[j+1]&&!a[(j+1)*j])
{
a[j]=0;
a[j+1]=1;
a[(j+1)*j]=1;
break;
}
}
flag=1;
for(j=2;j<(i+1)*(i+1);j++)
{
if(a[j])
{


if(flag)
{
printf("%d",j);
flag=0;
}
else
printf(" %d",j);
}
}
printf(" ");

}


return 0;
}

原文地址:https://www.cnblogs.com/1314wamm/p/4993109.html