聯萌十一大決戰之強力熱身 A. Easy Math
阿新 • • 發佈:2018-12-31
[題目連結] (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;
}