PAT A1009-1012

A 1009 Product of Polynomials (25 point(s))

  读懂题意就行。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <vector>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 typedef struct NODE
 8 {
 9     int exp;
10     double coe;
11     NODE(){exp = 0; coe = 0;}
12     NODE(int e, double v):exp(e),coe(v){}
13 }node;
14 vector<node> poly1, poly2;
15 double rstPoly[2010]={0};
16 int main()
17 {
18     int N, tmpExp;
19     double tmpCoe;
20     cin >> N;
21     for(int i = 0; i < N; ++ i)
22     {
23         cin >> tmpExp >> tmpCoe;
24         poly1.push_back(NODE(tmpExp, tmpCoe));
25     }
26     cin >> N;
27     for(int i = 0; i < N; ++ i)
28     {
29         cin >> tmpExp >> tmpCoe;
30         poly2.push_back(NODE(tmpExp, tmpCoe));
31     }
32     for(int i = 0; i < poly1.size(); ++i)
33         for(int j = 0; j < poly2.size(); ++ j)
34             rstPoly[poly1[i].exp+poly2[j].exp] += poly1[i].coe*poly2[j].coe;
35     int cnt = 0;
36     for(int i = 0; i < 2010; ++ i)
37         if(rstPoly[i] != 0)
38             cnt++;
39     cout << cnt;
40     for(int i = 2000; i >= 0; -- i)
41         if(rstPoly[i] != 0)
42         {
43             printf(" %d %.1f", i, rstPoly[i]);
44         }
45     return 0;
46 }
View Code

A 1010 Radix (25 point(s))

  这道题目相当有趣,坑的莫名其妙,哈哈哈。

  注意:1.数的范围,10位数字,int 不够用

     2.超时,因而不能从小到大递增取进制数

       3.二分法,最大进制和最小进制的确定

       4.超限,在找进制数的过程中,过大的进制数会发生超限,注意这种情况

     5.待确定进制数只有一位,此时需要注意取最小进制

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <string>
 7 #include <algorithm>
 8 
 9 using namespace std;
10 long long toNum(char c)
11 {
12     return isdigit(c) ? c-'0' : c-'a'+10;
13 }
14 long long strRadixToNum(string tmpStr, long long radix)
15 {
16     long long tmpNum = 0, ant = 1;
17     for(int i = tmpStr.size()-1; i >= 0; -- i)
18     {
19         tmpNum += toNum(tmpStr[i])*ant;
20         if(tmpNum < 0 || ant < 0)
21             return -1;
22         ant *= radix;
23     }
24     return tmpNum;
25 }
26 long long minRadix(string tmpStr)
27 {
28     char tmpChar = 0;
29     for(int i = 0; i < tmpStr.size(); ++ i)
30     {
31         if(tmpStr[i] > tmpChar)
32             tmpChar = tmpStr[i];
33     }
34     return toNum(tmpChar) + 1;
35 }
36 int main()
37 {
38     long long tag, radix, tmpNum, tmpTarget, lowRadix, highRadix;
39     string N1, N2;
40     cin >> N1 >> N2 >> tag >> radix;
41     //算出进制已经确定的数作为目标值
42     //并且 找出未确定进制的数的最小进制
43     if(tag == 2)
44         swap(N1, N2);
45     highRadix = tmpTarget = strRadixToNum(N1, radix);
46     lowRadix = max((long long)2 , minRadix(N2));
47     while(lowRadix <= highRadix)
48     {
49         long long midRadix = (lowRadix + highRadix)/2;
50         long long tmpNum1 = strRadixToNum(N2, midRadix);
51         if(tmpNum1 >= tmpTarget ||tmpNum1 == -1)
52              highRadix = midRadix - 1;
53         else
54             lowRadix = midRadix + 1;
55     }
56     if(strRadixToNum(N2, lowRadix) == tmpTarget)
57         cout << lowRadix;
58     else
59         cout << "Impossible";    
60     return 0;
61 }
View Code

 

