我也写了个:
#include <iostream>
#include <cmath>
using namespace std;
const int MAX=50;
int a[MAX]={0};
bool Sqn(int n)//判断是不是平方数
{
if(floor(sqrt(n))*floor(sqrt(n))==n)
return true;
return false;
}
int main()
{
// cout<<Sqn(9)<<endl;
int N;
int flag=1;
while(flag)//输入
{
cout<<"Please input the number of stick(N):"<<endl;
cin>>N;
if(N>=1 && N<=50)
flag=0;
else
cout<<"The number your input is boundary!"<<endl;
}
int cgd=1;//the candiedgound number
int flg=1;
while(flg)//put candiedgound
{
for(int i=0;i<N;i++)
{
if(a[i]==0 || Sqn(a[i]+cgd))
{
a[i]=cgd;
cgd++;
i=-1;//从零开始找
}
}
cgd--;//减去不满足条件的最后一次加一
flg=0;
}
cout<<"The largest number is:";
cout<<cgd<<endl;
return 0;
}