按照分解多项式的方法做就行了,简单题目
#include <stdio.h>
#include <stack>
using namespace std;
stack<int> s;
char buffer[400];
int a[10005];
int b[10005];
void func(int n, int k) //n代表p(s)里面的最高次幂的次数
{
int i;
long long r;
for(i=n-1; i>=0; i--)
{
if(i == n-1)
{
b[i] = a[n];
continue;
}
else
{
b[i] = a[i+1]+k*b[i+1];
continue;
}
}
r = a[0] + k*b[0];
printf("q(x):");
for(i=n-1; i>=0; i--)
printf(" %d", b[i]);
printf("\n");
printf("r = %d\n\n", r);
}
int main(void)
{
int k, num;
int count;
char ch;
while(gets(buffer))
{
sscanf(buffer, "%d", &k);
while(!s.empty())
s.pop();
while(1)
{
scanf("%d", &num);
s.push(num);
ch = getchar();
if(ch == '\n')
break;
}
count = 0;
while(!s.empty())
{
a[count++] = s.top();
s.pop();
}
func(count-1, k);
}
}