我只需要仔细检查一下我的解决方案,但我在网上找不到任何解决方案。
如果你能帮助我,我会很高兴的,因为我已经为自己设定了在假期学习c++的目标。
特别是,我需要一个功能练习的帮助,这是我到目前为止所得到的:
//(not using multiplication)
int square(int a)
{
int result = 0;
int count = 0;
while (count < a)
{
result += result;
++count;
return result;
}
}
int main()
{
int x = 0;
x = square(5);
cout << x;
}但我的输出是0。
发布于 2020-12-21 04:28:15
result是0,所以将它添加到自身中,无论多少次,都会产生0。如果您正在尝试平方a,则应将其添加到result中。此外,return语句应该在循环之外,而不是在循环内部:
int square(int a)
{
int result = 0;
int count = 0;
while (count < a)
{
result += a; // Here!
++count;
}
return result;
}https://stackoverflow.com/questions/65384436
复制相似问题