A polynomial is divided by B PTA-

A polynomial is divided by B

This is still a question about A / B, only A and B are replaced by a polynomial. You need to calculate two polynomials division quotient Q and a remainder R, wherein R must be less than the order of the order of B.

Input formats:

Enter two rows, each row is given a non-zero polynomial, given first A, then B. give Format each line as follows:

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

Wherein Nis the number of non-zero entries of the polynomial, e[i]the first iexponential non-zero entries, c[i]the first icoefficient of non-zero entries. The descending order of the index is given to ensure that the index is not the same for all non-negative integer, all the coefficients are non-zero integers, all integers within the range of integers.

Output formats:

It has two lines output quotient and a remainder, the same as the input format and output format, a retention coefficient output after the decimal point. Among peers figures separated by a space, the line from beginning to end may not have the extra space. Note: Zero is a special polynomial polynomial corresponding output 0 0 0.0. But not non-zero output of the zero polynomial coefficients (including rounded, 0.0) of the item. In the sample, there are actually more than polynomial constant terms -1/27, but because of its rounded to 0.0, it is not output.

Sample input:

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

Sample output:

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

I know how happy I am to see this? ! Yeah I debug a night 555, which is a method of pure Hand personally think, to sum up, first of all, is the integer integer type 32767, and then test when the associated documents with it, do not look at the question to the wrong format and when input with scanf is fast, but you have to think what is good data format% on what, though lost an integer, but to double output, you have to first lf, otherwise behind a bunch of weird I do not know is wrong what, then no problem rounding so write:

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

And then there's a bunch of careless, such as are considered finished and then when that did not count the number of output, rather than the sort of row index coefficient, etc., designed to test the sample! careful! I do not know this question is worthwhile to spend so much time, should probably practice code debugging on the problem, look at the positive solution of this problem? Bigwigs have to see tomorrow how to write

#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;
}
    
    
    

Today saw the solution to a problem of big brother, a sister school like Zhejiang University (www.liuchuo.net), really powerful, see her blog feelings too deep, really enrich the university flies, admire! Not only algorithm, strong competition thief, a bunch of certificates, also wrote several books, not only will knock code also learn of doing things, and so are very good, get blog recorded the college life, super strong. She a core process is complete, the subscript is the index, the content is a coefficient, the coefficient may be zero filled, then A [i] that a direct subtraction of the calculated current supplier, a [i] - = a [t1] / b [t2] (coefficient) * b [i- (t1- t2)] (t1-t2 is the highest power of the current index, can ensure the correspondence), and then Save Low cycle (for), until t1 <t2 up (while), Lift her original address code :(: 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;
}

Guess you like

Origin www.cnblogs.com/IamIron-Man/p/11930165.html