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 <algorithm>
 7 
 8 using namespace std;
 9 long long strRadixToNum(long long n, long long radix)
10 {
11     char numStr[11];
12     long long tmpNum = 0, ant = 1;
13     sprintf(numStr, "%lld", n);
14     for(int i = strlen(numStr)-1; i >= 0; -- i)
15     {
16         numStr[i] > '9' ? tmpNum += (numStr[i] - 'a' + 10)*ant : tmpNum += (numStr[i] - '0')*ant;
17         if(tmpNum < 0 || ant < 0)
18             return -1;
19         ant *= radix;
20     }
21     return tmpNum;
22 }
23 bool oneDigitFlag = false;
24 long long minRadix(long long n)
25 {
26     char numStr[11];
27     long tmpNum = 0;
28     sprintf(numStr, "%lld", n);
29     if(strlen(numStr) == 1)
30         oneDigitFlag = true;
31     for(long long i = 0; i < strlen(numStr); ++ i)
32         if(numStr[i] > tmpNum)
33             tmpNum = numStr[i];
34     return tmpNum > '9' ? tmpNum - 'a' + 11 : tmpNum - '0' + 1;
35 }
36 int main()
37 {
38     long long N1, N2, tag, radix, tmpNum, tmpTarget, lowRadix, highRadix;
39     cin >> N1 >> N2 >> tag >> radix;
40     //算出进制已经确定的数作为目标值
41     //并且 找出未确定进制的数的最小进制
42     if(tag == 2)
43         swap(N1, N2);
44     highRadix = tmpTarget = strRadixToNum(N1, radix);
45     lowRadix = minRadix(N2);
46     if(oneDigitFlag && lowRadix - 1 == tmpTarget)
47     {
48         lowRadix > 2 ? cout << lowRadix : cout << 2;
49         return 0;
50     }
51     while(lowRadix <= highRadix)
52     {
53         long long midRadix = (lowRadix + highRadix)/2;
54         long long tmpNum1 = strRadixToNum(N2, midRadix);
55         if(tmpNum1 == -1)
56              highRadix = midRadix - 1;
57         else if(tmpNum1 == tmpTarget)
58         {
59             cout << midRadix;
60             return 0;
61         }
62         else if(tmpNum1 > tmpTarget)
63             highRadix = midRadix - 1;
64         else
65             lowRadix = midRadix + 1;
66     }
67     cout << "Impossible";    
68     return 0;
69 }
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

猜你喜欢

转载自www.cnblogs.com/codewars/p/11355373.html