1002. A+B for Polynomials

C++语言: Codee#25843
01 /*
02 +++++++++++++++++++++++++++++++++++++++
03                 author: chm
04 +++++++++++++++++++++++++++++++++++++++
05 */
06
07 #include <map>
08 #include <set>
09 #include <list>
10 #include <queue>
11 #include <cmath>
12 #include <stack>
13 #include <bitset>
14 #include <cstdio>
15 #include <cctype>
16 #include <string>
17 #include <vector>
18 #include <cassert>
19 #include <cstdlib>
20 #include <cstring>
21 #include <fstream>
22 #include <sstream>
23 #include <iomanip>
24 #include <iostream>
25 #include <algorithm>
26
27 using namespace std;
28
29 FILE*            fin         = stdin;
30 FILE*            fout         = stdout;
31 const int        max_size     = 10086;
32
33 float buf[max_size];
34 #define ONLINE_JUDGE
35 int main()
36 {
37 #ifndef ONLINE_JUDGE
38     freopen("d:\\in.txt", "r", stdin);
39     freopen("d:\\out.txt", "w", stdout);
40 #endif
41     /*
42        数据小 直接数组通过
43        */
44     int n, m;
45     int a;
46     float b;
47     while(scanf("%d", &n) != EOF)
48     {
49         memset(buf, 0, sizeof(buf));
50         for(int i = 0; i < n; ++i)                          //read,add
51         {
52             scanf("%d%f", &a, &b);
53             buf[a] += b;
54         }
55         scanf("%d", &m);
56         int nonzero = 0;
57         for(int i = 0; i < m; ++i)                            //read ,add
58         {
59             scanf("%d%f", &a, &b);
60             buf[a] += b;
61         }
62         for(int i = 0; i <= 1000; ++i)
63             if(fabs(buf[i]) > 1e-6)
64                 ++nonzero;
65         printf("%d%s", nonzero, nonzero ? " " : "\n");
66         for(int i = 1000, cnt = 0; i >= 0; --i)          //print,form large to small
67             if(fabs(buf[i]) > 1e-6)
68                 printf("%d %.1f%s", i, buf[i], ++cnt == nonzero ? "\n" : " ");
69     }
70
71 #ifndef ONLINE_JUDGE
72     fclose(stdout);
73     system("start d:\\check.exe d:\\out.txt d:\\ans.txt");
74 #endif
75     return 0;
76 }
posted @ 2012-03-16 15:54  strorehouse  阅读(194)  评论(0编辑  收藏  举报