多项式合并: F(x)=8x7+7x4+3x2+x+2x-2+8x-3 G(x)=9x6+2x5+5x4-x+5
来源:互联网 发布:龙岗做网络推广公司 编辑:程序博客网 时间:2024/05/17 02:57
多项式合并:
F(x)=8x7+7x4+3x2+x+2x-2+8x-3
G(x)=9x6+2x5+5x4-x+5
======================================================
源代码:
#include <iostream>
using namespace std;
#include <list>
class Term
{
public:
Term(int c,int e):coef(c),exp(e){}
int getcoef();
int getexp();
private:
int coef;
int exp;
};
int Term::getcoef()
{
return coef;
}
int Term::getexp()
{
return exp;
}
int main()
{
void sort(list<Term> alist);
void merge(list<Term> alist,list<Term> blist);
list<Term> alist,blist;
int array1[]={8,7,3,1,2,8,9,2,5,-1,5};
int array2[]={7,4,2,1,-2,-3,6,5,4,1,0};
int i,j;
for(i=0;i<=5;i++)
{
Term t1(array1[i],array2[i]);
alist.push_back(t1);
}
cout<<"F(x)=";
sort(alist);
for(j=6;j<=10;j++)
{
Term t2(array1[j],array2[j]);
blist.push_back(t2);
}
cout<<"G(x)=";
sort(blist);
merge(alist,blist);
return 0;
}
void merge(list<Term> alist,list<Term> blist)
{
void sort(list<Term> alist);
list<Term> clist;
list<Term>::iterator a,b,c;
a=alist.begin();
b=blist.begin();
c=clist.begin();
while(a!=alist.end() && b!=blist.end())
{
if((*a).getexp() == (*b).getexp()){
Term t3((*a).getcoef()+(*b).getcoef(),(*a).getexp());
clist.push_back(t3);
a++;
b++;
}else if((*a).getexp() > (*b).getexp()){
clist.push_back(*a);
a++;
}else{
clist.push_back(*b);
b++;
}
if(a==alist.end()){
while(b!=blist.end()){
clist.push_back(*b);
b++;
}
}else if(b==blist.end()){
while(a!=alist.end()){
clist.push_back(*a);
a++;
}
}
}
cout<<"合并多项式:"<<endl<<"M(x)=";
sort(clist);
}
void sort(list<Term> alist)
{
list<Term>::iterator a;
for(a=alist.begin();a!=alist.end();a++)
{
if((*a).getcoef()!=0){
if( ((a!=alist.begin()) && ((*a).getcoef()>0)) ){
if( (*a).getcoef()==1 ){
cout<<"+";
}else{
cout<<"+"<<(*a).getcoef();
}
}else{
if((*a).getcoef()!=1){
if((*a).getcoef()==-1){
cout<<"-";
}else{
cout<<(*a).getcoef();
}
}
}
if((*a).getexp() != 0){
if((*a).getexp()==1){
cout<<"x";
}else{
cout<<"x^"<<(*a).getexp();
}
}
}
}
cout<<endl;
}
- 多项式合并: F(x)=8x7+7x4+3x2+x+2x-2+8x-3 G(x)=9x6+2x5+5x4-x+5
- 用弦截法求方程 f(x)=x^3-5x^2+16x-80=0 的根
- bitmap求哈密顿距离-给定N(1<=N<=100000)个五维的点A(x1,x2,x3,x4,x5),求两个点X(x1,x2,x3,x4,x5)和Y(
- nbut [1377] I Hate Math 输出f(x) = 2x^5 + 3x^4 + 4x^3 + 5x^2 - kx的最小解
- f(x) = e^(-x) * cos(2*PI*x)源代码
- 输出(x/1!)+(x*x*x/3!)+(x*x*x*x*x/5!)+````
- x^1 + x ^2 +...+x ^n = m 求x
- 1^x + 2 ^ x + 3 ^x + …… + n^x
- 计算机辅助设计Mastercam X-X4 各版本下载
- 计算sinx=x-x^3/3!+x^5/5!-x^7/7!+.........
- 计算s=x+x^2/2+x^3/3+...+x^n/n
- 递归实现f(x)=2f(x-1)+x^2
- 关于sin x = 1/x!-x/3!+x/5!-x/7!+……的c程序
- 杭电1098--f(x)=5*x^13+13*x^5+k*a*x
- E F M 3 2 - G 2 X X - D K 开 发 板 电路图
- Java求多项式exp(x)=1+x+ 2!/ x2 + 3!/x3 +……+ i!/xi (|i!/xi|≤10^-10)
- f(x) = sin(2x) x∈(-π,π) 源代码
- x=x&(x-1)
- 利用数组和链表实现优先队列
- 【操作系统】进程间通信
- Cocos2d-x 3.1.1 学习日志5--cocos2d-x3.1.1打飞机的实现
- OCP 1Z0 053 138
- 黑马程序员-分类以及类的本质研究
- 多项式合并: F(x)=8x7+7x4+3x2+x+2x-2+8x-3 G(x)=9x6+2x5+5x4-x+5
- Remove Nth Node From End of List
- jpa pageutil
- LDAP基础
- 算法竞赛入门经典(第2版)习题3-10 盒子 Box UVa1587
- [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)
- Centos杂项
- 给定表达式为:a*b+(c-d)/e 建立表达式二叉树
- 数据库进程之从属进程详解