ZeroJudge G488: COVID-101

At the beginning of the school year, the Computer Science Club was already infected with a virus called COVID-101. Those who contracted this disease would constantly act weak and often mention "computing." COVID-101 spreads in a special way: on the x-th day, there are n(x) = n(x – 1) + x2 – x + 1 viruses. Please calculate the number of COVID-101 viruses n(x) after x days.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
Input an integer, representing the number of days x.
x <= 200
Output n(x).
24
10340

Thought Process

You can use a recursive function to return the value, and the termination condition would be when N=1, then return 1.

Sample Code-ZeroJudge G488: COVID-101

#include <iostream>
using namespace std;

int calc (int N)
{
    if (N == 1) return 1;
    return calc(N-1) + (N*N) - N + 1;
}

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    cout << calc(N) << "\n";
}

//ZeroJudge G488
//Dr. SeanXD

Comments