Quotient Polynomial
2013-12-27 01:42
274 查看
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 then q(x) = x2 - 4x + 3 and r = 0.
In this problem you have to find the quotient polynomial q(x) and the remainder r. All 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.
[align=left][/align]
这个题其实并不难 一开始的那次错误是我想复杂了 其实只要想通了算法很简单 开始的时候扯得太远了 水题一个 上代码
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
using namespace std;
const int maxn= 10001;
int main()
{
char ch;
int a[maxn],n,k,r,b[maxn];
while(scanf("%d",&k)!=EOF)
{
int i=1,flag=1;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
while (scanf("%d%c",&a[i],&ch)) //不要直接输入整串字符串 那样往外取数太麻烦了
{
if (ch=='\n') break;
i++;
}
n=i;
b[n-1]=a[1];
for(i=n-2; i>=0; i--)
{
b[i]=k*b[i+1]+a[n-i];
}
printf("q(x):");
for (i=n-1; i>0; i--)
printf(" %d",b[i]);
printf("\n");
printf("r = %d\n",b[0]);
printf("\n");
}
return 0;
}
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 then q(x) = x2 - 4x + 3 and r = 0.
In this problem you have to find the quotient polynomial q(x) and the remainder r. All 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 |
这个题其实并不难 一开始的那次错误是我想复杂了 其实只要想通了算法很简单 开始的时候扯得太远了 水题一个 上代码
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
using namespace std;
const int maxn= 10001;
int main()
{
char ch;
int a[maxn],n,k,r,b[maxn];
while(scanf("%d",&k)!=EOF)
{
int i=1,flag=1;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
while (scanf("%d%c",&a[i],&ch)) //不要直接输入整串字符串 那样往外取数太麻烦了
{
if (ch=='\n') break;
i++;
}
n=i;
b[n-1]=a[1];
for(i=n-2; i>=0; i--)
{
b[i]=k*b[i+1]+a[n-i];
}
printf("q(x):");
for (i=n-1; i>0; i--)
printf(" %d",b[i]);
printf("\n");
printf("r = %d\n",b[0]);
printf("\n");
}
return 0;
}
相关文章推荐
- 动易2006序列号破解算法公布
- C#数据结构与算法揭秘二
- 浅析STL中的常用算法
- JavaScript 组件之旅(二)编码实现和算法
- 将15位身份证补全为18位身份证的算法示例详解
- C++算法系列之日历生成的算法代码
- 1 2 3 4 5 6 7 8 9 = 110的java实现
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- 【代码】Pythonの代码片段
- STL中算法
- 数据结构&算法学习
- 算法的时间复杂度
- 算法导论:选择排序的原理与实现
- PHP实现四种常用的排序算法
- 图解插入排序算法
- 一些常见算法的JavaScript实现
- 平方根sqrt()函数的底层算法效率
- 二叉搜索树的一些相关算法介绍
- 欧几里德算法(辗转相处法)练手
- 面试中常见的一些算法问题