Z Courses

A Simple Web Classroom

用户工具

站点工具


趣题:cpp:年龄问题

年龄问题

问题描述

已知第一个人10岁,第二个人比第一个人大2岁,以此类推,用递归算出第八个人几岁?

参考代码

#include <iostream>
using namespace std;
int age(int n)
{
	if(n==1)
		return 10;
	else 
	    return age(n-1)+2;
}


int main(){
	int n;
	cin >> n;
	cout << age(n);
}