小编lif*_*rah的帖子

C++中递归的完整虚拟

嗨,大家好我最近学习了递归,我尝试了很多返回类型,似乎我在过去的一两天里一直在努力解决这个问题.可悲的是,我没有运气.

这个想法是:

  1. 我输入一个值和基数
  2. 找到它的第一个余数并将其存储在一个字符串中.
  3. 然后将值除以基数以获得新值
  4. 重复进程直到值为0或1,然后返回整个字符串.

码:

string convert(int value, int b){
    stringstream s;
    //Once the value is 0 or 1 it returns the whole result
    if(value ==0 || value ==1)
        return s.str();
    else
    {
        //I store the remainder results in the stringstream and call the function again
        s<<convert(value/b,b)<<value%b;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ recursion

0
推荐指数
1
解决办法
122
查看次数

标签 统计

c++ ×1

recursion ×1