划分数而不在c。使用除法运算符 [英] dividing a number without using division operator in c

查看:77
本文介绍了划分数而不在c。使用除法运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以把一个未知号码的数量不使用这些操作符('*','/','%')。分母运行期间给出。

How can I divide a number with an unknown number without using these operators('*', '/', '%'). Denominator is given during runtime.

推荐答案

您可以使用此功能。

int divide(int nu, int de) {

    int temp = 1;
    int quotient = 0;

    while (de <= nu) {
        de <<= 1;
        temp <<= 1;
    }

    //printf("%d %d\n",de,temp,nu);
    while (temp > 1) {
        de >>= 1;
        temp >>= 1;

        if (nu >= de) {
            nu -= de;
            //printf("%d %d\n",quotient,temp);
            quotient += temp;
        }
    }

    return quotient;
}

您可以传递分子和分母这个功能,获得所需的商数。

You can pass a numerator and a denominator to this function and get the required quotient.

这篇关于划分数而不在c。使用除法运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