联萌十一大决战之强力热身 A. Easy Math

[题目链接] (http://www.bnuoj.com/v3/contest_show.php?cid=6865#problem/0)
题目描述:
A. Easy Math
Time Limit: 2000msMemory Limit: 65536KB 64-bit integer IO format: %lld Java class name: Main
Submit Status
Given n integers a1,a2,…,an, check if the sum of their square root a1−−√+a2−−√+⋯+an−−√ is a integer.
Input
The input consists of multiple tests. For each test:

The first line contains 1 integer n (1≤n≤105).
The second line contains n integers a1,a2,…,an (0≤ai≤109).

Output
For each test, write ‘‘Yes′′ if the sum is a integer, or ‘‘No′′ otherwise.

Sample Input
2
1 4
2
2 3
Sample Output
Yes
No

言简意赅:
此题是要求n个数的平方根的和是否为一个整数,直接计算求解即可,不需要有太多的其他方面的顾虑,水题~
代码实现如下:

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int a[100010];
int main()
{
    int n;
    double sum;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=(double)sqrt(a[i]);//此处一定要加上类型转换成double,否则数据在存储时会出现错误
        }
        //cout<<"sum="<<(long long)sum<<endl;
        if(sum==(long long)sum)
        {
            printf("Yes\n");
        }
        else printf("No\n");
    }
    return 0;
}

你可能感兴趣的:(Math)