【发布时间】:2020-12-21 21:15:23
【问题描述】:
我需要编写一个函数来打印 0 或 1
-
f变量需要为 0 或 1 - 当用户输入一个数字时,例如
4321并且f变量为0,该函数应返回1,因为该数字是从右到左的升序序列,否则返回0 - 当用户输入数字时,例如
1234和f变量为 1,函数应返回 1,因为数字从左到右递增,否则返回 0
这是我的代码尝试:
#include <stdio.h>
int f2(int num, int f)
{
int x = num % 10, y = num % 100 / 10;
//if (x == y) return 0;
if (f == 0) {
if (x > y) return 0;
else {
f2(num / 10, f);
return 1;
}
}
if (f == 1) {
if (x < y) return 0;
else {
f2(num/10, f);
return 1;
}
}
}
void main()
{
int num, f;
printf("please enter number and f:");
scanf_s("%d %d", &num, &f);
printf("The result of calling f2(%d) is: %d",num, f2(num, f));
}
【问题讨论】:
-
只是想为我的英语说借口,这不是我的母语
-
你没有使用递归调用的返回值。
-
@Barmar 你是什么意思?你能详细说明一下吗?
-
f2(num / 10, f);和f2(num/10, f);。那就是那里。函数被调用,它们返回一个值,返回值没有被使用。 -
请考虑阅读此Q&A 以了解
main的返回值。