- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
此处使用的所有其他函数(checkstrong 和 factorial)工作得很好。这个 deletestrong 函数最终会删除数组中的所有元素。
int deletestrong(int arr[],int n,int current){
if(current >= n){
return n;
}
if(checkstrong(arr[current]==1)){
if(current== n-1){
n--;
}
else {
for(int i=current; i< n-1 ;i++){
arr[i]=arr[i+1];
}
n--;
}
}
else{
current++;
}
deletestrong(arr,n,current);
}
驱动程序功能
int main(){
int n;
int arr[20];
printf("Enter number of elements.\n");
scanf("%d",&n);
printf("Enter the array.\n");
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printarray(arr,n);
n=deletestrong(arr,n,0);
printf("After deleting all the strong numbers.\n");
printarray(arr,n);
}
最佳答案
对于初学者来说,第三个参数是多余的。该函数可以用不同的方式编写。一种方法是当函数返回数组中不是强数字的元素数时。
你的函数实现至少有未定义的行为,因为当控制传递给这个语句时函数不返回任何东西
//...
deletestrong(arr,n,current);
}
这个if语句
if( checkstrong( arr[current] == 1 ) ){
有一个逻辑错误。你的意思是相反
if( checkstrong( arr[current] ) == 1 ){
函数可以这样写
size_t deletestrong( int a[], size_t n )
{
size_t non_strong_numbers = 0;
if ( n )
{
if ( checkstrong( *a ) )
{
for ( size_t i = 1; i != n; i++ ) a[i-1] = a[i];
}
else
{
non_strong_numbers = 1;
++a;
}
non_strong_numbers += deletestrong( a, n - 1 );
}
return non_strong_numbers;
}
并称呼为
size_t n;
int arr[20];
printf("Enter number of elements.\n");
scanf( "%zu",&n );
//...
n = deletestrong( arr, n );
//...
关于c - 这个使用 RECURSION 删除给定数组中所有强数字的函数有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58306014/
虽然我在理解递归方面没有任何问题,但我似乎无法理解汉诺塔问题的递归解决方案。这是来自 Wikipedia 的代码: procedure Hanoi(n: integer; source, dest,
虽然我在理解递归方面没有任何问题,但我似乎无法理解汉诺塔问题的递归解决方案。这是来自 Wikipedia 的代码: procedure Hanoi(n: integer; source, dest,
The Third Commandment的 The Little Schemer状态: When building a list, describe the first typical elemen
编辑 有关映射递归的“正确”Groovy 式方法,请参阅下面的@tim 解决方案。由于 Map findRecursive 在 Groovy 中尚不存在,如果您发现自己在应用程序的各个部分都需要此功能
这是尝试求解 3*3 的线性方程并打印结果,但在注释行中遇到了问题: 我在程序外部定义了 LinearSolution 模块,我应该在程序内部定义它吗?有什么区别? 为什么说该语句是递归的,你知道,当
我正在学习 Clojure 并从复制 Python 程序的功能开始,该程序将通过遵循(非常简单的)隐马尔可夫模型来创建基因组序列。 一开始,我坚持使用我已知的串行编程方式并大量使用 def 关键字,从
我有一个记录: type node = { content : string; parent : node option;
我发现 Java 8 已经显着清理了将文件内容读取到字符串中的过程: String contents = new String(Files.readAllBytes(Paths.get(new URI
我目前正在用 Java 编写一个图形库,我想要一个工具来可视化一些图形。我发现了 Graph-viz,它恰好是一种很好的(尽管有缺陷)做到这一点的方法。 在我的模型中,图由节点和边组成。每个节点都有一
昨天我遇到了这个pipes Common Lisp 库。它在某种程度上看起来很像 clojure 的惰性序列抽象,因此我决定使用它来实现 Common Lisp 中递归惰性斐波那契序列定义的经典(且优
昨天我遇到了这个pipes Common Lisp 库。它在某种程度上看起来很像 clojure 的惰性序列抽象,因此我决定使用它来实现 Common Lisp 中递归惰性斐波那契序列定义的经典(且优
我在开发一个递归函数时遇到了问题,该函数将查看两个列表是否彼此相等,包括查看子列表。到目前为止,我有: (defun are-equal2 (X Y) (cond ((null X) nil)
在 Abelson/Sussman 的经典著作《计算机程序的结构和解释》中,在关于树递归和斐波那契数列的第 1.2.2 节中,他们展示了这张图片: 计算第 5 个斐波那契数时生成的树递归过程 然后他们
SICP中的Section 1.2.1 中的作者在下面给出了这样的代码示例,以显示如何使用迭代过程解决阶乘问题: (define (factorial n) (fact-iter 1 1 n))
我继承了 的遗产Fortran 77 我现在的代码 试试 前往 编译 Fortran 2003 标准。我对 Fortran (我知道 C 和 Python)一无所知,我正在学习它。 下面的代码片段会导
这个警告来自哪里: Warning: `recursive` is deprecated, please use `recurse` instead 我在这里看到过:https://r-pkgs.or
Section 2.2 of the Happy user manual建议您使用左递归而不是右递归,因为右递归是“低效的”。基本上他们是说,如果您尝试解析一长串项目,右递归将溢出解析堆栈,而左递归使
问题 我有一个递归 CTE 查询,但是在创建循环时它失败了。我已经修复了简单的循环(例如 1 -> 2 -> 1),但无法修复更复杂的循环(例如 1 -> 2 -> 3 -> 2)。 查询详情 测试表
看完麻省理工学院的动态规划讲座后,我想练习一下斐波那契数列。我首先编写了朴素的递归实现,然后添加了内存。这是内存版本: package main import ( "fmt" ) func f
按照以下步骤,Cloudformation 堆栈可以进入递归锁: 在不导入值的情况下设置 CF(并创建堆栈) 使用相同的 CF 模板创建 soms 输出值(并更新堆栈) 在同一 CF 模板(和更新堆栈
我是一名优秀的程序员,十分优秀!