📜  前n个奇数自然数的平均值

📅  最后修改于: 2021-04-29 10:54:58             🧑  作者: Mango

给定数字n,然后找到前n个奇数的平均值
1 + 3 + 5 + 7 + 9 +…………。+(2n – 1)
例子 :

Input  : 5
Output : 5
(1 + 3 + 5 + 7 + 9)/5 = 5 

Input  : 10
Output : 10
(1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19)/10 =10

方法1(天真的方法:)
一个简单的解决方案是将循环形式迭代1到n次。通过所有奇数的总和除以n,此解决方案需要O(N)时间。

C++
// A  C++ program to find average of
// sum of first n odd natural numbers.
#include 
using namespace std;
 
// Returns the Avg of
// first n odd numbers
int avg_of_odd_num(int n)
{
 
    // sum of first n odd number
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += (2 * i + 1);
 
    // Average of first
    // n odd numbers
    return sum / n;
}
 
// Driver Code
int main()
{
    int n = 20;
    cout << avg_of_odd_num(n);
    return 0;
}


Java
// Java program to find average of
// sum of first n odd natural numbers.
import java.io.*;
 
class GFG {
 
    // Returns the Avg of
    // first n odd numbers
    static int avg_of_odd_num(int n)
    {
 
        // sum of first n odd number
        int sum = 0;
 
        for (int i = 0; i < n; i++)
            sum += (2 * i + 1);
 
        // Average of first
        // n odd numbers
        return sum / n;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int n = 20;
        avg_of_odd_num(n);
 
        System.out.println(avg_of_odd_num(n));
    }
}
 
// This code is contributed by vt_m


Python3
# A Python 3 program
# to find average of
# sum of first n odd
# natural numbers.
 
# Returns the Avg of
# first n odd numbers
def avg_of_odd_num(n) :
 
    # sum of first n odd number
    sm = 0
    for i in range(0, n) :
        sm = sm + (2 * i + 1)
      
    # Average of first
    # n odd numbers
    return sm//n
 
  
# Driver Code
n = 20
print(avg_of_odd_num(n))
 
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to find average
// of sum of first n odd
// natural numbers.
using System;
 
class GFG {
 
    // Returns the Avg of
    // first n odd numbers
    static int avg_of_odd_num(int n)
    {
 
        // sum of first n odd number
        int sum = 0;
 
        for (int i = 0; i < n; i++)
            sum += (2 * i + 1);
 
        // Average of first
        // n odd numbers
        return sum / n;
    }
 
    // Driver code
    public static void Main()
    {
 
        int n = 20;
        avg_of_odd_num(n);
 
        Console.Write(avg_of_odd_num(n));
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal


PHP


Javascript


C++
// CPP Program to find the average
// of sum of first n odd numbers
#include 
using namespace std;
 
// Return the average of sum
// of first n odd numbers
int avg_of_odd_num(int n)
{
    return n;
}
 
// Driver Code
int main()
{
    int n = 8;
    cout << avg_of_odd_num(n);
    return 0;
}


Java
// java Program to find the average
// of sum of first n odd numbers
import java.io.*;
 
class GFG {
 
    // Return the average of sum
    // of first n odd numbers
    static int avg_of_odd_num(int n)
    {
        return n;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 8;
 
        System.out.println(avg_of_odd_num(n));
    }
}
 
// This code is contributed by vt_m


Python3
# Python 3 Program to
# find the average
# of sum of first n
# odd numbers
 
# Return the average of sum
# of first n odd numbers
def avg_of_odd_num(n) :
    return n
     
 
# Driver Code
n = 8
print(avg_of_odd_num(n))
 
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# Program to find the average
// of sum of first n odd numbers
using System;
 
class GFG {
    // Return the average of sum
    // of first n odd numbers
    static int avg_of_odd_num(int n)
    {
        return n;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 8;
        Console.Write(avg_of_odd_num(n));
    }
}
// This code is contributed by
// Smitha Dinesh Semwal


PHP


Javascript


输出 :

20

时间复杂度:O(n)
方法2(有效方法:)
这个想法是第一个n个奇数的总和是n 2 ,为了找到第一个n个奇数的平均值,所以将其除以n,因此公式为n 2 / n = n 。需要O(1)时间。

Avg of sum of first N odd Numbers = N

C++

// CPP Program to find the average
// of sum of first n odd numbers
#include 
using namespace std;
 
// Return the average of sum
// of first n odd numbers
int avg_of_odd_num(int n)
{
    return n;
}
 
// Driver Code
int main()
{
    int n = 8;
    cout << avg_of_odd_num(n);
    return 0;
}

Java

// java Program to find the average
// of sum of first n odd numbers
import java.io.*;
 
class GFG {
 
    // Return the average of sum
    // of first n odd numbers
    static int avg_of_odd_num(int n)
    {
        return n;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 8;
 
        System.out.println(avg_of_odd_num(n));
    }
}
 
// This code is contributed by vt_m

Python3

# Python 3 Program to
# find the average
# of sum of first n
# odd numbers
 
# Return the average of sum
# of first n odd numbers
def avg_of_odd_num(n) :
    return n
     
 
# Driver Code
n = 8
print(avg_of_odd_num(n))
 
 
# This code is contributed
# by Nikita Tiwari.

C#

// C# Program to find the average
// of sum of first n odd numbers
using System;
 
class GFG {
    // Return the average of sum
    // of first n odd numbers
    static int avg_of_odd_num(int n)
    {
        return n;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 8;
        Console.Write(avg_of_odd_num(n));
    }
}
// This code is contributed by
// Smitha Dinesh Semwal

的PHP


Java脚本


输出 :

8

时间复杂度:O(1)
证明

Sum of first n terms of an A.P.(Arithmetic Progression)
= (n/2) * [2*a + (n-1)*d].....(i)
where, a is the first term of the series 
and d is the difference between the adjacent 
terms of the series.

Here, a = 1, d = 2, applying these values to e. q., 
(i), we get
Sum = (n/2) * [2*1 + (n-1)*2]
    = (n/2) * [2 + 2*n - 2]
    = (n/2) * (2*n)
    = n*n
    = n2

Avg of first n odd numbers = n2/n
                           = n