A 1011 World Cup Betting (20 point(s))

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <algorithm>
 7 
 8 using namespace std;
 9 int main()
10 {
11     double sum = 0.65, tmpW, tmpT, tmpL;
12     for(int i = 0; i < 3; ++ i)
13     {
14         cin >> tmpW >> tmpT >> tmpL;
15         if(tmpW > max(tmpT, tmpL))
16         {
17             cout << "W ";
18             sum *= tmpW;
19         }
20         else if(tmpT > max(tmpW, tmpL))
21         {
22             cout << "T ";
23             sum *= tmpT;
24         }
25         else
26         {
27             cout << "L ";
28             sum *= tmpL;;
29         }
30     }
31     printf("%.2f", (sum-1)*2);
32     return 0;
33 }
View Code

A 1012 The Best Rank (25 point(s))

  注意:1.排名的问题,相同成绩同一排名

     2.最佳排名相同,按所给优先级

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <algorithm>
 7 #include <set>
 8 #include <unordered_map>
 9 #include <functional>
10 
11 using namespace std;
12 typedef struct NODE
13 {
14     int id, math, cCode, aver, english;
15 }node;
16 vector<int> cCodeVec, mathVec, engVec, averVec;
17 unordered_map<int, node> nodeMap;
18 unordered_map<int, int> nodeTableMap;
19 void getBestRank(int u)
20 {
21     int type = 0, rank, cnt;
22     char typeStr[5]="ACME";
23     node tmpNode = nodeMap[u];
24     cnt = 1;
25     for(auto it = averVec.begin(); it != averVec.end(); ++it, ++cnt)
26         if(*it == tmpNode.aver)
27         {
28             type = 0; rank = cnt;break;
29         }
30     cnt = 1;
31     for(auto it = cCodeVec.begin(); it != cCodeVec.end(); ++it, ++cnt)
32         if(*it == tmpNode.cCode && cnt < rank)
33         {
34             type = 1; rank = cnt;break;
35         }
36     cnt = 1;
37     for(auto it = mathVec.begin(); it != mathVec.end(); ++it, ++cnt)
38         if(*it == tmpNode.math && cnt < rank)
39         {
40             type = 2; rank = cnt;break;
41         }
42     cnt = 1;
43     for(auto it = engVec.begin(); it != engVec.end(); ++it, ++cnt)
44         if(*it == tmpNode.english && cnt < rank)
45         {
46             type = 3; rank = cnt;break;
47         }
48     printf("%d %c\n", rank, typeStr[type]);
49 }
50 int main()
51 {
52     int N, M;
53     node tmpNode;
54     cin >> N >> M;
55     for(int i = 0; i < N; ++i)
56     {
57         cin >> tmpNode.id >> tmpNode.cCode >> tmpNode.math >> tmpNode.english;
58         tmpNode.aver = (tmpNode.cCode + tmpNode.math + tmpNode.english+1.5)/3;
59         cCodeVec.push_back(tmpNode.cCode); mathVec.push_back(tmpNode.math);
60         engVec.push_back(tmpNode.english); averVec.push_back(tmpNode.aver);
61         nodeMap[tmpNode.id] = tmpNode;nodeTableMap[tmpNode.id] = 1;
62     }
63     sort(averVec.begin(), averVec.end(), greater<int>());
64     sort(cCodeVec.begin(), cCodeVec.end(), greater<int>());
65     sort(mathVec.begin(), mathVec.end(), greater<int>());
66     sort(engVec.begin(), engVec.end(), greater<int>());
67     for(int i = 0; i < M; ++ i)
68     {
69         cin >> tmpNode.id;
70         if(nodeTableMap[tmpNode.id] > 0)
71             getBestRank(tmpNode.id);
72         else
73             cout << "N/A" << endl;
74     }
75     return 0;
76 }
View Code

 

posted on 2019-08-14 23:20  ChangeUp  阅读(160)  评论(0)    收藏  举报

导航