码迷,mamicode.com
首页 > 其他好文 > 详细

PAT-甲级-1001-A+B for Polynomials

时间:2018-10-12 01:19:28      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:rip   color   class   The   msu   pts   int   line   ...   

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:

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???? (,) are the exponents and coefficients, respectively. It is given that 1,0.

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



分析:
开一个1001的数组,直接用下标代替指数,存好两行数后相加,

输出数组内非零数即可

 1 #include<stdio.h>
 2 #define N 1001
 3 
 4 int main(){
 5   float v1[N]={0},v2[N]={0},res[N]={0};
 6   int x,n;
 7   scanf("%d",&n);
 8   for(int i=0;i<n;++i){
 9       scanf("%d",&x);
10       scanf("%f",v1+x);
11   }
12   scanf("%d",&n);
13   for(int i=0;i<n;++i){
14       scanf("%d",&x);
15       scanf("%f",v2+x);
16   }
17   int cnt=0;
18   for(int i=0;i<N;++i){
19     res[i]=v1[i]+v2[i];
20     if(res[i]!=0)
21       cnt++;
22   }
23   printf("%d",cnt);
24   for(int i=N;i>0;i--)
25   {
26     if(res[i-1]!=0)
27         printf(" %d %.1f",i-1,res[i-1]);
28   }
29   return 0;
30 }

 

 

PAT-甲级-1001-A+B for Polynomials

标签:rip   color   class   The   msu   pts   int   line   ...   

原文地址:https://www.cnblogs.com/tenjl-exv/p/9775874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!