C++解决 很急

1

Description

Johnny Q最心爱的女孩GJ被其情敌CK掳到了HFUT翡翠湖城堡，为了夺回自己的爱人，Johnny Q依然决定深入虎穴救出GJ

Input

Output

Sample Input

2

1

2

Sample Output

X+Y

X^2+2XY+Y^2

-1

ki表示第i项的系数，i的取值范围：0-n，并且有k0=kn=1, k1=k(n-1)=n，k2=k(n-2)

0

```static void Main(string[] args)
{
BinomialTheorem(1);
BinomialTheorem(2);
BinomialTheorem(3);
BinomialTheorem(4);
BinomialTheorem(5);
BinomialTheorem(6);
/*
X+Y
X^2+2XY+Y^2
X^3+3X^2Y+3XY^2+Y^3
X^4+4X^3Y+6X^2Y^2+4XY^3+Y^4
X^5+5X^4Y+10X^3Y^2+10X^2Y^3+5XY^4+Y^5
X^6+6X^5Y+15X^4Y^2+20X^3Y^3+15X^2Y^4+6XY^5+Y^6
*/
}

public static void BinomialTheorem(int n)
{
var index= GetIndex(n);
n++;
for (var j = 1; j < index.GetLength(1); j++)
{
if (index[n, j] != 0)
{
if (j == 1)
{
Console.Write("X" + (n - 1 > 1 ? "^" + (n - 1) : ""));
}
else if (j > 1 && index[n, j] == 1)
{
Console.Write("+Y" + (n - 1 > 1 ? "^" + (n - 1) : ""));
}
else
{
Console.Write("+" + index[n, j] + "X" + (n - j > 1 ? "^" + (n - j) : "") + "Y" + (j-1 > 1 ? "^" + (j-1) : ""));
}
}
}
Console.WriteLine();
}

public static int[,] GetIndex(int n)
{
n = n + 2;
int[,] arr = new int[n, n];
for (int i = 2; i < n; i++)
{
int j = i;
for (int k = 1; k <= j; k++)
{
if (k == 1)
{
arr[j, k] = 1;
}
else if (k == j)
{
arr[j, k] = 1;
}
else
{
arr[j, k] = arr[j - 1, k - 1] + arr[j - 1, k];
}
}
}
return arr;
}```
artwl | 园豆：16536 (专家六级) | 2012-06-01 18:07

artwl感谢代劳了。

您需要登录以后才能回答，未注册用户请先注册