sqrt 的顶部和底部数字

Top and bottom numbers of sqrt

本文关键字:底部 数字 顶部 sqrt      更新时间:2023-10-16

所以我被困住了,或者我把自己与以下内容混淆了:我需要使用 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;
}