English 中文(简体)
不能获得补救职能的正确产出[封闭]
原标题:cannot get the right output of a recursive function [closed]
  • 时间:2011-11-21 16:08:00
  •  标签:
  • c++
  • g++

我在这项职能中做了哪些错误。 我确信,问题在于<条码> = 支出(基数,pwr /= 2)*(基数,pwr /= 2);,但我不能认为合乎逻辑的理由。 是否有办法写出像这样的参数? 预先感谢。 页: 1 是错误的

#include <iostream>
using namespace std;

unsigned long& exp(unsigned long& base, unsigned long& pwr)
{
    if(pwr == 0)
      base = 1;
    else if(pwr == 1)
      base = base;
    else
      base = exp(base, pwr /= 2) * exp(base, pwr /= 2);
    return base;
}

int main()
{
    unsigned long n=2, m = 4;
    cout << exp(n,m) << endl;
    return 0;
}    
最佳回答

Here are five things to note about this line:

  base = exp(base, pwr /= 2) * exp(base, pwr /= 2);
  1. As noted in a comment above, base is passed by reference, not value, so there is only one copy of it and you re changing its value. This is a bad idea.
  2. pwr is also passed by reference and you re changing its value when you use /= instead of just /. There are two /= statements in this line, so after this line runs, pwr now has one fourth its original value.
  3. The exp function will get run twice each time you run this line. It would make more sense to store the value and then square it.
  4. /2 is integer division, so it will round down. So if you give it a number like 3 as an exponent, it will not work correctly because 3/2 is 1. If you correct the other mistakes and the call it with an exponent of 7, it will end up only doing exp(2,7) = exp(2,3)*exp(2,3) = exp(2,1)*exp(2,1)*exp(2,1)*exp(2,1) = 16 when obviously the correct answer is 128. This function, as designed, will only work correctly when the exponent is a power of 2.
  5. Good thing that number 4 is true because if you did get exp(2,1.5) you d never terminate since it wouldn t match your base cases. You should probably rethink your algorithm generally.
问题回答

The first problem that I can see is that you pass base and pwr by reference. When you do this, their global value gets modified every time yo call exp, so the output you are getting should be expected based on the code you wrote.

要取得正确结果,我将取代

exp(base, pwr /= 2) * exp(base, pwr /= 2);

iii

exp(base, pwr/2) * exp(base, pwr/2);

because your exp(2, 4) = exp(2, 2) * exp(2, 1) is not really correct..

Since you claim this isn t homework:

  1. 正如其他人提到的那样,这里的提及并不属实。 每次复程电话在计算中都需要这一特定点的价值。

  2. exp (base, pwr /= 2) * 支出(基数,pwr /= 2);

    This won t work as expected when your exponent is not a multiple of 2. After you fix the reference thing, if you still really want to do it this way, try:

    exp(base, pwr/2) * 支出(基准,(pwr/2 + pwr%2));

We can step through this fairly easily, the first time through exp base is 2 and pwr is 4 so we call with exp (2, 1) * exp (2, 0) (remember you are setting pwr = pwr / 2). So the first one evaluates to 2 and the second evaluates to 1 so you are returning the result of 2 * 1 which is 2. I think you re code means to return the result of exp (2, 1) * exp (2,1) instead.

这非常奇怪。 对我来说,你的法典在逻辑上是正确的,而且非常正确。 我同意你的看法,即问题何在。 由于缺乏我自己,我建议你尝试该方案的简单数字(N=1, m=1;N=4, m=1;N=2, m=1;N=2,m=0)。 如果是这样的话,问题似乎完全在于你指出的。

我知道,这并没有解决你的问题,而是一开始(如果你已经这样做的话)。 我会因为Im insane也试图这样做。

else {
  power = power-1;
  base = base * exp(base, power);
}

我不敢肯定,这种危险是否有助于(肯定会放缓),但也许值得尝试? 我也将删除许多提法。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签