将数字除以循环直到满足条件= 0

时间:2017-06-01 20:35:20

标签: c++ divide

我必须在程序中输入一个值并将其除以4,直到达到数字0.但是当我运行它时,它不会停在0,它会一直重复0。代码有什么问题?

#include <iostream>

using namespace std;

int main(){
    double input;
    cout << "Enter an Integer: ";
    cin >> input;
    cout << input << "/ 4 ";
    do
    {
        input = input / 4;
        if (input >= 0)
            cout <<" = "<< input << endl;
        cout <<input << " /4";
    }
    while ((input >= 0) || (input != 0));
    return 0;
}

1 个答案:

答案 0 :(得分:1)

这是我的三分钱。:))

#include <iostream>

int main() 
{
    const long long int DIVISOR = 4;

    while ( true )
    {
        std::cout << "Enter an Integer (0 - Exit): ";

        long long int n;

        if ( not ( std::cin >> n ) or ( n == 0 ) ) break;

        std::cout << std::endl;

        do
        {
            std::cout << n << " / " << DIVISOR;
            n /= DIVISOR;
            std::cout << " = " << n << std::endl;

        } while ( n );

        std::cout << std::endl;
    }

    return 0;
}

程序输出可能看起来像

Enter an Integer (0 - Exit): 1000

1000 / 4 = 250
250 / 4 = 62
62 / 4 = 15
15 / 4 = 3
3 / 4 = 0

Enter an Integer (0 - Exit): 0