题目链接: http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1482&cid=1180
关键在于对字符串的操作。
两种情况:
‘+’
此时要把x和y指数均相同的项的系数相加。若链表中不存在系数相同的,则按优先级顺序插入当前项。
‘*’
把任意两项(不属于同一式子的两项)都要相乘。系数相乘,指数相加。
最后要考虑一下存在 0 的情况。
优先级关系。
x的指数越大优先级越高。
x的指数相同时,比较y。
常数优先级最低。
特例:x > x^ny^m
AC_code
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 char sen[1010]; 8 struct N 9 { 10 int x1,x2,y1,y2; 11 struct N *next; 12 } mark[1010]; 13 14 N *creat() 15 { 16 N *p = (struct N *)malloc(sizeof(struct N)); 17 p->next = NULL; 18 return p; 19 } 20 21 void insert_mul(N *head,N *p) 22 { 23 p->next = head->next; 24 head->next = p; 25 } 26 27 void insert_add(N *head,N *p) 28 { 29 N *q1 = head; 30 N *q2 = head->next; 31 while(q2 != NULL) 32 { 33 if(q2->x2 == p->x2 && q2->y2 == p->y2) 34 { 35 q2->x1 += p->x1; 36 return ; 37 } 38 if(p->x2 != 0 || p->y2 != 0) 39 { 40 41 if(q2->x2 < p->x2) 42 { 43 break; 44 } 45 else if(q2->x2 == p->x2) 46 { 47 if(p->y2 == 0) 48 break; 49 else if(q2->y2 < p->y2 && ( q2->y2 != 0 || (q2->x2 == 0 && q2->y2 == 0) ) ) 50 break; 51 } 52 } 53 q1 = q1->next; 54 q2 = q2->next; 55 } 56 p->next = q1->next; 57 q1->next = p; 58 } 59 60 int cal(char *s) 61 { 62 int ans,sum,l; 63 for(ans = 1,sum = 0,l = strlen(s) - 1; l >= 0; l--) 64 { 65 sum += ans*(s[l]-'0'); 66 ans *= 10; 67 } 68 return sum; 69 } 70 71 void check_number(char *s,char order,struct N *head) 72 { 73 char num[30]; 74 int mark,top; 75 int ans; 76 int i,l; 77 N *p = creat(); 78 p->x1 = 0; 79 p->x2 = 0; 80 p->y1 = 0; 81 p->y2 = 0; 82 for(mark = 1,ans = 1,i = 0,l = strlen(s),top = 0; i <= l; i++) 83 { 84 if(i != l && '0' <= s[i] && s[i] <= '9') 85 { 86 num[top++] = s[i]; 87 } 88 else 89 { 90 num[top] = '