zoukankan      html  css  js  c++  java
  • pta l2-18(多项式A除以B)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984

    题意:给定两个多项式,求出其做除法运算的商和余数。

    思路:记下除数、被除数的最大指数,按照多项式除法的算法模拟即可,要注意的是这里的舍入操作是四舍五入,而不是向下取整,还有浮点运算存在误差,比如对浮点数f进行判零操作应该:abs(f)<0.0000001,因为你初始化f=0,在计算机中实际可能存的是0.00...01。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=100005;
     5 int m1,m2,n,t1,t2,n1,n2;
     6 double cc;
     7 double c1[maxn],c2[maxn],c3[maxn];
     8 
     9 int main(){
    10     scanf("%d",&n);
    11     for(int i=0;i<n;++i){
    12         scanf("%d%d",&t1,&t2);
    13         if(i==0) m1=t1;
    14         c1[t1]=t2;
    15     }
    16     scanf("%d",&n);
    17     for(int i=0;i<n;++i){
    18         scanf("%d%d",&t1,&t2);
    19         if(i==0) m2=t1,cc=t2;
    20         c2[t1]=t2;
    21     }
    22     for(int i=m1;i>=m2;--i)
    23         if(c1[i]<=-0.0000001||c1[i]>=0.0000001){
    24             c3[i-m2]=c1[i]/cc;
    25             for(int j=m2-1;j>=0;--j)
    26                 if(c2[j]<=-0.0000001||c2[j]>=0.0000001)
    27                     c1[i-m2+j]-=c1[i]*c2[j]/cc;
    28         }
    29     for(int i=m1-m2;i>=0;--i)
    30         if(abs(c3[i])+0.05>=0.1)
    31             ++n1;
    32     for(int i=m2-1;i>=0;--i)
    33         if(abs(c1[i])+0.05>=0.1)
    34             ++n2;
    35     printf("%d",n1);
    36     if(!n1)
    37         printf(" 0 0.0");
    38     for(int i=m1-m2;i>=0;--i)
    39         if(abs(c3[i])+0.05>=0.1)
    40             printf(" %d %.1f",i,c3[i]);
    41     printf("
    %d",n2);
    42     if(!n2)
    43         printf(" 0 0.0");
    44     for(int i=m2-1;i>=0;--i)
    45         if(abs(c1[i])+0.05>=0.1)
    46             printf(" %d %.1f",i,c1[i]);
    47     printf("
    ");
    48     return 0;
    49 }
  • 相关阅读:
    2021NUAA暑假集训 Day3 题解
    2021NUAA暑假集训 Day2 题解
    2021NUAA暑期模拟赛部分题解
    CodeForces 1038D Slime
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 10689 Yet another Number Sequence
    HDU 4549 M斐波那契数列
    HDU 4990 Reading comprehension
    CodeForces 450B Jzzhu and Sequences
  • 原文地址:https://www.cnblogs.com/FrankChen831X/p/10571163.html
Copyright © 2011-2022 走看看