PAT A1015-1016

A 1015 Reversible Primes

  看清题意即可。给的数是十进制的,需要先判断是不是素数,然后按照给定进制转化成字符串后进行翻转,最后再转化为十进制并判断是否为素数。

PAT A1015-1016
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string>
 5 #include <cstdlib>
 6 #include <cmath>
 7 #include <cstring>
 8 using namespace std;
 9 
10 int N, D;
11 char numStr[20];
12 bool isPrime(int tmpNum)
13 {
14     if(tmpNum < 2)
15         return false;
16     int maxNum = sqrt(1.0*tmpNum);
17     for(int i = 2; i <= maxNum; ++ i)
18         if(tmpNum % i == 0)
19             return false;
20     return true;
21 }
22 int strToNum()
23 {
24     int len = strlen(numStr), tmpNum = 0;
25     for(int i = 0; i < len; ++ i)
26     {
27         tmpNum = tmpNum*D + numStr[i]-'0';
28     }
29     return tmpNum;
30 }
31 int main()
32 {
33     int tmpNum;
34     while(scanf("%d%d", &N, &D) != EOF && N >= 0)
35     {
36         if(!isPrime(N))
37         {
38             cout << "No" << endl;
39             continue;
40         }
41         for(int i = 0; N > 0; ++ i)
42         {
43             numStr[i] = N % D + '0';
44             N /= D;
45             numStr[i+1] = '\0';
46         }
47         tmpNum = strToNum();
48         if(!isPrime(tmpNum))
49             cout << "No" << endl;
50         else
51             cout << "Yes" << endl;
52     }
53     return 0;
54 }
View Code

A 1016 1016 Phone Bills

 因为存在无效的记录,所以需要将他们先进行配对。处理方法是:按照名字和时间进行排序,然后筛选出所需要的记录再进行下一步处理。

 超时问题:每一个有效时间段的话费计算需要尽量减少计算量。

以下是柳婼精简的代码:

PAT A1015-1016
 1 #include <iostream>
 2 #include <map>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 struct node {
 7     string name;
 8     int status, month, time, day, hour, minute;
 9 };
10 bool cmp(node a, node b) {
11     return a.name != b.name ? a.name < b.name : a.time < b.time;
12 }
13 double billFromZero(node call, int *rate) {
14     double total = rate[call.hour] * call.minute + rate[24] * 60 * call.day;
15     for (int i = 0; i < call.hour; i++)
16         total += rate[i] * 60;
17     return total / 100.0;
18 }
19 int main() {
20     int rate[25] = {0}, n;
21     for (int i = 0; i < 24; i++) {
22         scanf("%d", &rate[i]);
23         rate[24] += rate[i];
24     }
25     scanf("%d", &n);
26     vector<node> data(n);
27     for (int i = 0; i < n; i++) {
28         cin >> data[i].name;
29         scanf("%d:%d:%d:%d", &data[i].month, &data[i].day, &data[i].hour, &data[i].minute);
30         string temp;
31         cin >> temp;
32         data[i].status = (temp == "on-line") ? 1 : 0;
33         data[i].time = data[i].day * 24 * 60 + data[i].hour * 60 + data[i].minute;
34     }
35     sort(data.begin(), data.end(), cmp);
36     map<string, vector<node> > custom;
37     for (int i = 1; i < n; i++) {
38         if (data[i].name == data[i - 1].name && data[i - 1].status == 1 && data[i].status == 0) {
39             custom[data[i - 1].name].push_back(data[i - 1]);
40             custom[data[i].name].push_back(data[i]);
41         }
42     }
43     for (auto it : custom) {
44         vector<node> temp = it.second;
45         cout << it.first;
46         printf(" %02d\n", temp[0].month);
47         double total = 0.0;
48         for (int i = 1; i < temp.size(); i += 2) {
49             double t = billFromZero(temp[i], rate) - billFromZero(temp[i - 1], rate);
50             printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", temp[i - 1].day, temp[i - 1].hour, temp[i - 1].minute, temp[i].day, temp[i].hour, temp[i].minute, temp[i].time - temp[i - 1].time, t);
51             total += t;
52         }
53         printf("Total amount: $%.2f\n", total);
54     }
55     return 0;
56 }
View Code

 

上一篇:PAT乙级1016


下一篇:006、浏览器窗口大小设置