从n个缺失一个的求和方程中寻找n个变量

给出了一个数组a[],其中n个值为[1],a[2]…a[n],它们是n变量中n个方程的一部分,其中方程为: Eqn1=>X2+X3++Xn=a1 Eqn2=>X1+X3++Xn=a2 Eqni=>X1+X2+…+X i-1 +X i+1 ..+Xn=ai Eqnn=>X1+X2++十、 n-1 =安 因为在n个变量中有n个方程,所以求所有变量(X1,X2…Xn)的值。 例如:

null
Input : a[] = {4, 4, 4, 4, 4}Output : X1 = 1         X2 = 1         X3 = 1         X4 = 1         X5 = 1Input : a[] = {2, 5, 6, 4, 8}Output : X1 = 4.25         X2 = 1.25         X3 = 0.25         X4 = 2.25         X5 = -1.75

方法 : 设X1+X2+X3+…。Xn=总和 使用值和,我们的方程如下

SUM - X1 = a1 -----(1)SUM - X2 = a2  -----(2)SUM - Xi = ai  -----(i)SUM - Xn = an -------(n)---------------------------------------------------------------Now, if we add all these equation we will have an equation as :n*SUM -(X1+X2+...Xn) = a1 + a2 + ...ann*SUM - SUM = a1 + a2 + ...anSUM = (a1+a2+...an)/(n-1)Calculate SUM from above equation.putting value of SUM in (i), (ii).... we haveX1 = SUM - a1X2 = SUM - a2

解决方案:

X1 = SUM - a1X2 = SUM - a2Xi = SUM - aiXn = SUM - an

C++

// CPP program to find n-variables
#include <bits/stdc++.h>
using namespace std;
// function to print n-variable values
void findVar( int a[], int n)
{
// calculate value of array SUM
float SUM = 0;
for ( int i = 0; i < n; i++)
SUM += a[i];
// Every variable contributes n-1
// times to sum. So dividing by
// n-1 to get sum of all.
SUM /= (n - 1);
// print the values of n-variables
for ( int i = 0; i < n; i++)
cout << "X" << (i + 1)
<< " = " << SUM - a[i] << endl;
}
// driver program
int main()
{
int a[] = { 2, 5, 6, 4, 8 };
int n = sizeof (a) / sizeof (a[0]);
findVar(a, n);
return 0;
}


JAVA

// Java program to
// find n-variables
import java.io.*;
class GFG
{
// function to print
// n-variable values
static void findVar( int []a,
int n)
{
// calculate value+
// of array SUM
float SUM = 0 ;
for ( int i = 0 ; i < n; i++)
SUM += a[i];
// Every variable contributes
// n-1 times to sum. So dividing
// by n-1 to get sum of all.
SUM /= (n - 1 );
// print the values
// of n-variables
for ( int i = 0 ; i < n; i++)
System.out.print( "X" + (i + 1 ) +
" = " + (SUM -
a[i]) + "" );
}
// Driver Code
public static void main(String args[])
{
int []a = new int []{ 2 , 5 , 6 , 4 , 8 };
int n = a.length;
findVar(a, n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)


Python3

# Python3 program to
# find n-variables
# function to print
# n-variable values
def findVar(a, n):
# calculate value of
# array SUM
SUM = 0 ;
for i in range (n):
SUM + = a[i];
# Every variable contributes
# n-1 times to sum. So
# dividing by n-1 to get sum
# of all.
SUM = SUM / (n - 1 );
# print the values
# of n-variables
for i in range (n):
print ( "X" , (i + 1 ),
" = " , SUM - a[i]);
# Driver Code
a = [ 2 , 5 , 6 , 4 , 8 ];
n = len (a);
findVar(a, n);
# This code is contributed
# by mits


C#

// C# program to find n-variables
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
// function to print
// n-variable values
static void findVar( int []a,
int n)
{
// calculate value+
// of array SUM
float SUM = 0;
for ( int i = 0; i < n; i++)
SUM += a[i];
// Every variable contributes
// n-1 times to sum. So dividing
// by n-1 to get sum of all.
SUM /= (n - 1);
// print the values
// of n-variables
for ( int i = 0; i < n; i++)
Console.Write( "X" + (i + 1) +
" = " + (SUM - a[i]) + "" );
}
// Driver Code
static void Main()
{
int []a = { 2, 5, 6, 4, 8 };
int n = a.Length;
findVar(a, n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP

<?php
// PHP program to
// find n-variables
// function to print
// n-variable values
function findVar( $a , $n )
{
// calculate value of
// array SUM
$SUM = 0;
for ( $i = 0; $i < $n ; $i ++)
$SUM += $a [ $i ];
// Every variable contributes n-1
// times to sum. So dividing by
// n-1 to get sum of all.
$SUM /= ( $n - 1);
// print the values
// of n-variables
for ( $i = 0; $i < $n ; $i ++)
echo "X" , ( $i + 1)
, " = " , $SUM - $a [ $i ];
}
// Driver Code
$a = array (2, 5, 6, 4, 8);
$n = count ( $a );
findVar( $a , $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// function to print
// n-variable values
function findVar(a,n)
{
// calculate value+
// of array SUM
let SUM = 0;
for (let i = 0; i < n; i++)
SUM += a[i];
// Every variable contributes
// n-1 times to sum. So dividing
// by n-1 to get sum of all.
SUM /= (n - 1);
// print the values
// of n-variables
for (let i = 0; i < n; i++)
document.write( "X" + (i + 1) + " = " + (SUM - a[i]) + "<br>" );
}
let a = [2, 5, 6, 4, 8 ];
let n = a.length;
findVar(a, n);
// This code is contributed by mohit kumar 29.
</script>


输出:

X1 = 4.25X2 = 1.25X3 = 0.25X4 = 2.25X5 = -1.75

时间复杂性: O(n) 辅助空间: O(1)

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享