Uva 10719 - Quotient Polynomial (多项式求解)

Problem B

Quotient Polynomial

Time Limit

2 Seconds

A polynomial of degree n can be expressed as

If k is any integer then we can write:

Here q(x) is called the quotient polynomial of p(x) of degree (n-1) and r is any integer which is called the remainder.

For example, if p(x) = x3 - 7x2+ 15x - 8 and k = 3 then q(x) = x2 - 4x + 3 and r = 1. Again if p(x) = x3 - 7x2+ 15x - 9 and k = 3 thenq(x) = x2 - 4x + 3 and r = 0.

In this problem you have to find the quotient polynomial q(x) and the remainder rAll the input and output data will fit in 32-bit signed integer.

Input
Your program should accept an even number of lines of text. Each pair of line will represent one test case. The first line will contain an integer value for k. The second line will contain a list of integers (an, an-1, … a0), which represent the set of co-efficient of a polynomialp(x). Here 1 ≤ n ≤ 10000. Input is terminated by <EOF>.

Output
For each pair of lines, your program should print exactly two lines. The first line should contain the coefficients of the quotient polynomial. Print the reminder in second line. There is a blank space before and after the ‘=’ sign. Print a blank line after the output of each test case. For exact format, follow the given sample.

Sample Input

Output for Sample Input

3
1 -7 15 -8
3
1 -7 15 -9

q(x): 1 -4 3
r = 1

q(x): 1 -4 3
r = 0

 

题意:

输入k,再输入一行整数 (an, an-1, … a0)

叫你根据

If k is any integer then we can write:

计算出q(x)的系数。

这题是个多项式求解的问题,只要找到规律就很简单了。

a4x4+a3x3+a2x2+a1x1+a0=x(b3x3+b2x2+b1x1+b0)-k*(b3x3+b2x2+b1x1+b0)+r
a4x4+a3x3+a2x2+a1x1+a0=b3x4+(b2-kb3)x3+(b1-kb2)x2+(b0-kb1)x+r-kbo;
1  2  3  4  5
a4 a3 a2 a1 a0
a[n-i];
an=bn-1;
a0=r-kb0;
a1=b0-kb1;
a2=b1-kb2;
a3=b2-kb3
a4=b3
bn-1=an+kbn
由于读入是倒序的所以下标转化下即可

#include<stdio.h>
#include<math.h>

const int N = 10010;
int a[N],b[N];
int main() {
	int k;
	int r;
	char ch;
	while(scanf("%d",&k) != EOF) {
		int count = 0;
		do{
			scanf("%d",&a[count]);
			count++;
		}while((ch = getchar())!='\n');
		b[1] = a[0];
		for(int i=1; i < count;i++) {
			b[i+1] = a[i] + k*b[i];
		}
		r = a[count-1] + k*b[count-1];
		printf("q(x): ");
		for(int i=1;i<count-1;i++)
			printf("%d ",b[i]);
		printf("%d\n",b[count-1]);
		printf("r = %d\n\n",r);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了方便,把代码放在Word里面了,每次上机实验的题目代码都在。 第一次: 对如下多项式编写类定义: + + +…+ 其中,n为多项式的次数。完成如下功能: (1) 可存储任意大的多项式(提示:可用动态数组实现)。 (2) 定义构造函数、析构函数、拷贝构造函数。 (3) 包含一个static成员存储定义的多项式的数量。 (4) 定义一个成员函数输出多项式。(可参照-x^4-6x^3+5格式输出) (5) 定义一个成员函数计算多项式的值。 (6) 写main函数测试类的功能。 (7) 采用多文件实现。 考虑:哪些成员函数可以声明为const. 第二次: (8) 重载“+”运算符,实现两个多项式相加。 (9) 重载“-”运算符,实现两个多项式相减。 (10) 重载“*”运算符,实现两个多项式相乘。 (11) 重载“=”运算符,实现两个多项式的赋值运算。 考虑:把其中某个运算符重载为友元函数。 第三次: C++的一般编译器都定义和封装了字符串功能,请模仿定义string类的实现,可以实现并支持如下功能: (1)string s = “吉林大学”; (2)string t = s; (3)string m; m = t; (4)m.legnth() 函数测量字符串的长度 (5)m.cat(string const &)连接字符串 第四次: 我公司为仪器生产企业,目前生产摄像机和行车记录仪两种产品,分别销售给用户。 摄像机包含摄像、图像质量设定、编码算法等属性。 将摄像机增加相应芯片(具有操作菜单、自动拍摄、车速传感器、源代码等功能)后,形成一个行车记录仪。 要求: 设计摄像机类,并请根据下列不同的功能要求,采用不同的继承方式,设计行车记录仪类,并添加测试代码,体验不同继承方式下的成员访问属性。(类设计时可根据需要自行添加数据成员和其他成员函数。) (1) 行车记录仪的芯片可以使用摄像机的摄像、图像质量设定功能。 行车记录仪用户可以操作行车记录仪的操作菜单和摄像机的摄像功能。 (2)行车记录仪的芯片可以使用摄像机的拍摄、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单。 (3) 行车记录仪的芯片可以使用摄像机的拍摄、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单 同时其他公司购买行车记录仪,因该公司也用于销售,不得泄露其全部内容 课后: (1)采用组合方式设计行车记录仪类,增加相应测试代码,体验继承和组合的关系。 (2)分别为继承和组合方式下为各类添加构造函数、析构函数,增加相应测试代码,体验对象的初始化和构造顺序。 (3)将摄像机类和行车记录仪类功能相近的函数(如拍摄、编码等功能函数)设为同名函数,增加相应测试代码,体验同名函数覆盖。 (4)为我公司建立一个多态的产品类层次结构,使用抽象类,测试时,创建一个基类指针的容器,通过基类指针调用虚函数,体验多态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值