PAT A1005-1008

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

  25 points the subject, is relatively simple, note that N ranges , can be treated with a string.

 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))

  The classic sequence and the largest problem, pay attention to the whole negative number and only the negative and 0 in both cases can be, disarray that Italy may have one or two points to make life difficult.

 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))

  An intuitive mathematical problem

 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

 

Guess you like

Origin www.cnblogs.com/codewars/p/11348539.html