私のコンソールプログラムに関する質問があります。 Hornerアルゴリズムを使用してカウントする必要があります。例外はスローされませんが、正しい結果は得られません。ここでHornerアルゴリズム
誰もが私が何をするかわからないので、私は、非常に感謝される私を助けることができれば...
は私のプログラムのコードです:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Consola_Horner_Rekurencyjnie
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Podaj stopień wielomioanu: ");
n = Convert.ToInt32(Console.ReadLine());
int[] a = new int[++n];
Console.WriteLine("Podaj wartosc a: ");
for (int i = 0; i < n; i++)
{
Console.WriteLine("a [" + i + "] = ");
a[i] = Convert.ToInt32(Console.ReadLine());
}
int x;
Console.WriteLine("Podaj x:");
x = Convert.ToInt32(Console.ReadLine());
int Horner;
Horner = a[0];
for (int i = 1; i < n; i++)
{
Horner = Horner * (i - 1) * x + a[i];
}
Console.WriteLine("Wynik to:" + Horner);
Console.ReadLine();
}
}
}
は、これが第二の選択肢でありますコードを計算しますが、カウントはすべて間違っている:私は+ +(再帰アルゴリズムの形で)Cから元のコードを書き換えてみたかった
Func<int, int> Horner = null;
Horner = (i) => (i == 0) ? a[0] : Horner(i - 1) * x + a[i];
Console.WriteLine("Wynik to:" + Horner(x));
Console.ReadLine();
。
元のコードは次のようになります。私はそれを行う方法がわからないすでに
int Horner;
int n;
int *a = new int[n];
int x;
int main()
{
cout <<"Podaj stopień wielomianu: ";
cin >> n;
cin.ignore();
cout << "Podaj wartość a: \n";
for (int i = 0; i <= n; i++)
{
cout <<"a[" <<i<<"] = ";
cin >> a[i];
cin.ignore();
}
cout <<"Podaj x: ";
cin >> x;
cin.ignore();
cout <<"Wynik to: " << Horner(n);
getchar();
return 0;
}
int Horner (int i)
{
if (i == 0)
return a[0];
else
return Horner (i - 1) * x + a[i];
}
...まだ同じ場所でさまよう...
あなたは何を求めているのかはっきりしていません – sll
本当にコードで何をしようとしていますか?これはユーザー入力を受け付けるアプリケーションですか?あなたのカウントがオフになっている場合も、forループを見てください。C#はインデックスに基づいて0ですので、1でカウントを開始するように設定している場合は、<ステートメントを変数にする必要があります。 – MethodMan