簡體   English   中英

C#Horner Exercise錯誤答案

[英]c# Horner Exercise wrong answer

我做運動有問題。 該程序必須使用Horner方案進行計數。 我已盡力而為,但我總是得到錯誤的答案,恐怕我犯了一個小錯誤,可以輕易解決。 我不知道該怎么辦,希望有人能幫助我解決這個問題。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FeelsBadMan
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    int i, a, x, h, n;
                    int[] array = new int[100];
                    Console.Write("Degree of a polynomial: ");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Constant term : ");
                    n = Convert.ToInt32(Console.ReadLine());


                    for (i = 1; i <= a; i++)
                    {
                        Console.Write("Input number x^{0}: ", i);
                        array[i] = Convert.ToInt32(Console.ReadLine());
                    }
                    Console.Write("Input x value: ");
                    x = Convert.ToInt32(Console.ReadLine());
                    {
                        h = array[0];
                        for (i = 1; i < a; i++)
                        {
                            h = (h * x) + array[i] + n;
                        }
                        Console.Write("Result: {0}\n", h);
                        Console.ReadKey();
                        break;
                    }
                }
                catch (OverflowException)
                {
                    Console.WriteLine("Number out of scale! Try again.\n");
                }
                catch (FormatException)
                {
                    Console.WriteLine("Incorrect format! Try again.\n");
                }
            }
        }
    }
}

例如:

Degree of a polynomial:3
Constant term: 5
Input number x^1: 1
Input number x^2: 0
Input number x^3: 1
Input x value: 3
Result: 23

但是正確的答案是35。

這將輸出正確的結果:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FeelsBadMan
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    Console.Write("Degree of a polynomial: ");
                    Int32 a = Convert.ToInt32(Console.ReadLine());

                    Console.Write("Constant term : ");
                    Int32 n = Convert.ToInt32(Console.ReadLine());

                    List<Int32> polys = new List<Int32>(a + 1);

                    for (Int32 i = 1; i <= a; ++i)
                    {
                        Console.Write("Input number x^{0}: ", i);
                        polys.Add(Convert.ToInt32(Console.ReadLine()));
                    }

                    polys.Insert(0,n);

                    Console.Write("Input x value: ");
                    Int32 x = Convert.ToInt32(Console.ReadLine());

                    Int32 result = array[a];

                    for (Int32 i = a - 1; i >= 0; --i)
                        result = (result * x) + polys[i];

                    Console.Write("Result: {0}\n", result);
                    Console.ReadKey();
                }
                catch (OverflowException)
                {
                    Console.WriteLine("Number out of scale! Try again.\n");
                }
                catch (FormatException)
                {
                    Console.WriteLine("Incorrect format! Try again.\n");
                }
            }
        }
    }
}

您在每次迭代中都將常數項包括在結果中,這是主要問題! 另外,正如@Peter Duniho指出的那樣,Horner的方法要求從最高階系數開始到最低系數,所以我反轉了迭代順序。

最后但並非最不重要的一點是,您的系統基於固定長度的整數數組,在這種情況下,這實際上不是一個好習慣。 我改用List<Int32>更好,它用等於a + 1的固定容量初始化,以便也照顧常數項,一旦所有多項式都被加到索引0定義。

附帶說明一下,如果必須發布此代碼,請不要忘記添加健全性檢查以查看用戶輸入是否正確。

您的循環中存在三個獨立的問題:

  1. 您的初始值設置為array[0] ,它是數組的未初始化元素,因此值為0 相反,應將其設置為多項式的最高系數,即array[a]
  2. 由於某種無法解釋的原因,您需要在循環的每次迭代中添加常數項,即最低階系數。
  3. 最后但並非最不重要的一點是,您以錯誤的順序計算值,從最低階的系數開始到最高的系數。 Horner的方法要求從最高階系數開始到最低階。

直接從Wikipedia文章實現算法,您的循環應如下所示:

array[0] = n;
int h = array[a];

for (int i = a - 1; i >= 0; i--)
{
    h = array[i] + h * x;
}

這將產生預期的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM