📌  相关文章
📜  教资会网络 | UGC-NET CS 2017 年 11 月 – III |问题 56(1)

📅  最后修改于: 2023-12-03 15:39:56.280000             🧑  作者: Mango

UGC-NET CS 2017 November - III | Question 56

In this question, we are given two C++ functions:

int fun(int n)
{
    if(n == 1)
        return 1;
    else
        return fun(n - 1) + n * n;
}

int gun(int n)
{
    if(n == 0)
        return 0;
    else
        return gun(n - 1) + n * n;
}

We are asked to choose the correct option regarding these two functions.

Approach

Let's first understand what these two functions are doing.

The function fun takes an integer n and returns the sum of squares of numbers from 1 to n. For example, fun(3) returns 14 as 1^2 + 2^2 + 3^2 = 14.

The function gun takes an integer n and returns the sum of squares of numbers from 0 to n. For example, gun(3) returns 14 as 0^2 + 1^2 + 2^2 + 3^2 = 14.

We need to choose the correct option regarding these two functions, which means we need to identify the differences between them.

Solution

Let's start with option (A).

fun(n) = gun(n - 1) + n^2

This equation looks like the definition of fun, but with gun instead of fun. Let's check if it holds for some inputs.

fun(1) = 1   // 1^2 = 1
gun(0) + 1^2 = 1   // 0 + 1^2 = 1

This equation holds true for n = 1, but let's check for another value.

fun(2) = 5   // 1^2 + 2^2 = 5
gun(1) + 2^2 = 5   // 0^2 + 1^2 + 2^2 = 5

This equation doesn't hold true for n = 2, so option (A) is incorrect.

Let's move to option (B).

fun(n) - fun(n - 1) = n^2

This equation says that the difference between the sum of squares of numbers from 1 to n and the sum of squares of numbers from 1 to n - 1 is n^2. Let's check if it holds for some inputs.

fun(1) - fun(0) = 1   // 1^2 - 0^2 = 1
1^2 = 1

fun(2) - fun(1) = 3   // (1^2 + 2^2) - (1^2) = 3
2^2 = 4

fun(3) - fun(2) = 5   // (1^2 + 2^2 + 3^2) - (1^2 + 2^2) = 5
3^2 = 9

This equation holds true for all n, so option (B) is correct.

Let's move to option (C).

gun(n) - gun(n - 1) = n^2

This equation says that the difference between the sum of squares of numbers from 0 to n and the sum of squares of numbers from 0 to n - 1 is n^2. Let's check if it holds for some inputs.

gun(1) - gun(0) = 1   // 1^2 - 0^2 = 1
1^2 = 1

gun(2) - gun(1) = 3   // (0^2 + 1^2 + 2^2) - (0^2 + 1^2) = 3
2^2 = 4

gun(3) - gun(2) = 5   // (0^2 + 1^2 + 2^2 + 3^2) - (0^2 + 1^2 + 2^2) = 5
3^2 = 9

This equation also holds true for all n, so option (C) is incorrect.

Let's move to option (D).

fun(n) - gun(n) = fun(n - 1)

This equation says that the difference between the sum of squares of numbers from 1 to n and the sum of squares of numbers from 0 to n is the sum of squares of numbers from 1 to n - 1. Let's check if it holds for some inputs.

fun(1) - gun(1) = fun(0)   // 1^2 - 0^2 = 1
1 = 1

fun(2) - gun(2) = fun(1)   // (1^2 + 2^2) - (0^2 + 1^2 + 2^2) = 1
5 - 5 = 0

fun(3) - gun(3) = fun(2)   // (1^2 + 2^2 + 3^2) - (0^2 + 1^2 + 2^2 + 3^2) = -5
14 - 14 = 0

This equation also holds true for all n, so option (D) is correct.

Hence, the correct options are (B) and (D).

Conclusion

In this question, we have analyzed two C++ functions and identified the differences between them. We have used simple algebraic equations to check the correctness of the given options.