[Blue Bridge Cup] 2017 final square tens

Title description

The 10 numbers from 0 to 9 are not repeated or omitted, and can form many 10-digit numbers.
Many of these are exactly square numbers (the square of a certain number).
For example: 1026753849, which is the smallest square number.
Could you find out what is the largest square number among them?
Output
Output an integer to indicate the answer
Source/category


Code:

Answer: 9814072356

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
bool is(ll x) //判断十位数是否不同
{
    
    
	set<int> s;
	while(x)
	{
    
    
		s.insert(x%10);
		x/=10;	
	}	
	if(s.size()==10) return 1; //十位数都不同
	else return 0;
}

int main()
{
    
    
	int ans;
	for(ll i=9876543210;;i--)
	{
    
    
		ll x=sqrt(i);
		if(x*x==i && is(i)) //是平方数 && 十位数不一样
		{
    
    
			cout<<i; //第一个找到的就是最大的平方数
			return 0;
		}
	}
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45260385/article/details/109497825