多项式的Sgn值

给定一个多项式函数f(x)=1+a1*x+a2*(x^2)+…an(x^n)。找到 Sgn 这些函数的值,当x给定时,所有的系数也是。

null
If value of polynomial greater than 0   Sign = 1Else If value of polynomial less than 0   Sign = -1Else if value of polynomial is 0   Sign = 0

例如:

Input: poly[] = [1, 2, 3]        x = 1 Output:  1 Explanation: f(1) = 6 which is > 0 hence 1.Input: poly[] = [1, -1, 2, 3]        x = -2 Output: -1 Explanation: f(-2)=-11 which is less then 0, hence -1.

A. 缺乏经验的 方法是计算x的每一次幂,然后将其与系数相乘,将其加到答案中。计算x的幂需要O(n)个时间,计算n个系数需要O(n)个时间。因此,将总复杂度设为O(n*n) 一 有效率的 方法是使用 霍纳法 我们用霍纳的方法计算多项式的值。然后我们根据值的符号返回值。 下面是上述方法的实现

C++

// CPP program to find sign value of a
// polynomial
#include <iostream>
using namespace std;
// returns value of poly[0]x(n-1) + poly[1]x(n-2)
// + .. + poly[n-1]
int horner( int poly[], int n, int x)
{
int result = poly[0]; // Initialize result
// Evaluate value of polynomial
// using Horner's method
for ( int i=1; i<n; i++)
result = result*x + poly[i];
return result;
}
// Returns sign value of polynomial
int findSign( int poly[], int n, int x)
{
int result = horner(poly, n, x);
if (result > 0)
return 1;
else if (result < 0)
return -1;
return 0;
}
// Driver program to test above function.
int main()
{
// Let us evaluate value of 2x3 - 6x2
// + 2x - 1 for x = 3
int poly[] = {2, -6, 2, -1};
int x = 3;
int n = sizeof (poly)/ sizeof (poly[0]);
cout << "Sign of polynomial is "
<< findSign(poly, n, x);
return 0;
}


JAVA

// Java program to find sign value of a
// polynomial
class GFG
{
// returns value of poly[0]x(n-1) + poly[1]x(n-2)
// + .. + poly[n-1]
static int horner( int poly[], int n, int x)
{
// Initialize result
int result = poly[ 0 ];
// Evaluate value of polynomial
// using Horner's method
for ( int i = 1 ; i < n; i++)
result = result * x + poly[i];
return result;
}
// Returns sign value of polynomial
static int findSign( int poly[], int n, int x)
{
int result = horner(poly, n, x);
if (result > 0 )
return 1 ;
else if (result < 0 )
return - 1 ;
return 0 ;
}
// Driver code
public static void main (String[] args)
{
// Let us evaluate value of 2x3 - 6x2
// + 2x - 1 for x = 3
int poly[] = { 2 , - 6 , 2 , - 1 };
int x = 3 ;
int n = poly.length;
System.out.print( "Sign of polynomial is " +
findSign(poly, n, x));
}
}
// This code is contributed by Anant Agarwal.


Python3

# Python3 program to find
# sign value of a
# polynomial
# returns value of poly[0]x(n-1) +
# poly[1]x(n-2) + .. + poly[n-1]
def horner( poly, n, x):
# Initialize result
result = poly[ 0 ];
# Evaluate value of
# polynomial using
# Horner's method
for i in range ( 1 ,n):
result = (result * x +
poly[i]);
return result;
# Returns sign value
# of polynomial
def findSign(poly, n, x):
result = horner(poly, n, x);
if (result > 0 ):
return 1 ;
elif (result < 0 ):
return - 1 ;
return 0 ;
# Driver Code
# Let us evaluate value
# of 2x3 - 6x2
# + 2x - 1 for x = 3
poly = [ 2 , - 6 , 2 , - 1 ];
x = 3 ;
n = len (poly);
print ( "Sign of polynomial is " ,
findSign(poly, n, x));
# This code is contributed by mits


C#

// C# program to find sign value of a
// polynomial
using System;
class GFG {
// returns value of poly[0]x(n-1)
// + poly[1]x(n-2) + .. + poly[n-1]
static int horner( int []poly, int n, int x)
{
// Initialize result
int result = poly[0];
// Evaluate value of polynomial
// using Horner's method
for ( int i = 1; i < n; i++)
result = result * x + poly[i];
return result;
}
// Returns sign value of polynomial
static int findSign( int []poly, int n, int x)
{
int result = horner(poly, n, x);
if (result > 0)
return 1;
else if (result < 0)
return -1;
return 0;
}
// Driver code
public static void Main ()
{
// Let us evaluate value of 2x3 - 6x2
// + 2x - 1 for x = 3
int []poly = {2, -6, 2, -1};
int x = 3;
int n = poly.Length;
Console.Write( "Sign of polynomial is "
+ findSign(poly, n, x));
}
}
// This code is contributed by vt_m.


PHP

<?php
// PHP program to find
// sign value of a
// polynomial
// returns value of poly[0]x(n-1) +
// poly[1]x(n-2) + .. + poly[n-1]
function horner( $poly , $n , $x )
{
// Initialize result
$result = $poly [0];
// Evaluate value of polynomial
// using Horner's method
for ( $i = 1; $i < $n ; $i ++)
$result = $result * $x + $poly [ $i ];
return $result ;
}
// Returns sign value
// of polynomial
function findSign( $poly , $n , $x )
{
$result = horner( $poly , $n , $x );
if ( $result > 0)
return 1;
else if ( $result < 0)
return -1;
return 0;
}
// Driver Code
// Let us evaluate value
// of 2x3 - 6x2
// + 2x - 1 for x = 3
$poly = array (2, -6, 2, -1);
$x = 3;
$n = count ( $poly );
echo "Sign of polynomial is "
, findSign( $poly , $n , $x );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript program to find sign value of a
// polynomial
// Returns value of poly[0]x(n-1) + poly[1]x(n-2)
// + .. + poly[n-1]
function horner(poly, n, x)
{
// Initialize result
var result = poly[0];
// Evaluate value of polynomial
// using Horner's method
for ( var i = 1; i < n; i++)
result = result * x + poly[i];
return result;
}
// Returns sign value of polynomial
function findSign(poly, n, x)
{
var result = horner(poly, n, x);
if (result > 0)
return 1;
else if (result < 0)
return -1;
return 0;
}
// Driver Code
// Let us evaluate value of 2x3 - 6x2
// + 2x - 1 for x = 3
var poly = [ 2, -6, 2, -1 ];
var x = 3;
var n = poly.length;
document.write( "Sign of polynomial is " +
findSign(poly, n, x));
// This code is contributed by kirti
</script>


输出:

Sign of polynomial is 1

时间复杂性 :O(n) 本文由 拉贾·维克拉马蒂亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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