PAT-B1010 & A1002 & A1009 一元多项式运算

题目B1010【求导】:https://www.patest.cn/contests/pat-b-practise/1010

题目A1002【相加】:https://www.patest.cn/contests/pat-a-practise/1002

题目A1009【相乘】:https://www.patest.cn/contests/pat-a-practise/1009

#include<cstdio>
#include<iostream>
using namespace std;

int main() {
    /* PAT B1010
    一元多项式 求导
    */
    int a[1010] = { 0 }, b[1001] = { 0 };
    int key = 0, count = 0;
    while (scanf("%d%d", &a[key], &b[key]) != EOF) {
        if (a[key] != 0) {
            a[key] *= b[key];
            b[key]--;
        }
        if (a[key] != 0) count++;
        key++;
    }
    for (int i = 0; i < key; i++) {
        if (a[i] != 0) {
            printf("%d %d", a[i], b[i]);

            if (a[i + 1] != 0)  //判断是否为最后一项
                printf(" ");
            else
                printf("\n");
        }
    }
    if (count == 0) printf("0 0\n"); //特例 - 无非零项
    system("pause");
    return 0;
}

PAT-1002

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cfloat>

#define maxSize 1000
using namespace std;

int main() {
    /*
     一元多项式 相加
     注:浮点数判零,虽然这道题用"=="也能ac,但正确的做法应该是用 DBL_EPSILON
    */

    double a[1010] = { 0.0 }, b[1001] = { 0.0 }, k;
    int count = 0, num1, num2, e;
    //input
    scanf("%d", &num1);
    for (int i = 0; i < num1; i++){
        scanf("%d %lf", &e, &k);
        a[e] = k;
    }
    scanf("%d", &num2);
    for (int i = 0; i < num2; i++) {
        scanf("%d %lf", &e, &k);
        b[e] = k;
    }
    //从高次项 依次处理
    for (int i = maxSize; i >= 0; i--) {
        a[i] += b[i];
        //if (a[i] != 0) count++;      //浮点数 判0
        if (fabs(a[i]) > DBL_EPSILON) count++;
    }
    printf("%d", count);
    for (int i = maxSize; i >= 0; i--) {
        //if (a[i] != 0) printf(" %d %.1f", i, a[i]);
        if (fabs(a[i]) > DBL_EPSILON) 
            printf(" %d %.1f", i, a[i]);
    }
    printf("\n");

    system("pause");
    return 0;
}

PAT-A1009

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cfloat>

#define maxSize 2010
using namespace std;

int main() {
    /*
     一元多项式 相乘
     注:浮点数判零,虽然这道题用"=="也能ac,但正确的做法应该是用 DBL_EPSILON
    */

    double a[maxSize + 10] = { 0.0 }, b[maxSize + 10] = { 0.0 };
    double c[maxSize + 10] = { 0.0 }, k;
    int count = 0, num1, num2, e;
    //input
    scanf("%d", &num1);
    for (int i = 0; i < num1; i++){
        scanf("%d %lf", &e, &k);
        a[e] = k;
    }
    scanf("%d", &num2);
    for (int i = 0; i < num2; i++) {//其实这一层时,可以边输入边处理
        scanf("%d %lf", &e, &k);
        b[e] = k;
    }
    //从高次项 依次处理
    for (int i = maxSize; i >= 0; i--) { //两层循环,遍历
        if (fabs(a[i]) > DBL_EPSILON) {
            for (int j = maxSize; j >= 0; j--) {
                if (fabs(b[j]) > DBL_EPSILON) {
                    int key = i + j;
                    c[key] += (a[i] * b[j]);                
                }
            }
        }
    }
    for (int i = maxSize; i >= 0; i--) //获取非零项数
        if (fabs(c[i]) > DBL_EPSILON) 
            count++;
    //output
    printf("%d", count);
    for (int i = maxSize; i >= 0; i--) {
        if (fabs(c[i]) > DBL_EPSILON) printf(" %d %.1lf", i, c[i]);
    }
    printf("\n");

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26398495/article/details/79105166