Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square.
A number x is said to be a perfect square if there exists an integer y such that x = y2.
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array.
The second line contains n integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — the elements of the array.
It is guaranteed that at least one element of the array is not a perfect square.
Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.
Input
2
4 2
Output
2
Input
8
1 2 4 8 16 32 64 576
Output
32
In the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2.
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; int check(int t) { if(t < 0)return 1; int i = 0; while(i <= t) { if(i * i == t)return 0; else if(i * i > t)return 1; i ++; } } int main() { int n,m = -100000000; cin>>n; int d; for(int i = 0;i < n;i ++) { cin>>d; if(d > m && check(d))m = d; } cout<<m; }
如果觉得有帮助,点个推荐啦~