PAT A1002 PAT A1009

版权声明:敲一敲看一看,记得评论告诉我错误 https://blog.csdn.net/idealhunting/article/details/88051236

1002 A+B for Polynomials (25 分)

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

 《算法笔记》

#include<cstdio>
const int max_n=1111;
double p[max_n]={};
int main(){
    int k,n,count=0;
    double a;
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        scanf("%d %lf",&n,&a);
        p[n]+=a;
    }
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        scanf("%d %lf",&n,&a);
        p[n]+=a;
    }
    for(int i=0;i<max_n;i++){
        if(p[i]!=0){
            count++;
        }
    }
    printf("%d",count);
    for(int i=max_n-1;i>=0;i--){
        if(p[i]!=0)printf(" %d %.1f",i,p[i]);
    }
    return 0;
}

 ac:

#include<cstdio>
#include<cmath>
const int maxn=1001;
int main(){
    float A[maxn],B[maxn],C[maxn];
    for(int i=0;i<maxn;i++){
        A[i]=10001;
        B[i]=10001;
        C[i]=10001;
    }
    int a, b,temp;
    scanf("%d",&a);
    for(int i=0;i<a;i++){
        scanf("%d",&temp);
        scanf("%f",&A[temp]);
    }

    scanf("%d",&b);
    for(int i=0;i<b;i++){
        scanf("%d",&temp);
        scanf("%f",&B[temp]);
    }
    int H=0;
    for(int i=0;i<maxn;i++){
        if(10001!=(int)A[i]&&10001!=(int)B[i]){
           C[i]=B[i]+A[i];
           if((int)C[i]!=0)H++;
        }
        else if(10001!=(int)A[i]||10001!=(int)B[i]){
            if((int)A[i]!=10001)C[i]=A[i];
            else C[i]=B[i];
            if((int)C[i]!=0)H++;
        }
    }
    printf("%d",H);
    for(int i=maxn-1;i>=0;i--){
        if((int)C[i]!=10001&&(int)C[i]!=0){
            printf(" %d %.1f",i,C[i]);
        }
    }
    return 0; //float 大小比较问题~~~~~~~~!!!!!!!!!! 且没考虑到消去为0的情况
}//好久前写的代码了2333,仅做复时使用

1009 Product of Polynomials (25 分)

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

 《算法笔记》:

#include<cstdio>
struct Poly{
    int exp;//指数
    double cof;//系数
}poly[1001];//第一个多项式

double ans[2001];//存放结果
int main(){
    int n,m,number=0;
    scanf("%d",&n);      //第一个多项式中非零系数的项数
    for(int i=0;i<n;i++){
        scanf("%d %lf",&poly[i].exp,&poly[i].cof);
    }
    scanf("%d",&m);//第二个多项式中非零系数的项数
    for(int i=0;i<m;i++){
        int exp;
        double cof;
        scanf("%d %lf",&exp,&cof);//第二个多项式的指数和系数
        for(int j=0;j<n;j++){//与第一个多项式中的每一项相乘
            ans[exp+poly[j].exp]+=(cof*poly[j].cof);

        }
    }
    for(int i=0;i<=2000;i++){
        if(ans[i]!=0.0)number++;
    }
    printf("%d",number);
    for(int i=2000;i>=0;i--){
        if(ans[i]!=0.0){
            printf(" %d %.1f",i,ans[i]);
        }
    }
    return 0;
}

ac:

#include<cstdio>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

int k,ex,en=0;
double cf;
struct Node{
    int e;
    double c;
};
vector<Node> a1,a2,ans;
map<int,int> p;
bool cmp(Node a,Node b){
    return a.e>b.e;
}

int main(){
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        scanf("%d %lf",&ex,&cf);
        a1.push_back(Node{ex,cf});
    }
    sort(a1.begin(),a1.end(),cmp);
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        scanf("%d %lf",&ex,&cf);
        a2.push_back(Node{ex,cf});
    }
    sort(a2.begin(),a2.end(),cmp);
    for(int i=0;i<a1.size();i++){
        for(int j=0;j<a2.size();j++){
            ex=a1[i].e+a2[j].e;
            cf=a1[i].c*a2[j].c;
            if(p.find(ex)==p.end()){
                ans.push_back(Node{ex,cf});
                p[ex]=en;
                en++;
            }
            else{
                int temp=p[ex];
                ans[temp].c+=cf;
            }
        }
    }

    sort(ans.begin(),ans.end(),cmp);
    for(vector<Node>::iterator it=ans.begin();it!=ans.end();it++){
        if((int)(*it).c==0)ans.erase(it);
    }
    printf("%d",ans.size());
    for(int i=0;i<ans.size();i++){
        if((int)ans[i].c!=0)printf(" %d %.1lf",ans[i].e,ans[i].c);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/idealhunting/article/details/88051236