标签:call mes push amp ide false als ios node
算是第二次做这套题吧,感觉从上次考试到现在自己有了挺大提高,提前30min做完了。
7-1 Sexy Primes
读懂题意就行。
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 7 using namespace std; 8 bool isPrime(int n) 9 { 10 if(n < 2) 11 return false; 12 int tmpNum = (int)sqrt(1.0*n); 13 for(int i = 2; i <= tmpNum; ++ i) 14 { 15 if(n % i == 0) 16 return false; 17 } 18 return true; 19 } 20 int main() 21 { 22 int tmpNum, prime1 = -1; 23 cin >> tmpNum; 24 if(isPrime(tmpNum)) 25 { 26 if(tmpNum > 7 && isPrime(tmpNum-6)) 27 prime1 = tmpNum-6; 28 else if(isPrime(tmpNum+6)) 29 prime1 = tmpNum + 6; 30 } 31 prime1 == -1 ? cout << "No" << endl : cout << "Yes" << endl; 32 for(int i = tmpNum-5; prime1 == -1;i ++) 33 { 34 if(isPrime(i) && isPrime(i+6)) 35 { 36 if(i > tmpNum) 37 prime1 = i; 38 else 39 prime1 = i +6; 40 } 41 42 } 43 cout << prime1; 44 return 0; 45 }
7-2 Anniversary
也是读懂题意就行。
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <unordered_map> 7 #include <string> 8 #include <cstdlib> 9 using namespace std; 10 unordered_map<string, int> alumFlagMap; 11 int main() 12 { 13 int n, tmpNum, oldGuestNum = 999999999, oldAlumNum = 999999999; 14 string tmpStr, oldGuest, oldAlum; 15 cin >> n; 16 while(n--) 17 { 18 cin >> tmpStr; 19 alumFlagMap[tmpStr] = 1; 20 } 21 cin >> n; 22 int cnt = 0; 23 while(n--) 24 { 25 cin >> tmpStr; 26 if(alumFlagMap[tmpStr] > 0) 27 { 28 tmpNum = atoi(tmpStr.substr(6,8).c_str()); 29 if(tmpNum < oldAlumNum) 30 { 31 oldAlumNum = tmpNum; 32 oldAlum = tmpStr; 33 } 34 cnt ++; 35 } 36 else 37 { 38 tmpNum = atoi(tmpStr.substr(6,8).c_str()); 39 if(tmpNum < oldGuestNum) 40 { 41 oldGuestNum = tmpNum; 42 oldGuest = tmpStr; 43 } 44 } 45 } 46 cout << cnt << endl; 47 cnt == 0 ? cout << oldGuest << endl : cout << oldAlum << endl; 48 49 return 0; 50 }
7-3 Telefraud Detection
这道题目理清条件就行,先找到嫌疑人,在根据嫌疑人找团伙,团伙进行并查集处理。
1.嫌疑人:给大于K个人打短电话,且不超过20%的人回拨。
2.团伙:两个嫌疑人互相打过电话。
3.短电话:A拨打B电话的总时长不超过5分钟,认为A向B打短电话。
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <unordered_map> 7 #include <string> 8 #include <cstdlib> 9 #include <cstring> 10 using namespace std; 11 12 #define MAX_AMOUNT 1010 13 const int INF = 0x7f7f7f7f; 14 #define CLR(a,b) memset(a,b,sizeof(a)) 15 int callInfo[MAX_AMOUNT][MAX_AMOUNT]; 16 int fatherId[MAX_AMOUNT+1]; 17 int K, N, M; 18 vector<int> susPeopleVec; 19 vector<int> phoneNumCnt[MAX_AMOUNT]; 20 unordered_map<int, int> susPeopleMap; 21 int getFather(int u) 22 { 23 int tmpNum = u; 24 while(fatherId[tmpNum] != tmpNum) 25 tmpNum = fatherId[tmpNum]; 26 int tmpFa = tmpNum; 27 tmpNum = u; 28 while(fatherId[tmpNum] != tmpNum) 29 { 30 u = tmpNum; 31 tmpNum = fatherId[tmpNum]; 32 fatherId[u] = tmpFa; 33 } 34 return tmpFa; 35 } 36 int main() 37 { 38 int tmpNum, tmpSt, tmpEnd, tmpTime, susCnt = 0; 39 cin >> K >> N >> M; 40 CLR(callInfo, 0); 41 while(M--) 42 { 43 cin >> tmpSt >> tmpEnd >> tmpTime; 44 callInfo[tmpSt][tmpEnd] += tmpTime; 45 } 46 for(int i = 1; i <= N; ++ i) 47 { 48 int shortCallCnt = 0; 49 int rebackCnt = 0; 50 for(int j = 1; j <= N; ++ j) 51 { 52 if(callInfo[i][j] > 0 && callInfo[i][j] <= 5) 53 { 54 shortCallCnt ++; 55 if(callInfo[j][i] > 0) 56 rebackCnt ++; 57 } 58 } 59 if(shortCallCnt > K && 1.0*rebackCnt/shortCallCnt <= 0.2) 60 { 61 susPeopleMap[i] = 1;susCnt++;susPeopleVec.push_back(i); 62 } 63 } 64 for(int i = 1; i <= N; ++ i) 65 { 66 fatherId[i] = i; 67 } 68 int index; 69 for(int i = 0; i < susPeopleVec.size(); ++ i) 70 { 71 index = susPeopleVec[i]; 72 for(int j = 1; j <= N; ++ j) 73 { 74 if(susPeopleMap[j] == 0) 75 continue; 76 if(callInfo[index][j] > 0 && callInfo[j][index] > 0) 77 { 78 int tmpF1 = getFather(index); 79 int tmpF2 = getFather(j); 80 if(tmpF1 > tmpF2) 81 { 82 fatherId[tmpF1] = tmpF2; 83 getFather(index); 84 } 85 else 86 { 87 fatherId[tmpF2] = tmpF1; 88 getFather(j); 89 } 90 } 91 } 92 } 93 if(susPeopleVec.size() == 0) 94 { 95 cout << "None" << endl; 96 return 0; 97 } 98 for(int i = 0; i < susPeopleVec.size(); ++ i) 99 phoneNumCnt[fatherId[susPeopleVec[i]]].push_back(susPeopleVec[i]); 100 for(int i = 1; i <= N; ++ i) 101 { 102 if(phoneNumCnt[i].size() > 0) 103 { 104 bool symbolFlag = false; 105 for(auto it = phoneNumCnt[i].begin(); it != phoneNumCnt[i].end(); ++ it) 106 { 107 symbolFlag ? printf(" ") : symbolFlag = true; 108 printf("%d", *it); 109 } 110 printf("\n"); 111 } 112 } 113 return 0; 114 }
7-4 Structure of a Binary Tree
这种判断是否正确的题目,一般选择比较适合查找的顺序存储结构。
首先根据题目给出的后序和先序顺序,在数组中将该二叉树建立出来。
之后便是提取题目给出的结论中的数字,这个自己写个函数就可以搞定。
最后便是利用string.find()的查找功能确定结论是哪一种,然后进行判断(因为是顺序存储,判断会相当简单)。
(代码有点啰嗦,赶时间哈)
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <unordered_map> 7 #include <string> 8 #include <cstdlib> 9 #include <cstring> 10 using namespace std; 11 12 #define MAX_AMOUNT 1010 13 const int INF = 0x7f7f7f7f; 14 #define CLR(a,b) memset(a,b,sizeof(a)) 15 int N, M; 16 bool fullFlag = true; 17 vector<int> postVec, inorderVec; 18 vector<int> treeVec(10000); 19 unordered_map<int, int> nodeIndexMap; 20 bool createTree(int inL, int inR, int postL, int postR, int nodeIndex) 21 { 22 if(inL > inR || postL > postR) 23 return false; 24 int tmpNum = postVec[postR]; 25 int index = inL; 26 while(inorderVec[index] != tmpNum) 27 index++; 28 treeVec[nodeIndex] = tmpNum; 29 nodeIndexMap[tmpNum] = nodeIndex; 30 int childCnt = 0; 31 if(createTree(inL, index-1, postL, postL+index-1-inL, nodeIndex*2)) 32 childCnt ++; 33 if(createTree(index+1, inR, postR-inR+index, postR-1, nodeIndex*2+1)) 34 childCnt ++; 35 if(childCnt == 1) 36 fullFlag = false; 37 return true; 38 } 39 int getNumber(string tmpStr) 40 { 41 bool flag = false; 42 int index = 0; 43 while(index < tmpStr.size()) 44 { 45 if(isdigit(tmpStr[index])) 46 break; 47 index ++; 48 } 49 int endIndex = index; 50 while(endIndex < tmpStr.size()) 51 { 52 if(!isdigit(tmpStr[endIndex])) 53 { 54 break; 55 } 56 endIndex++; 57 } 58 return atoi(tmpStr.substr(index, endIndex-index).c_str()); 59 } 60 int main() 61 { 62 cin >> N; 63 int tmpNum; 64 postVec.resize(N); 65 inorderVec.resize(N); 66 for(int i = 0; i < N; ++ i) 67 cin >> postVec[i]; 68 for(int i = 0; i < N; ++ i) 69 cin >> inorderVec[i]; 70 71 createTree(0, N-1, 0, N-1, 1); 72 cin >> M; 73 getchar(); 74 string tmpStr; 75 while(M--) 76 { 77 getline(cin, tmpStr); 78 if(tmpStr[tmpStr.size()-1] == ‘t‘) 79 { 80 getNumber(tmpStr) == treeVec[1] ? printf("Yes\n") : printf("No\n"); 81 } 82 else if(tmpStr[0] == ‘I‘) 83 { 84 fullFlag ? printf("Yes\n") : printf("No\n"); 85 } 86 else if(tmpStr[tmpStr.size()-1] == ‘s‘) 87 { 88 int tmpNum1 = getNumber(tmpStr.substr(0,4)); 89 int tmpNum2 = getNumber(tmpStr.substr(4,10)); 90 tmpNum1 = nodeIndexMap[tmpNum1]; 91 tmpNum2 = nodeIndexMap[tmpNum2]; 92 if((tmpNum1%2==0 && tmpNum2-tmpNum1==1)||tmpNum1%2==1 && tmpNum1-tmpNum2==1) 93 { 94 printf("Yes\n"); 95 } 96 else 97 printf("No\n"); 98 } 99 else if(tmpStr[tmpStr.size()-1] == ‘l‘) 100 { 101 int tmpNum1 = getNumber(tmpStr.substr(0,4)); 102 int tmpNum2 = getNumber(tmpStr.substr(4,10)); 103 tmpNum1 = nodeIndexMap[tmpNum1]; 104 tmpNum2 = nodeIndexMap[tmpNum2]; 105 int cnt = 0; 106 while(tmpNum1 > 0) 107 { 108 tmpNum1 /= 2; 109 cnt++; 110 } 111 while(tmpNum2 > 0) 112 { 113 tmpNum2 /= 2; 114 cnt --; 115 } 116 if(cnt == 0) 117 printf("Yes\n"); 118 else 119 printf("No\n"); 120 } 121 else 122 { 123 int tmpNum1 = getNumber(tmpStr.substr(0,5)); 124 int tmpNum2 = getNumber(tmpStr.substr(tmpStr.size()-10,10)); 125 tmpNum1 = nodeIndexMap[tmpNum1]; 126 tmpNum2 = nodeIndexMap[tmpNum2]; 127 if(tmpStr.find("left")!=tmpStr.npos) 128 { 129 tmpNum1 == tmpNum2*2 ? printf("Yes\n") :printf("No\n"); 130 } 131 else if(tmpStr.find("right")!=tmpStr.npos) 132 { 133 tmpNum1 == tmpNum2*2+1 ? printf("Yes\n") :printf("No\n"); 134 } 135 else 136 { 137 tmpNum1 == tmpNum2/2 ? printf("Yes\n") :printf("No\n"); 138 } 139 } 140 } 141 return 0; 142 }
标签:call mes push amp ide false als ios node
原文地址:https://www.cnblogs.com/codewars/p/11366800.html