给定N和标准差,则找到N个元素。
Mean is average of element.
Mean of arr[0..n-1] = Σ(arr[i]) / n
where 0 <= i < n
Variance is sum of squared differences from the mean divided by number of elements.
Variance = Σ(arr[i] – mean)2 / n
Standard Deviation is square root of variance
Standard Deviation = Σ(variance)
Please refer Mean, Variance and Standard Deviation for details.
例子:
Input: 6 0
Output: 0 0 0 0 0 0
Explanation:
The standard deviation of 0, 0, 0, 0, 0, 0 is 0.
Also the standard deviation of 4, 4, 4, 4, 4, 4
is 0, we print any of the possible N elements.
Input: 3 3
Output: 0 -3.67423 3.67423
Explanation:
On calculating SD of these N elements,
we get standard deviation to be 3.
方法:
如果看一下公式,我们有两个未知项,一个是xi,另一个是均值。主要动机是使平均值为0,以便我们可以获取X元素的公式。将有两种情况,一种是偶数,一种是奇数。
当N为偶数时:
要使N个元素的平均值为0,最好的方法是将N个元素表示为-X + X -X + X…。公式将是sqrt((x ^ 2)/ n的总和),x2 + x ^ 2 + x ^ 2 +………N个项,因此公式将是sqrt(N *(x ^ 2)/ N) 。 N互相抵消,因此sqrt(x ^ 2)变成SD。因此,我们得到N个元素为-SD + SD -SD + SD……以得到均值0。我们需要打印-SD + SD -SD + SD……
当N为奇数时:
N个元素的平均值为0。因此,一个元素为0,其他N-1个元素为-X + X -X…。公式将为sqrt((x ^ 2)/ n的总和),x2 + x ^ 2 + x ^ 2 + ………N-1个项,因此公式为sqrt((N-1)*(x ^ 2)/ N),所以
X = SD * sqrt(n /(n-1))。 n个元素是0 -X + X -X + X…
当SD为0时,所有元素都将相同,因此我们可以为其打印0。
下面是上述方法的实现:
C++
// CPP program to find n elements
#include
using namespace std;
// function to print series of n elements
void series(int n, int d)
{
// if S.D. is 0 then print all
// elements as 0.
if (d == 0) {
// print n 0's
for (int i = 0; i < n; i++)
cout << "0 ";
cout << endl;
return;
}
// if S.D. is even
if (n % 2 == 0) {
// print -SD, +SD, -SD, +SD
for (int i = 1; i <= n; i++) {
cout << pow(-1, i) * d << " ";
}
cout << endl;
}
else // if odd
{
// convert n to a float integer
float m = n;
float r = (m / (m - 1));
float g = (float)(d * (float)sqrtf(r));
// print one element to be 0
cout << "0 ";
// print (n-1) elements as xi derived
// from the formula
for (int i = 1; i < n; i++) {
cout << pow(-1, i) * g << " ";
}
cout << endl;
}
}
// driver program to test the above function
int main()
{
int n = 3, d = 3;
series(n, d);
return 0;
}
Java
// Java program to find n elements
import java.util.*;
import java.lang.*;
public class GfG {
// function to print series of n elements
public static void series(int n, int d)
{
// if S.D. is 0 then print all
// elements as 0.
if (d == 0) {
// print n 0's
for (int i = 0; i < n; i++)
System.out.print("0 ");
System.out.println();
return;
}
// if S.D. is even
if (n % 2 == 0) {
// print -SD, +SD, -SD, +SD
for (int i = 1; i <= n; i++) {
System.out.print(Math.pow(-1, i) * d + " ");
}
System.out.println();
}
else // if odd
{
// convert n to a float integer
float m = n;
float r = (m / (m - 1));
float g = (float)(d * (float)(Math.sqrt(r)));
// print one element to be 0
System.out.print("0 ");
// print (n-1) elements as xi
// derived from the formula
for (int i = 1; i < n; i++) {
System.out.print(Math.pow(-1, i) * g + " ");
}
System.out.println();
}
}
// driver function
public static void main(String args[])
{
int n = 3, d = 3;
series(n, d);
}
}
/* This code is contributed by Sagar Shukla */
Python3
# Python program to find n elements
import math
# function to print series of n elements
def series( n, d):
# if S.D. is 0 then print all
# elements as 0.
if d == 0:
# print n 0's
for i in range(n):
print("0", end = ' ')
return 1
# if S.D. is even
if n % 2 == 0:
# print -SD, +SD, -SD, +SD
i = 1
while i <= n:
print("%.5f"%((math.pow(-1, i) * d)),
end =' ')
i += 1
else:
# if odd
# convert n to a float integer
m = n
r = (m / (m - 1))
g = (float)(d * float(math.sqrt(r)))
# print one element to be 0
print("0 ", end = ' ')
# print (n-1) elements as xi derived
# from the formula
i = 1
while i < n:
print("%.5f"%(math.pow(-1, i) * g),
end = ' ')
i = i + 1
print("\n")
# driver code to test the above function
n = 3
d = 3
series(n, d)
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to find n elements
using System;
public class GfG {
// function to print series of n
// elements
public static void series(int n, int d)
{
// if S.D. is 0 then print all
// elements as 0.
if (d == 0) {
// print n 0's
for (int i = 0; i < n; i++)
Console.Write("0");
Console.WriteLine();
return;
}
// if S.D. is even
if (n % 2 == 0) {
// print -SD, +SD, -SD, +SD
for (int i = 1; i <= n; i++) {
Console.Write(Math.Pow(-1, i)
* d + " ");
}
Console.WriteLine();
}
else // if odd
{
// convert n to a float integer
float m = n;
float r = (m / (m - 1));
float g = (float)(d *
(float)(Math.Sqrt(r)));
// print one element to be 0
Console.Write("0 ");
// print (n-1) elements as xi
// derived from the formula
for (int i = 1; i < n; i++) {
Console.Write(Math.Pow(-1, i)
* g + " ");
}
Console.WriteLine();
}
}
// driver function
public static void Main()
{
int n = 3, d = 3;
series(n, d);
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
0 -3.67423 3.67423