chenxi16

导航

PAT (Basic Level) Practice (中文) 1010 一元多项式求导

 1 #include<stdio.h>
 2 #define MAXN 100000
 3 struct poly{
 4     int exp,var;
 5 };
 6 struct poly a[MAXN];
 7 void read();
 8 void deal();
 9 void print();
10 int main(){
11     read();
12     deal();
13     print();
14     return 0;
15 }
16 void read(){
17     int e,v,count=0;
18     
19     while(scanf("%d %d",&v,&e)!=EOF){
20         a[++count].exp = e;
21         a[count].var = v;
22     }
23     a[0].exp = count;
24     a[0].var = count;
25     return;
26 
27 }
28 void deal(){
29     int e,v,ne,nv;
30     for(int i=1;i<=a[0].exp;i++){
31         e = a[i].exp;
32         v = a[i].var;
33          nv = e * v;
34          ne = e-1;
35         a[i].exp = ne;
36         a[i].var = nv;
37     }
38 
39     //合并同类
40     for(int i=1;i<=a[0].exp;i++){
41         for(int j=i+1;j<=a[0].exp;j++){
42             if(a[i].exp==a[j].exp){
43                 a[i].var += a[j].var;
44                 a[j].var = 0;
45                 a[j].exp = 0;
46             }
47             else if (a[i].exp>a[j].exp) break;
48             
49         }
50     }
51 }
52 void print(){
53     int count=0;
54     for(int i=1;i<=a[0].exp;i++)
55         if(a[i].var!=0) count++;
56     if(count==0) {printf("0 0\n");return;}
57     for(int i=1;i<=a[0].exp;i++){
58         if(a[i].var!=0){
59             printf("%d %d",a[i].var,a[i].exp);
60             count--;
61             if(count!=0) printf(" ");
62         }
63     }
64     
65     
66 }
View Code

windows  vs 模拟EOF ctrl + z

posted on 2020-04-13 23:41  chenxi16  阅读(121)  评论(0编辑  收藏  举报