问题C ++我需要帮助 [英] problem c++ i need help

查看:56
本文介绍了问题C ++我需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好.希望我发布的主题正确.您能帮我解决c ++问题吗?
问题出在这里:
编写一个递归子程序,该程序计算两个自然数a和b的乘积,将a的和重复b次(a * b = a + a + a + a + a + ... + a).

预先谢谢您.

Hello.I hope I posted in the right topic.Can you help me with a c++ problem?
Here''s the problem:
Write a recursive subprogramme which calculates the product of two natural numbers,a and b,repeating the sum of a for b times.(a*b=a+a+a+a+...+a).

Thank you in advance.

推荐答案

首先,它甚至不会编译. suma方法的主体中包含无效的代码.

其次,您在suma方法中的逻辑是错误的.

我可以轻松地给您代码,但是您会学到什么?
First, that won''t even compile. The body of your suma method has invalid code in it.

Second, your logic is wrong in the suma method.

I could easily give you the code, but what would you learn as a result?


我认为我还没有删除任何内容.无论如何,这是代码:
I think I haven''t deleted anything.Anyway,this is the code:
# include<iostream.h>
int suma(int a,int b)
{
    if(b==1)return a;
    else return suma(a,b-1)+a;
}
void main ()
{ int a,b;
 cout<<"a=";cin>>a;
 cout<<"b=";cin>>b;
 cout<<suma(a,b);
}


这篇关于问题C ++我需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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