国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

1002. A+B for Polynomials (25)

2019-11-11 03:49:13
字體:
來源:轉載
供稿:網友

題目:

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

InputEach input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.OutputFor 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 Input2 1 2.4 0 3.22 2 1.5 1 0.5Sample Output3 2 1.5 1 2.9 0 3.2

題目地址:https://www.patest.cn/contests/pat-a-PRactise/1002

解答:

#include<stdio.h>struct PolyNode {  int expo;  double coef;};struct Polynomial {  int Size;  struct PolyNode Array[20];};int main(){  struct Polynomial P1, P2, P;  P.Size = 0;  scanf("%d", &P1.Size);  for (int i = 0; i < P1.Size; i++)    scanf("%d %lf", &P1.Array[i].expo, &P1.Array[i].coef);    scanf("%d", &P2.Size);  for (int i = 0; i < P2.Size; i++)    scanf("%d %lf", &P2.Array[i].expo, &P2.Array[i].coef);  int i = 0, j = 0;  while (i < P1.Size && j < P2.Size) {    if (P1.Array[i].expo > P2.Array[j].expo) {      P.Array[P.Size].coef = P1.Array[i].coef;      P.Array[P.Size].expo= P1.Array[i].expo;      i++;      P.Size++;    }    else if (P1.Array[i].expo < P2.Array[j].expo) {      P.Array[P.Size].coef = P2.Array[j].coef;      P.Array[P.Size].expo = P2.Array[j].expo;      j++;      P.Size++;    }    else {      P.Array[P.Size].expo = P1.Array[i].expo;      P.Array[P.Size].coef = P1.Array[i].coef + P2.Array[j].coef;      i++;      j++;      if (P.Array[P.Size].coef > 10e-6)        P.Size++;    }  }  while (i < P1.Size) {    P.Array[P.Size].coef = P1.Array[i].coef;    P.Array[P.Size].expo = P1.Array[i].expo;    i++;    P.Size++;  }  while (j < P2.Size) {    P.Array[P.Size].coef = P2.Array[j].coef;    P.Array[P.Size].expo = P2.Array[j].expo;    j++;    P.Size++;  }  printf("%d", P.Size);  for (i = 0; i < P.Size; i++) {    printf(" %d %.1f", P.Array[i].expo, P.Array[i].coef);  }  return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定日县| 辽阳县| 湘乡市| 长治市| 福海县| 高密市| 宜兴市| 将乐县| 佛坪县| 枝江市| 永修县| 靖西县| 区。| 璧山县| 岳池县| 资溪县| 咸丰县| 邓州市| 睢宁县| 巴东县| 志丹县| 贵港市| 南靖县| 阳山县| 克拉玛依市| 兴海县| 岢岚县| 建阳市| 土默特左旗| 肃宁县| 奉新县| 宜章县| 高青县| 五常市| 永德县| 乌拉特后旗| 邯郸市| 高雄县| 扎囊县| 长武县| 石林|