有四个人坐一起,问第四个人多少岁,他说比第三个人大四岁,问第三个人多少岁,他说比第二个人大四岁,问第二个人多少岁,他说比第一个人大四岁,问第一个人多少岁,他说12岁,问第四个人多少岁?要求用C语言递归实现
时间: 2024-02-03 15:02:23 浏览: 58
好的,我会用C语言递归实现解决这个问题。代码如下:
```c
#include <stdio.h>
int age(int n) {
if (n == 1) {
return 12; // 第一个人12岁
} else {
return age(n - 1) + 4; // 每个人比前一个人大4岁
}
}
int main() {
printf("第四个人的年龄是:%d岁\n", age(4));
return 0;
}
```
解析:通过递归函数 `age(n)`,可以求出第n个人的年龄。当 `n=1` 时,返回第一个人的年龄12岁;当 `n>1` 时,递归调用 `age(n-1)`,并加上4岁,即可得到第n个人的年龄。最终在 `main()` 函数中调用 `age(4)`,即可得到第四个人的年龄。
相关问题
设计一个包含5个类的Java程序,名为Person的父类有两个子类,学生类Student和员工类Employee。Employee类有两个子类,教师类Faculty和 职员类Staff。所有人都有编号ID、姓名、地址、电话号码和电子邮件地址。学生类Student有班级状态(大一、大二、大三或大四)。教师类Faculty有主讲课程、上课时间、专业信息。职员类Staff有职务、入职日期信息。覆盖每个类中的toString()方法,输出相应的类名、编号ID和姓名。 编写一个测试程序,随机创建8个Student、Faculty或Staff对象,放在一个数组中,依次调用他们的toString()方法显示信息。并说明如何用dos执行
以下是Java程序代码:
```
public class Person {
private String ID;
private String name;
private String address;
private String phoneNumber;
private String email;
public Person(String ID, String name, String address, String phoneNumber, String email) {
this.ID = ID;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getID() {
return ID;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "Person{" +
"ID='" + ID + '\'' +
", name='" + name + '\'' +
'}';
}
}
public class Student extends Person {
private String classStatus;
public Student(String ID, String name, String address, String phoneNumber, String email, String classStatus) {
super(ID, name, address, phoneNumber, email);
this.classStatus = classStatus;
}
public String getClassStatus() {
return classStatus;
}
@Override
public String toString() {
return "Student{" +
"ID='" + getID() + '\'' +
", name='" + getName() + '\'' +
'}';
}
}
public class Employee extends Person {
private String position;
private String hireDate;
public Employee(String ID, String name, String address, String phoneNumber, String email, String position, String hireDate) {
super(ID, name, address, phoneNumber, email);
this.position = position;
this.hireDate = hireDate;
}
public String getPosition() {
return position;
}
public String getHireDate() {
return hireDate;
}
@Override
public String toString() {
return "Employee{" +
"ID='" + getID() + '\'' +
", name='" + getName() + '\'' +
'}';
}
}
public class Faculty extends Employee {
private String course;
private String teachingTime;
private String major;
public Faculty(String ID, String name, String address, String phoneNumber, String email, String position, String hireDate, String course, String teachingTime, String major) {
super(ID, name, address, phoneNumber, email, position, hireDate);
this.course = course;
this.teachingTime = teachingTime;
this.major = major;
}
public String getCourse() {
return course;
}
public String getTeachingTime() {
return teachingTime;
}
public String getMajor() {
return major;
}
@Override
public String toString() {
return "Faculty{" +
"ID='" + getID() + '\'' +
", name='" + getName() + '\'' +
'}';
}
}
public class Staff extends Employee {
private String duty;
public Staff(String ID, String name, String address, String phoneNumber, String email, String position, String hireDate, String duty) {
super(ID, name, address, phoneNumber, email, position, hireDate);
this.duty = duty;
}
public String getDuty() {
return duty;
}
@Override
public String toString() {
return "Staff{" +
"ID='" + getID() + '\'' +
", name='" + getName() + '\'' +
'}';
}
}
```
以下是测试程序代码:
```
import java.util.Random;
public class TestPerson {
public static void main(String[] args) {
Person[] people = new Person[8];
Random random = new Random();
for (int i = 0; i < 8; i++) {
int type = random.nextInt(3);
String ID = "ID" + i;
String name = "Name" + i;
String address = "Address" + i;
String phoneNumber = "PhoneNumber" + i;
String email = "Email" + i;
if (type == 0) {
String classStatus = "大" + (random.nextInt(4) + 1);
people[i] = new Student(ID, name, address, phoneNumber, email, classStatus);
} else if (type == 1) {
String position = "Position" + i;
String hireDate = "HireDate" + i;
people[i] = new Staff(ID, name, address, phoneNumber, email, position, hireDate, "Duty" + i);
} else {
String position = "Position" + i;
String hireDate = "HireDate" + i;
people[i] = new Faculty(ID, name, address, phoneNumber, email, position, hireDate, "Course" + i, "TeachingTime" + i, "Major" + i);
}
}
for (int i = 0; i < 8; i++) {
System.out.println(people[i].toString());
}
}
}
```
要在DOS上执行该程序,可以依次执行以下命令:
```
javac Person.java
javac TestPerson.java
java TestPerson
```
其中,第一条命令用于编译Person类和其子类的代码,第二条命令用于编译测试程序TestPerson的代码,第三条命令用于运行测试程序。在执行第三条命令时,会输出8个Person对象的信息。
阅读全文