C++编程问题请大神赐教谢谢！ 5C

double px(int n) {
double c;
if (n == 0)
c = 1;
if (n == 1)
c ='x';
if(n>1)
c = ((2 * n - 1)*'x'*px(n - 1) - (n - 1)*px(n - 2)) / n;
return c;

}

int main()
{
int n;
while (1) {
cout << "please enter n:" << endl;
cin >> n;
cout << "c=:" << px(n) << endl;
}
return 0;
}

9个回答

cout << "c=:" << px(n) << endl;这个语句换成cout << "c=:" << (char)px(n) << endl。

#include
using namespace std;

double px(int n) {
double c;
if (n == 0)
c = 1;
if (n == 1)
c ='x';
if(n>1)
c = ((2 * n - 1)*'x'*px(n - 1) - (n - 1)*px(n - 2)) / n;
return c;

}

int main()
{
int n;
while (1) {
cout << "please enter n:" << endl;
cin >> n;
cout << "c=:" << (char)px(n) << endl;
}
return 0;
}

c 定义成int，再检查一下c的表达式有没有错误

”c = ((2 * n - 1)*'x'*px(n - 1) - (n - 1)*px(n - 2)) / n; “ 这里面的第一个x是被转换成ascII码参与运算的，而且是对应十进制120。如果是想输出表达式的话，
cout<<"c =" <<(”表达式“)<<"x"<<(”表达式“)<<<endl; 这样写应该可以（中文字符记得替换成你要写的表达式）。

`````` #include<iostream>

using namespace std;
double px(int n) {
double c;
if (n == 0)
c = 1;
if (n == 1)
c = 'x';
if (n > 1)
c = ((2 * n - 1)*'x'*px(n - 1) - (n - 1)*px(n - 2)) / n;
return c;

}

int main()
{
int n;
while (1) {
cout << "please enter n:" << endl;
cin >> n;
cout << "c=:" << (char)px(n) << endl;
}
return 0;
}
``````

`````` double px(int n) {
double c;
if (n == 0)
c = 1;
if (n == 1)
c ='x';
if(n>1)
c = ((2 * n - 1)*'x'*px(n - 1) - (n - 1)*px(n - 2)) / n;
return c;
}
int main()
{
int n;
while (1) {
cout << "please enter n:" << endl;
cin >> n;
cout << "c=:" << px(n) << endl;/*cout<<"c=:";
double xx=px(n);
if(xx==120)
cout<<'x';
else cout<<xx;
*/
}
return 0;
}
``````

int main()
{
int n;
while (1)
{
cout << "please enter n:" << endl;
cin >> n;
if(n==1)
{
cout << "c=:" <<(char) px(n) << endl; //这里，如果n==1，那么px(n)=120,然后把double类型的120强制转换成char类型，就是字符‘x'
}
else
{
cout << "c=:" << px(n) << endl;
}

``````            }
``````

return 0;
}