📜  在A和B之间找到N个算术平均值

📅  最后修改于: 2021-04-26 05:20:53             🧑  作者: Mango

给定三个整数A,B和N,任务是在A和B之间找到N个算术平均值。我们基本上需要在算术级数中插入N个项。其中A和B是第一项和最后一项。

例子:

Input : A = 20 B = 32 N = 5
Output : 22 24 26 28 30
The Arithmetic progression series as 
20 22 24 26 28 30 32 

Input : A = 5  B = 35  N = 5
Output : 10 15 20 25 30

方法 :
设A 1 ,A 2 ,A 3 ,A 4 …A n为两个给定数字A和B之间的N个算术平均值。然后A,A 1 ,A 2 ….. A n ,B将处于算术级数
现在,B =算术级数的(N + 2)项。
所以 :
找出算术级数级数的(N + 2)
其中d是共同差异

B = A +(N + 2 – 1)d
B – A =(N + 1)d

因此,共同差d由给出。

d =(B – A)/(N +1)

所以现在我们有了A的值和共同差(d)的值
现在我们可以找到A和B之间的所有N个算术平均值。

C++
// C++ program to find n arithmetic 
// means between A and B
#include 
using namespace std;
   
// Prints N arithmetic means between
// A and B.
void printAMeans(int A, int B, int N)
{
    // calculate common difference(d)
    float d = (float)(B - A) / (N + 1);
       
    // for finding N the arithmetic 
    // mean between A and B
    for (int i = 1; i <= N; i++) 
        cout << (A + i * d) <<" ";    
}
   
// Driver code to test above 
int main()
{
    int A = 20, B = 32, N = 5;
    printAMeans(A, B, N);    
    return 0;
}


Java
// java program to illustrate
// n arithmetic mean between 
// A and B
import java.io.*;
import java.lang.*;
import java.util.*;
   
public class GFG {
   
    // insert function for calculating the means
    static void printAMeans(int A, int B, int N)
    {       
        // Finding the value of d Common difference
        float d = (float)(B - A) / (N + 1);
                              
        // for finding N the Arithmetic 
        // mean between A and B
        for (int i = 1; i <= N; i++) 
          System.out.print((A + i * d) + " ");
           
    }
   
    // Driver code
    public static void main(String args[])
    {
        int A = 20, B = 32, N = 5;
        printAMeans(A, B, N);
    }
}


Python3
# Python3 program to find n arithmetic
# means between A and B
  
# Prints N arithmetic means 
# between A and B.
def printAMeans(A, B, N):
  
    # Calculate common difference(d)
    d = (B - A) / (N + 1)
      
    # For finding N the arithmetic 
    # mean between A and B
    for i in range(1, N + 1): 
        print(int(A + i * d), end = " ") 
  
# Driver code
A = 20; B = 32; N = 5
printAMeans(A, B, N) 
  
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to illustrate
// n arithmetic mean between 
// A and B
using System;
  
public class GFG {
  
    // insert function for calculating the means
    static void printAMeans(int A, int B, int N)
    {     
        // Finding the value of d Common difference
        float d = (float)(B - A) / (N + 1);
                              
        // for finding N the Arithmetic 
        // mean between A and B
        for (int i = 1; i <= N; i++) 
        Console.Write((A + i * d) + " ");
          
    }
  
    // Driver code
    public static void Main()
    {
        int A = 20, B = 32, N = 5;
        printAMeans(A, B, N);
    }
}
// Contributed by vt_m


PHP


输出:
22 24 26 28 30