简体   繁体   English

无法弄清楚为什么这个函数返回零

[英]Cannot figure out why this function returns zero

I have been at this for more than an hour and I still can't figure out why this function returns 0.我已经在这里工作了一个多小时,但我仍然无法弄清楚为什么这个函数返回 0。

Hopefully an extra pair of eyes can help me figure out what is going wrong.希望多一双眼睛可以帮助我弄清楚出了什么问题。

#include <bits/stdc++.h>
using namespace std;

int power(int x, int y, int p)
{
    // cout << x << " " << y << " " << p << " " << res << endl;
    if(x==0)
        return 0;
    if(y==0)
        return 0;

    long res = 1;
    if(y%2 == 0){
        res = power(x, y/2, p);
        res = (res*res)%p;
    }
    else{
        res = x%p;
        res = (res*power(x, y-1, p) % p) % p;
    }
    cout << x << " " << y << " " << p << " " << res << endl;
    return int((res+p)%p);
}

int main() {
    cout << power(2, 100000, 1000000007);
    return 0;
}

This should be changed:这应该改变:

if(y==0)
        return 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM