PTA-多项式A除以B

1|0多项式A除以B

这仍然是一道关于A/B的题,只不过A和B都换成了多项式。你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数。

1|0输入格式:

输入分两行,每行给出一个非零多项式,先给出A,再给出B。每行的格式如下:

N e[1] c[1] ... e[N] c[N]

其中N是该多项式非零项的个数,e[i]是第i个非零项的指数,c[i]是第i个非零项的系数。各项按照指数递减的顺序给出,保证所有指数是各不相同的非负整数,所有系数是非零整数,所有整数在整型范围内。

1|0输出格式:

分两行先后输出商和余,输出格式与输入格式相同,输出的系数保留小数点后1位。同行数字间以1个空格分隔,行首尾不得有多余空格。注意:零多项式是一个特殊多项式,对应输出为0 0 0.0。但非零多项式不能输出零系数(包括舍入后为0.0)的项。在样例中,余多项式其实有常数项-1/27,但因其舍入后为0.0,故不输出。

1|0输入样例:

4 4 1 2 -3 1 -1 0 -1 3 2 3 1 -2 0 1

1|0输出样例:

3 2 0.3 1 0.2 0 -1.0 1 1 -3.1

知道我看到这个有多高兴吗?!我调试了一晚上呀555,这是纯手打亲自想的一个方法,总结一下,首先,整型是integer型,32767,然后测试的时候关联文件都加上吧,看题给的格式不要错了,然后输入的时候用scanf是快,但是你得想好数据是什么格式就%什么,虽然输的是整数,但是给double输,就得先lf,不然后面一堆奇怪错误我也不知道为啥,然后四舍五入这么写没问题:

ans_xishu[i] = (double) ((int)(ans_xishu[i]*10 + (ans_xishu[i]<0?-0.5:0.5)))/10;

然后还有一堆粗心,比如都算完了然后输出没算的那个数,排序的时候排指数而不是系数,等等,设计测试样例!细心!我也不知道这道题花这么多时间值不值得,可能应该在难题上练代码调试,这种题看看正解?明天看看大佬们都怎么写的

#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<algorithm> using namespace std; struct dxs{ int zhishu; double xishu; }c[33000]; bool cmp(dxs d1,dxs d2) { return d1.zhishu>d2.zhishu; } int main() { // freopen("test.in","r",stdin); // freopen("test.out","w",stdout); int a_zhishu[33000],b_zhishu[33000],temp_zhishu[33000],shuchu; double temp_xishu[33000],b_xishu[33000]; double ans_xishu[33000],a_xishu[33000]; int ans_zhishu[33000]; bool temp_biaoji[33000],pd,vo; int n1,n2,i,num=0,start,tot=0; cin>>n1; for (i=1;i<=n1;i++) scanf("%d%lf",&a_zhishu[i],&a_xishu[i]); cin>>n2; for (i=1;i<=n2;i++) scanf("%d%lf",&b_zhishu[i],&b_xishu[i]); while (n1!=0 and a_zhishu[1]>=b_zhishu[1]) { start=1; num++; ans_zhishu[num]=a_zhishu[1]-b_zhishu[1]; ans_xishu[num]=a_xishu[1]/b_xishu[1]; for (i=1;i<=n2;i++) { temp_zhishu[i]=ans_zhishu[num]+b_zhishu[i]; temp_xishu[i]=ans_xishu[num]*b_xishu[i]; temp_biaoji[i]=0; } for (i=1;i<=n1;i++) { pd=false; for (int j=start;j<=n2 and temp_zhishu[j]>=a_zhishu[i];j++) if (a_zhishu[i]==temp_zhishu[j]) { pd=true; if (a_xishu[i]!=temp_xishu[j]) { c[++tot].zhishu=a_zhishu[i]; c[tot].xishu=a_xishu[i]-temp_xishu[j]; } temp_biaoji[j]=1; start++; break; } if (pd==false) { c[++tot].zhishu=a_zhishu[i]; c[tot].xishu=a_xishu[i]; } } for (i=1;i<=n2;i++) if (temp_biaoji[i]==0) { c[++tot].zhishu=temp_zhishu[i]; c[tot].xishu=(-1)*temp_xishu[i]; } sort(c+1,c+tot+1,cmp); for (int i=1;i<=tot;i++) { a_xishu[i]=c[i].xishu; a_zhishu[i]=c[i].zhishu; } n1=tot; tot=0; } for(int i=1;i<=num;i++) ans_xishu[i] = (double) ((int)(ans_xishu[i]*10 + (ans_xishu[i]<0?-0.5:0.5)))/10; for(int i=1;i<=n1;i++) a_xishu[i] = (double)((int)(a_xishu[i]*10 + (a_xishu[i]<0?-0.5:0.5)))/10; shuchu=num; for (i=1;i<=num;i++) if (ans_xishu[i]==0) shuchu--; if (shuchu==0) cout<<"0 0 0.0"<<endl; else { vo=true; printf("%d ",shuchu); for (i=1;i<=num;i++) if (ans_xishu[i]!=0) if (vo==true) { printf("%d %.1lf",ans_zhishu[i],ans_xishu[i]); vo=false; } else printf(" %d %.1lf",ans_zhishu[i],ans_xishu[i]); printf("\n"); } shuchu=n1; for (i=1;i<=n1;i++) if (a_xishu[i]==0) shuchu--; if (shuchu==0) cout<<"0 0 0.0"; else { printf("%d ",shuchu); vo=true; for (i=1;i<=n1;i++) if (a_xishu[i]!=0) if (vo==true) { printf("%d %.1lf",a_zhishu[i],a_xishu[i]); vo=false; } else printf(" %d %.1lf",a_zhishu[i],a_xishu[i]); } return 0; }

