Computer College Undergraduate Program Design contest (2015 ' 11) 1004 Quality Square number

Source: Internet
Author: User
1004 Quality Square number Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)Problem Description Xiaoming is naturally sensitive to the number, 3-year-old can recite pi 100 digits.

Now, Xiaoming slowly grew up, but still very like the number, recently, he was fascinated with prime numbers and the square number, and his own prime number of the square named "Quality Square."
Now he is working on the question of how much of the nearest mass is to a positive integer n.
Input input data The first line is a positive integer t (t<=20), which indicates that there is a T-group input data.
Next T line, enter a positive integer N (1<=n<=10^8) per line.
Output for each set of data, please export the closest to the number of the mass square, each set of output in one row.

Sample Input
2 1 10
Sample Output
4 9

 #include <iostream> #include <cmath> using namespace std; #define MAX 10000 int a[max+1]= {
1,1,0}; int main () {for (int i=2; i<=5000; i++) for (int j=i<<1; j<max+1; j+=i) {A
        [J]=1;
    } int T;
    int N;
    cin>>t;
        while (t--) {cin>>n;
        int temp=sqrt (N);
        if (temp*temp = = N && a[temp]==0) cout<<n<<endl;
        else if (temp==1) cout<<4<<endl;
            else {int I, J;
            For (I=temp, i>=2; i--) {if (a[i]==0) break;
            For (j=temp+1 j<=max; j + +) {if (a[j]==0) break;
            } if (N-i*i <= j*j-n) cout<<i*i<<endl;
        else cout<<j*j<<endl; }
    }
}

Always looking at once space in a daze, those who say not separate friends, turn around, strangers. Familiar, quiet, quiet, left, left, unfamiliar, unfamiliar, disappeared, disappeared, strangers.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.