📜  直到给定奇数的奇数的平均值

📅  最后修改于: 2021-05-06 21:01:59             🧑  作者: Mango

给定一个奇数n,求1到n的奇数平均值。
例子:

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

Input : n = 221
Output : 111

方法1我们可以通过将每个奇数加到n,然后将和除以计数来计算平均值。
下面是该方法的实现。

C++
// Program to find average of odd numbers
// till a given odd number.
#include 
 
// Function to calculate the average
// of odd numbers
int averageOdd(int n)
{
    if (n % 2 == 0) {
        printf("Invalid Input");
        return -1;
    }
 
    int sum = 0, count = 0;
    while (n >= 1) {
 
        // count odd numbers
        count++;
 
        // store the sum of odd numbers
        sum += n;
 
        n = n - 2;
    }
    return sum / count;
}
 
// driver function
int main()
{
    int n = 15;
    printf("%d", averageOdd(n));
    return 0;
}


Java
// Program to find average of odd numbers
// till a given odd number.
import java.io.*;
 
class GFG {
     
    // Function to calculate the average
    // of odd numbers
    static int averageOdd(int n)
    {
        if (n % 2 == 0) {
            System.out.println("Invalid Input");
            return -1;
        }
      
        int sum = 0, count = 0;
        while (n >= 1) {
      
            // count odd numbers
            count++;
      
            // store the sum of odd numbers
            sum += n;
      
            n = n - 2;
        }
        return sum / count;
    }
      
    // driver function
    public static void main(String args[])
    {
        int n = 15;
        System.out.println(averageOdd(n));
    }
}
 
/*This code is contributed by Nikita tiwari.*/


Python3
# Program to find average
# of odd numbers till a
# given odd number.
 
# Function to calculate
# the average of odd
# numbers
def averageOdd(n) :
 
    if (n % 2 == 0) :
        print("Invalid Input")
        return -1
     
    sm = 0
    count = 0
 
    while (n >= 1) :
 
        # count odd numbers
        count = count + 1
 
        # store the sum of
        # odd numbers
        sm = sm + n
 
        n = n - 2
     
    return sm // count
     
# Driver function
n = 15
print(averageOdd(n))
 
# This code is contributed by Nikita Tiwari.


C#
// C# Program to find average 
// of odd numbers till a given
// odd number.
using System;
 
class GFG {
     
    // Function to calculate the
    // average of odd numbers
    static int averageOdd(int n)
    {
        if (n % 2 == 0) {
            Console.Write("Invalid Input");
            return -1;
        }
     
        int sum = 0, count = 0;
        while (n >= 1) {
     
            // count odd numbers
            count++;
     
            // store the sum of odd numbers
            sum += n;
     
            n = n - 2;
        }
        return sum / count;
    }
     
    // driver function
    public static void Main()
    {
        int n = 15;
        Console.Write(averageOdd(n));
    }
}
 
/*This code is contributed by vt_m.*/


PHP
= 1)
    {
 
        // count odd numbers
        $count++;
 
        // store the sum of
        // odd numbers
        $sum += $n;
 
        $n = $n - 2;
    }
    return $sum / $count;
}
 
    // Driver Code
    $n = 15;
    echo(averageOdd($n));
     
// This code is contributed by vt_m.
?>


Javascript


C
// Program to find average of odd numbers
// till a given odd number.
#include 
 
// Function to calculate the average
// of odd numbers
int averageOdd(int n)
{
    if (n % 2 == 0) {
        printf("Invalid Input");
        return -1;
    }
 
    return (n + 1) / 2;
}
 
// driver function
int main()
{
    int n = 15;
    printf("%d", averageOdd(n));
    return 0;
}


Java
// Program to find average of odd
// numbers till a given odd number.
import java.io.*;
 
class GFG
{
    // Function to calculate the
    // average of odd numbers
    static int averageOdd(int n)
    {
        if (n % 2 == 0)
        {
            System.out.println("Invalid Input");
            return -1;
        }
     
        return (n + 1) / 2;
    }
     
    // driver function
    public static void main(String args[])
    {
        int n = 15;
        System.out.println(averageOdd(n));
    }
}
 
// This code is contributed by Nikita tiwari.


Python3
# Program to find average of odd
# numbers till a given odd number.
 
# Function to calculate the
# average of odd numbers
def averageOdd(n) :
    if (n % 2 == 0) :
        print("Invalid Input")
        return -1
     
     
    return (n + 1) // 2
     
# driver function
n = 15
print(averageOdd(n))
 
 
 
# This code is contributed by Nikita tiwari.


C#
// C# Program to find average
// of odd numbers till a given
// odd number.
using System;
 
class GFG
{
    // Function to calculate the
    // average of odd numbers
    static int averageOdd(int n)
    {
        if (n % 2 == 0)
        {
            Console.Write("Invalid Input");
            return -1;
        }
     
        return (n + 1) / 2;
    }
     
    // driver function
    public static void Main()
    {
        int n = 15;
        Console.Write(averageOdd(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

8

方法二
奇数的平均值只能一步找到
通过使用以下公式
[n + 1] / 2
其中n是最后一个奇数。
这个公式如何运作?

We know there are (n+1)/2 odd numbers till n.  

For example:
There are two odd numbers till 3 and there are
three odd numbers till 5.

Sum of first k odd numbers is k*k 

Sum of odd numbers till n is ((n+1)/2)2

Average of odd numbers till n is (n + 1)/2

下面是该方法的实现。

C

// Program to find average of odd numbers
// till a given odd number.
#include 
 
// Function to calculate the average
// of odd numbers
int averageOdd(int n)
{
    if (n % 2 == 0) {
        printf("Invalid Input");
        return -1;
    }
 
    return (n + 1) / 2;
}
 
// driver function
int main()
{
    int n = 15;
    printf("%d", averageOdd(n));
    return 0;
}

Java

// Program to find average of odd
// numbers till a given odd number.
import java.io.*;
 
class GFG
{
    // Function to calculate the
    // average of odd numbers
    static int averageOdd(int n)
    {
        if (n % 2 == 0)
        {
            System.out.println("Invalid Input");
            return -1;
        }
     
        return (n + 1) / 2;
    }
     
    // driver function
    public static void main(String args[])
    {
        int n = 15;
        System.out.println(averageOdd(n));
    }
}
 
// This code is contributed by Nikita tiwari.

Python3

# Program to find average of odd
# numbers till a given odd number.
 
# Function to calculate the
# average of odd numbers
def averageOdd(n) :
    if (n % 2 == 0) :
        print("Invalid Input")
        return -1
     
     
    return (n + 1) // 2
     
# driver function
n = 15
print(averageOdd(n))
 
 
 
# This code is contributed by Nikita tiwari.

C#

// C# Program to find average
// of odd numbers till a given
// odd number.
using System;
 
class GFG
{
    // Function to calculate the
    // average of odd numbers
    static int averageOdd(int n)
    {
        if (n % 2 == 0)
        {
            Console.Write("Invalid Input");
            return -1;
        }
     
        return (n + 1) / 2;
    }
     
    // driver function
    public static void Main()
    {
        int n = 15;
        Console.Write(averageOdd(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

8