今天看了大佬的题解,是一个浙大的学姐好像是(www.liuchuo.net),果然厉害,看她博客感触也颇深,大学过得真的充实、佩服!不仅算法、比赛贼强,一堆证书,还自己写了几本书,不仅会敲代码还学习处事等等都非常好,拿博客记录了大学生活,超级强。她就一个核心过程就完成了,下标是指数,内容是系数,系数为零也填充,然后A[i]直接减当前算出来的那一项商,a[i]-=a[t1]/b[t2](系数)*b[i-(t1-t2)](t1-t2是当前最高次幂指数,就能保证一一对应了),然后从高到低循环减(for),一直到t1<t2为止(while),捧上她的代码:(原文地址:https://www.liuchuo.net/archives/5585)

#include <cstdio> #include <cmath> using namespace std; int nonNegativeNum(double c[], int start) { int cnt = 0; for (int i = start; i >= 0; i--) if (abs(c[i]) + 0.05 >= 0.1) cnt++; return cnt; } void printPoly(double c[], int start) { printf("%d", nonNegativeNum(c, start)); if (nonNegativeNum(c, start) == 0) printf(" 0 0.0"); for (int i = start; i >= 0; i--) if (abs(c[i]) + 0.05 >= 0.1) printf(" %d %.1f", i, c[i]); } double c1[3000], c2[3000], c3[3000]; int main() { int m = 0, n = 0, t = 0, max1 = -1, max2= -1; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &t); max1 = max1 > t ? max1 : t; scanf("%lf", &c1[t]); } scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &t); max2 = max2 > t ? max2 : t; scanf("%lf", &c2[t]); } int t1 = max1, t2 = max2; while (t1 >= t2) { double c = c1[t1] / c2[t2]; c3[t1 - t2] = c; for (int i = t1, j = t2; j >= 0; j--, i--) c1[i] -= c2[j] * c; while (abs(c1[t1]) < 0.000001) t1--; } printPoly(c3, max1 - max2); printf("\n"); printPoly(c1, t1); return 0; }


如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。

__EOF__

本文作者Liujiahang
本文链接https://www.cnblogs.com/IamIron-Man/p/11930165.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   IamIron-Man  阅读(935)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示