四、阅读程序-3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
  
int n,k;
  
int solve1(){
    int l = 0, r = n;
    while(l <= r){
         int mid = (l + r) / 2;
         if (mid * mid <= n) l = mid + 1;
         else r = mid - 1;
     }
     return l - 1;
 }
 
double solve2(double x){
     if (x == 0) return x;
     for (int i = 0; i < k; i++)
         x = (x + n / x) / 2;
     return x;
 }

 int main(){
     cin >> n >> k;
     double ans = solve2(solve1());
     cout << ans << ' ' << (ans * ans == n) << endl;
     return 0;
 }

假设 int 为32位有符号整数类型,输入的 n 是不超过47000的自然数、k 是不超过 int 表示范围的自然数,完成下面的判断题和单选题:

Scroll to Top