确定出生年份的奥林匹克运动员数量是多少?

时间:2016-09-19 03:03:37

标签: c++

我试图编写一个程序,询问用户他们的分娩年份,然后计算这个人经历过多少次奥林匹克运动会。

例如:

When were you born?: 1998
You have lived through 4 olympiads.

数学让我感到困惑,我觉得自己不知道如何去做就完全是个白痴。这是我到目前为止所做的。

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int birthyear = 0; // Declare our birthyear variable to store the users year of birth, we use int to declare the data as an integer.
int olympiads = 4; // Declare our olympiads variable, used to help with the calculation of how many olympiads someone has lived through(One olympiad is the equivalent to 4 years).
//int OlympiadsCalc = <MATH CALCULATION HERE>;

cout << "In what year were you born?: "; // Using the cout() function, we    get user input
cin >> birthyear; // We store the users input into our variable birthyear
cout << "You have lived for " << olympiads << " olympiads!" << endl;
system("pause\n");
return 0;
}

我只是不知道如何计算出生年至今的4年间隔时间。

1 个答案:

答案 0 :(得分:-3)

首先,我认为“奥林匹克运动会”是指4年期间,而不是实际发生的奥运会(否则您的任务变得更加困难,并且涉及查询在线数据源等。)

无论如何,你有两个问题可以解决:

  1. 获取当前年份。请参阅StackOverflow上的this answer,关于如何使用C / C ++(但请注意,C ++ 11有另一种方法,如果对您很重要)。

  2. 一个简单的计算:一个人的生活年数是当年减去出生年份;奥林匹克运动会的全部数量是通过用户所居住的年数的简单整数除以奥林匹克运动员(4)的长度得到的。我相信你可以管理......

  3. 请注意,初始化两个变量的值不是您要使用的值。值4实际上应该是一个常量,可能名为OlympiadLengthInYears(或years_in_an_olympiad,或者你喜欢称之为。)