Description
求一个数开根号,有多种方法,函数法、二分法、迭代法等等
出此题可测试不同方法的效率
Input
第一行整数n,代表下面有n行待开根号的整数(106 < n < 107)
Output
输出n行,结果保留0位小数(四舍五入或使用printf("%.0f",ans);)
Sample Input
5
1
2
3
4
5
Sample Output
1
1
2
2
2
该回答引用ChatGPT
在上述代码中,我们首先读入整数 n,代表下面有 n 行待开根号的整数。然后,对于每个整数,我们使用 sqrt() 函数求出它的平方根,然后使用 round() 函数对结果四舍五入输出。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
double ans = sqrt(x);
cout << round(ans) << endl; // 四舍五入输出
}
return 0;
}