编写一个函数来获取一个用户的年龄(用户的输入以粗体显示) 这是一个示例
人数:3
输入人员 1 的年龄:12
输入人 2 的年龄:10
输入人员 3 的年龄:15
有 3 人,年龄分别为:12、10 和 15
我很困惑如何不使用数组来解决此类问题!请帮忙!!!
最佳答案
拉沙恩建议:
Use recursion without array.
这是一个很棒的主意。以下是您可以采取的方法。
#include <stdio.h>
void print_rec(int index, int maxindex)
{
int age;
printf("Enter age of person %d",index);
scanf("%d",&age);
if (index == maxindex)
{
printf("The ages are %d ",age);
return;
}
print_rec(index + 1, maxindex);
printf(",%d",age);
}
int main()
{
printf("Enter the number of people : ");
int num;
scanf("%d",&num);
print_rec(1,num);
return 0;
}
唯一的缺点是它以相反的顺序打印年龄。
关于C编程,For循环获取每个数字而不使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29190899/