我这里有一个程序,它总是将数字的数字相加,直到我得到一个数字.例如,给定num = 38,过程如下:3 + 8 = 11,1 + 1 = 2.由于2只有一位数,所以返回它.
问题是,返回main的值是垃圾,但是在从函数addDigits()返回值之前,它是正确的值.谁能在这看到我的错误?这让我疯狂.提前谢谢了.
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int addDigits(int num)
{
vector <int> nums;
if (num <10)
{
return num;
}
int rem, temp = 0;
while (num > 9)
{
temp = num /10;
rem = num %10;
nums.push_back(rem);
num = temp;
}
nums.push_back(num);
int total = nums[0];
for (int i = 1; i < nums.size(); i++)
{
total += nums[i];
}
cout<<"total :"<<total<<endl;
if …Run Code Online (Sandbox Code Playgroud) 在这个代码中使用 a--和b++显示分段错误,但如果给予--a和++b它的工作,为什么?!
add(a,b)
{
if (a==0)
return b;
else
return add(a--,b++); //why this line didn't work??!
}
Run Code Online (Sandbox Code Playgroud) 我的目标是以递归方式编写所有函数,以便让我看起来更聪明.为了好玩,我想出了一个字符串比较功能
#include <iostream>
int string_compare ( char * s1, char * s2 )
{
int retval;
switch ( s1 ? 1 : 0 + s2 ? 2 : 0 )
{
case 0 : retval = 1; break;
case 1 :
case 2 : retval = 0; break;
case 3 : retval = *s1 == *s2 ? string_compare(++s1,++s2) : 0; break;
}
return retval;
}
int main ( )
{
char str1 [] = "hey there",
str2 [] = "hey …Run Code Online (Sandbox Code Playgroud) 下面是代码,请指出我错在哪里.我已声明,定义了这个功能,我不知道出了什么问题.
#include<stdio.h>
int factorial(int b); /* Declaration */
int main()
{
int num;
printf("Enter number: ");
scanf("%d", &num);
printf("%d",factorial(num));
return 0;
}
int factorial(int b) /*Function definition*/
{
return b*factorial(b-1);
}
Run Code Online (Sandbox Code Playgroud) 我不明白以下代码的结果如何6被称为sum(3).有人可以解释一下吗?
public int sum(int number) {
if (number == 1) {
return number;
} else {
return number + sum(number - 1);
}
}
Run Code Online (Sandbox Code Playgroud) 这是整个来源:
#include <iostream>
void recursion(static short &di);
using namespace std;
int main()
{
short pi = 1;
for(pi; pi > 0; pi++)
{
cout << "Hi, pi!" << "\n";
recursion(pi);
}
}
void recursion(static short &di)
{
di++;
if(di < 20)
{
return;
}
else
{
cout << di << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当16位容器不是静态的时,它工作正常,但我希望它是静态的,并且它给出以下错误:
main.cpp:2:29:错误:存储类说明符在参数声明中无效void recursion(static short di); ^ main.cpp:2:29:错误:为参数'di'main.cpp指定的存储类:14:29:错误:存储类说明符在参数声明中无效void recursion(static short di)^ main.cpp:14: 29:错误:为参数'di'指定的存储类
我在编写用于计算n的代码时遇到问题!没有递归.我知道如何在循环中执行它,但我不知道如何非递归地执行它.
procedure factorial
if n = 1 or n = 0
return 1
if n>1
return(n*factorial(n-1))
end
Run Code Online (Sandbox Code Playgroud) 将:
int foo();
int foo()
{
return foo();
}
Run Code Online (Sandbox Code Playgroud)
永久循环或只运行两次函数(一次隐式,一次一次终止).
为二分查找创建递归函数.
此函数接受已排序的数组和要搜索的项,并返回项的索引(如果项在数组中),或返回-1(如果项不在数组中).
此外,编写测试程序来测试您的功能.
template <class elemType>
int orderedArrayListType<elemType>::binarysearch
(const elemType& item) const
{
int first= 0;
int last = length -1;
int mid;
int list[];
int BinarySearch(,Type & Item, int first, int last)
bool found = false;
while (first <= last && !found){
mid = (first + last) / 2;
if (list[mid] > item)
return BinarySearch(list, item, first, mid -1)
found = true;
else if (list[mid] > item)
return BinarySearch( list, item, first, mid -1)
last = mid - 1; …Run Code Online (Sandbox Code Playgroud) 我不想使用while或for循环,只想使用递归来返回给定列表中的奇数.谢谢!
说我有这个对象:
{
"prop1":"Hello",
"prop2":{
"prop1":{
"prop1":"Tablecloth",
"prop2":"Indians"
},
"prop2":"JuicyJuice"
},
"prop3":"Sponge",
"prop4":{"Bob":"Squarepants"}
}
Run Code Online (Sandbox Code Playgroud)
我想要一个将返回的递归函数HelloTableclothIndiansJuicyJuiceSpongeSquarepants.
无论我把它放在什么对象上,我都希望它循环直到它获得所有字符串并将它们全部添加起来.
谢谢!
我是c#的新手,所以请忽略我的问题,我很困惑我的递归函数是正确的,但代码显示错误.请帮忙
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
void Print100(int n)
{
if (n >= 100)
{
Console.WriteLine();
return;
}
Console.WriteLine(n);
Print100(n + 1);
Console.WriteLine(n);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个函数来使用递归显示1-100和100-1整数.