sqrt 的顶部和底部数字

Top and bottom numbers of sqrt

所以我被卡住了,或者我将自己与以下内容混淆了: 我需要使用 for 循环来计算正整数平方根的顶部和底部数字

即:

Enter Num: 10
Top is 4
Bottom is 3

Enter Num: 16
Top is 4
Bottom is 3

Enter Num: 8
Top is 3
Bottom 2 

编辑:

我有

for(int top =1;top >=num; top++)

top >=num去那里吗?我知道 10^(1/2)3.16

还有顶部和底部是怎么找到的?我不知道 sqrt(10) 顶部和底部是 4 和 3...这是分数还是简化的正方形?我对这个问题很困惑。

根据这里的帮助就是答案

for(int top = 1; top <=num  ; top++) 
{
   if( top * top >= num)
   {
        cout << "Top is " << top ;
        cout << "\nBottom is " << (top-1) << endl;
        top =num +1;
    }
}

您可以循环遍历整数,直到您通过平方根:

int bottom = 0;
int top = 0;

for (int i = 1; i <= num; ++i) {
    if (i * i > num) {
        top = i;
        break;
    }
    bottom = i;
}