fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node{
  5. int row;
  6. int col;
  7. int val;
  8. struct node* next;
  9. };
  10.  
  11. struct node* add_element(struct node* tail,int row,int col,int val)
  12. {
  13. struct node* new=(struct node*)malloc(sizeof(struct node));
  14. new->row=row;
  15. new->col=col;
  16. new->val=val;
  17. new->next=NULL;
  18. tail=new;
  19. return tail;
  20. }
  21.  
  22. void sorted_matrix(struct node* head,int row,int col,int val)
  23. {
  24. struct node* new=(struct node*)malloc(sizeof(struct node));
  25. new->row=row;
  26. new->col=col;
  27. new->val=val;
  28. new->next=NULL;
  29. struct node* pre = head;
  30. struct node* current = head->next;
  31.  
  32. while (pre->next!= NULL && current->row < row)
  33. {
  34. pre = current;
  35. current = current->next;
  36. }
  37.  
  38. while (current != NULL && current->row == row && current->col < col)
  39. {
  40. pre = current;
  41. current = current->next;
  42. }
  43.  
  44. if (pre == NULL)
  45. {
  46. new->next = head->next;
  47. head->next = new;
  48. }
  49. else
  50. {
  51. pre->next = new;
  52. new->next = current;
  53. }
  54. }
  55.  
  56. void sorted_by_col(struct node* M,int row,int col,int val)
  57. {
  58. struct node* current=M->next;
  59. struct node* pre=M;
  60. struct node* new=(struct node*)malloc(sizeof(struct node));
  61.  
  62. new->row=row;
  63. new->col=col;
  64. new->val=val;
  65. new->next=NULL;
  66. if(pre->next==NULL)
  67. {
  68. pre->next=new;
  69. }
  70. else if(pre->next!=NULL)
  71. {
  72. while(current->next!=NULL)
  73. {
  74. if(col<current->col)
  75. {
  76. pre->next=new;
  77. new->next=current;
  78. break;
  79. }
  80. current=current->next;
  81. pre=pre->next;
  82. }
  83. if(col<current->col)
  84. {
  85. pre->next=new;
  86. new->next=current;
  87. }
  88. else
  89. current->next=new;
  90. }
  91. }
  92.  
  93. void sum_matrix(struct node* add,struct node* M1,struct node* M2)
  94. {
  95. struct node* cur1=M1->next;
  96. struct node* cur2=M2->next;
  97. struct node* add_tail=add->next;
  98. struct node* add_pre=add;
  99. while (cur1!=NULL && cur2!=NULL)
  100. {
  101. if(cur1->col==cur2->col && cur1->row==cur2->row)
  102. {
  103. if(cur1->val+cur2->val!=0)
  104. {
  105. add_tail=add_element(add,cur1->row,cur1->col,cur1->val+cur2->val);
  106. add_pre->next=add_tail;
  107. add_tail=add_tail->next;
  108. add_pre=add_pre->next;
  109. }
  110. cur1=cur1->next;
  111. cur2=cur2->next;
  112. }
  113. else if(cur1->row<cur2->row || (cur1->row==cur2->row && cur1->col<cur2->col))
  114. {
  115. add_tail=add_element(add,cur1->row,cur1->col,cur1->val);
  116. add_pre->next=add_tail;
  117. add_tail=add_tail->next;
  118. add_pre=add_pre->next;
  119. cur1=cur1->next;
  120. }
  121. else
  122. {
  123. add_tail=add_element(add,cur2->row,cur2->col,cur2->val);
  124. add_pre->next=add_tail;
  125. add_tail=add_tail->next;
  126. add_pre=add_pre->next;
  127. cur2=cur2->next;
  128. }
  129. }
  130. while(cur1!=NULL)
  131. {
  132. add_tail=add_element(add,cur1->row,cur1->col,cur1->val);
  133. add_pre->next=add_tail;
  134. add_tail=add_tail->next;
  135. add_pre=add_pre->next;
  136. cur1=cur1->next;
  137. }
  138. while(cur2!=NULL)
  139. {
  140. add_tail=add_element(add,cur2->row,cur2->col,cur2->val);
  141. add_pre->next=add_tail;
  142. add_tail=add_tail->next;
  143. add_pre=add_pre->next;
  144. cur2=cur2->next;
  145. }
  146. }
  147.  
  148. struct node* multi(struct node* M1, struct node* M2) {
  149. struct node* result = (struct node*)malloc(sizeof(struct node));
  150. result->row = M1->next->row;
  151. result->col = M2->next->col;
  152. result->next = NULL;
  153. struct node* temp_res;
  154. int sum;
  155. for(struct node* temp_1 =M1->next;temp_1!=NULL;temp_1=temp_1->next)
  156. {
  157. for(struct node* temp_2=M2->next;temp_2!= NULL;temp_2=temp_2->next)
  158. {
  159. if(temp_1->col == temp_2->row) {
  160. sum = 0;
  161. temp_res = result->next;
  162. while(temp_res != NULL) {
  163. if(temp_res->row == temp_1->row && temp_res->col == temp_2->col)
  164. {
  165. sum = temp_res->val;
  166. break;
  167. }
  168. temp_res = temp_res->next;
  169. }
  170. sum += temp_1->val * temp_2->val;
  171. if(temp_res != NULL)
  172. {
  173. temp_res->val = sum;
  174. }
  175. else
  176. {
  177. sorted_matrix(result,temp_1->row,temp_2->col,sum);
  178. }
  179. }
  180. }
  181. }
  182. return result;
  183. }
  184.  
  185. int main()
  186. {
  187.  
  188. char* oper=(char *)malloc(6);
  189. int m,n,k;
  190. scanf("%s",oper);
  191. if(oper[0]=='T' && oper[1]=='R' && oper[2]=='A')
  192. {
  193. struct node* M=(struct node*)malloc(sizeof(struct node));
  194. scanf("%d %d %d",&m,&n,&k);
  195. for(int i=0;i<k;i++)
  196. {
  197. int row,col,val;
  198. scanf("%d %d %d",&row,&col,&val);
  199. sorted_by_col(M,row,col,val);
  200. }
  201. M=M->next;
  202. printf("%d\n",k);
  203. if(k!=0)
  204. {while(M->next!=NULL)
  205. {
  206. printf("%d %d %d\n",M->col,M->row,M->val);
  207. M=M->next;
  208. }
  209. printf("%d %d %d\n",M->col,M->row,M->val);
  210. }
  211. free(M);
  212. }
  213. else if(oper[0]=='A' && oper[1]=='D' && oper[2]=='D')
  214. {
  215. int m,n,k1,k2;
  216. scanf("%d %d %d %d",&m,&n,&k1,&k2);
  217. struct node* M1=(struct node*)malloc(sizeof(struct node));
  218. struct node* M2=(struct node*)malloc(sizeof(struct node));
  219. struct node* tail_1=M1->next;
  220. struct node* tail_2=M1->next;
  221. struct node* pre_1=M1;
  222. struct node* pre_2=M2;
  223.  
  224. int row,col,val;
  225. for(int i=0;i<k1;i++)
  226. {
  227. scanf("%d %d %d",&row,&col,&val);
  228. tail_1=add_element(tail_1,row,col,val);
  229. pre_1->next=tail_1;
  230. tail_1=tail_1->next;
  231. pre_1=pre_1->next;
  232. }
  233. for(int i=0;i<k2;i++)
  234. {
  235. scanf("%d %d %d",&row,&col,&val);
  236. tail_2=add_element(M2,row,col,val);
  237. pre_2->next=tail_2;
  238. tail_2=tail_2->next;
  239. pre_2=pre_2->next;
  240.  
  241. }
  242. struct node* sum=(struct node*)malloc(sizeof(struct node));
  243. sum_matrix(sum,M1,M2);
  244. struct node* dummy=sum->next;
  245. int count=0;
  246. while(dummy!=NULL)
  247. {
  248. count++;
  249. dummy=dummy->next;
  250. }
  251. sum=sum->next;
  252. printf("%d\n",count);
  253. while(sum->next!=NULL)
  254. {
  255. printf("%d %d %d\n",sum->row,sum->col,sum->val);
  256. sum=sum->next;
  257. }
  258. printf("%d %d %d\n",sum->row,sum->col,sum->val);
  259. free(sum);
  260. free(pre_1);
  261. free(pre_2);
  262. free(tail_1);
  263. free(tail_2);
  264. free(M1);
  265. free(M2);
  266. }
  267. else if(oper[0]=='M' && oper[1]=='U' && oper[2]=='L')
  268. {
  269. int m,n,l,k1,k2;
  270. struct node* M1=(struct node*)malloc(sizeof(struct node));
  271. struct node* M2=(struct node*)malloc(sizeof(struct node));
  272. struct node* tail_1=M1->next;
  273. struct node* tail_2=M1->next;
  274. struct node* pre_1=M1;
  275. struct node* pre_2=M2;
  276. scanf("%d %d %d %d %d",&m,&n,&l,&k1,&k2);
  277. int row,col,val;
  278. for(int i=0;i<k1;i++)
  279. {
  280. scanf("%d %d %d",&row,&col,&val);
  281. tail_1=add_element(tail_1,row,col,val);
  282. pre_1->next=tail_1;
  283. tail_1=tail_1->next;
  284. pre_1=pre_1->next;
  285. }
  286. for(int i=0;i<k2;i++)
  287. {
  288. scanf("%d %d %d",&row,&col,&val);
  289. tail_2=add_element(M2,row,col,val);
  290. pre_2->next=tail_2;
  291. tail_2=tail_2->next;
  292. pre_2=pre_2->next;
  293. }
  294. struct node* mult=(struct node*)malloc(sizeof(struct node));
  295. struct node* mult_next=mult->next;
  296. struct node* pre_mult=mult;
  297. mult_next=multi(M1,M2);
  298. pre_mult=mult_next;
  299. mult=mult_next;
  300. pre_mult=pre_mult->next;
  301. struct node* dummy=mult->next;
  302. int count=0;
  303. while(dummy!=NULL)
  304. {
  305. if(dummy->val!=0)
  306. {count++;}
  307. dummy=dummy->next;
  308. }
  309. mult=mult->next;
  310. printf("%d\n",count);
  311. while(mult->next!=NULL)
  312. {
  313. if(mult->val!=0)
  314. {printf("%d %d %d\n",mult->row,mult->col,mult->val);}
  315. mult=mult->next;
  316. }
  317. if(mult->val!=0)
  318. {printf("%d %d %d\n",mult->row,mult->col,mult->val);}
  319. free(M1);
  320. free(M2);
  321. free(mult);
  322. }
  323. }
Success #stdin #stdout 0s 5444KB
stdin
Standard input is empty
stdout
Standard output is empty