PAT A1005-1008

A 1005 Spell It Right (20 point(s))

  25分的题目,比较简单,注意N的范围,用字符串处理即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     const char * numStr[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
13     string tmpStr;
14     cin >> tmpStr;
15     int sum = 0;
16     for(int i = 0; i < tmpStr.size(); ++ i)
17         sum += tmpStr[i]-'0';
18     char charArray[10];
19     sprintf(charArray, "%d", sum);
20     for(int i = 0; i < strlen(charArray); ++ i)
21     {
22         if(i > 0) printf(" ");
23         printf("%s", numStr[charArray[i]-'0']);
24     }
25     return 0;
26 }
View Code

A 1006 1006 Sign In and Sign Out (25 point(s))

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     int M, tmpHour, tmpMinute, tmpSecond, unLockTime=24*3600, lockTime=-1;
13     cin >> M;
14     string tmpStr, unLockId, lockId;
15     for(int i = 0; i < M; ++i)
16     {
17         cin >> tmpStr;
18         scanf("%d:%d:%d", &tmpHour, &tmpMinute, &tmpSecond);
19         tmpHour = tmpHour*3600+tmpMinute*60+tmpSecond;
20         if(tmpHour < unLockTime)
21         {
22             unLockId = tmpStr;
23             unLockTime = tmpHour;
24         }
25         scanf("%d:%d:%d", &tmpHour, &tmpMinute, &tmpSecond);
26         tmpHour = tmpHour*3600+tmpMinute*60+tmpSecond;
27         if(tmpHour > lockTime)
28         {
29             lockId = tmpStr;
30             lockTime = tmpHour;
31         }
32     }
33     cout << unLockId << " " << lockId;
34     return 0;
35 }
View Code

A 1007 Maximum Subsequence Sum (25 point(s))

  经典的最大子序列和问题,注意 全为负只有负数和0 这两种情况即可,理不清题意可能会有一两个点过不去。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     int K, tmpSt, resSt, resEnd, tmpNum, sum = -1, maxSum = -1;
13     cin >> K;
14     bool negFlag = true;
15     for(int i = 0; i < K; ++ i)
16     {    
17         cin >> tmpNum;
18         if(i==0)
19             resSt = tmpNum;
20         if(tmpNum >= 0)
21             negFlag = false;
22         if(sum < 0)
23         {
24             sum = 0;
25             tmpSt = tmpNum;
26         }
27         sum += tmpNum;
28         if(sum > maxSum)
29         {
30             maxSum = sum;
31             resSt = tmpSt;
32             resEnd = tmpNum;
33         }
34     }
35     if(negFlag)
36         cout << 0 << " " << resSt << " " << tmpNum;
37     else
38         cout << maxSum << " " << resSt << " " << resEnd;
39     return 0;
40 }
View Code

A 1008 Elevator (20 point(s))

  一个直观的数学计算问题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     int N, tmpNum, tmpFloor = 0, sum = 0;
13     cin >> N;
14     for(int i = 0; i < N; ++ i)
15     {
16         cin >> tmpNum;
17         if(tmpFloor < tmpNum)
18             sum += 5 + 6*(tmpNum - tmpFloor);
19         else
20             sum += 5 + 4*(tmpFloor - tmpNum);
21         tmpFloor = tmpNum;
22     }
23     cout << sum;
24     return 0;
25 }
View Code

猜你喜欢

转载自www.cnblogs.com/codewars/p/11348539.html
今日推荐