[問題] Babylonian algorithm
剛剛寫了一個程式
是關於猜平方根的程式
如下
#include<iostream>
using namespace std;
int main()
{
//宣告要用到的變數
int n;
double r,guess;
const double RATE=0.01;
char ans;
do
{
//介紹程式
cout<<"This program is about The Babylonian algorithm to compute the square root of a number n."<<endl;
//輸入資料
cout<<"Enter a number n: ";
cin>>n;
cout<<"Enter your initial guess: ";
cin>>guess;
//計算
while(((guess+r)/2-guess)/(guess+r)/2>RATE)
{
r=n/guess;
guess=(guess+r)/2;
}
//輸出資料
cout<<"The answer close to the square of number n is:"<<guess<<endl;
//問是否要繼續
cout<<"Do you want to continue?Press y for yes,n for no: ";
cin>>ans;
}while(ans=='y'||ans=='Y');
//結束
return 0;
}
但是問題來了
它只算一次就停了...
比如說我打n=100 guess=50
它顯示26
明明可以在算下去阿但他就是停了= =
請問是哪裡出了問題呢
請高手幫忙解答
感激
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.63.2
→
10/09 22:40, , 1F
10/09 22:40, 1F
推
10/09 22:41, , 2F
10/09 22:41, 2F
→
10/09 23:00, , 3F
10/09 23:00, 3F
→
10/09 23:00, , 4F
10/09 23:00, 4F
→
10/09 23:01, , 5F
10/09 23:01, 5F
→
10/09 23:01, , 6F
10/09 23:01, 6F
→
10/09 23:01, , 7F
10/09 23:01, 7F
→
10/09 23:01, , 8F
10/09 23:01, 8F
→
10/09 23:01, , 9F
10/09 23:01, 9F
→
10/10 01:29, , 10F
10/10 01:29, 10F