http://oj.ecustacm.cn/problem.php?id=1341
注意精度,注意精度。用long long int型不然会溢出。
#include<cstdio>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
int a[10]={0,1,2,3,4,5,6,7,8,9};
//int a[10]={9,8,1,4,0,7,2,3,5,6};
int main(void)
{
long long int max=-1;
long long int sum;
long long int i,j;
long long int b;
do
{
sum=0;
for(i=0;i<10;i++)
{
sum=sum*10+a[i];
}
b=sqrt(sum);//b一定要long long 不然会出错
if(b*b==sum)
if(sum>max)
{
max=sum;
}
}while(next_permutation(a,a+10));
printf("%lld\n",max);
// printf("%lf\n",sqrt(max));
return 0;
}
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[10]={0,1,2,3,4,5,6,7,8,9};
long long int ans;
int main(void)
{
do
{
long long int sum=0;
for(int i=0;i<10;i++)
sum=sum*10+a[i];
long long int b=sqrt(sum);
if(sum==b*b) ans=max(ans,sum);
}while(next_permutation(a,a+10));
cout<<ans<<endl;
return 0;
}