c - C语言递归阶乘不起作用
问题描述
对于下面显示的代码,我没有得到任何响应;我在没有条件的情况下保持最小化,例如想象人们会选择正数。除了:
进程返回 -1073741571 (0xC00000FD) 执行时间:3.194 s
如果我输入 5,答案应该是 120,而不是这里。
#include <stdio.h>
#include <stdlib.h>
int faktorijel(int x) {
return (x*faktorijel(x-1));
}
main() {
int a,b;
printf("Type in a number:");
scanf("%d\n", &a);
b=faktorijel(a);
printf("Result is %d\n", b);
return 0;
}
解决方案
你应该设置停止if
:
int faktorijel(int x){
if (x == 1) {
return 1;
} else {
return (x*faktorijel(x-1));
}
}
推荐阅读
- javascript - 如何在 sequelize 中将右外连接与 where 子句一起使用
- rust - Rust 自定义裸机编译目标:链接器需要“_start”符号并丢弃未使用的符号:如何指定自定义条目符号?
- node.js - 如何使用 Outlook api 发送 ejs
- python - Tabula-py 无法正确识别列
- apache - 安装密钥时 Apache2 语法错误 ssl
- asp.net-core - Blazor WebAssembly 中的用户管理
- testing - 端到端测试与移动应用程序交互的 Web 应用程序的最佳方法是什么
- php - 在不重新加载页面的情况下向选择列表添加新选项 - MODAL
- cisco - 带有 VLAN 的端口在 Cisco 交换机上不起作用
- node.js - 对返回完整文档的数组进行 Mongoose 查询