A Shortest Distance
It corresponds to a meaning of the title ring, find the minimum value obtained from the distance between two points in a different direction.
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 6 using namespace std; 7 vector<int> disVec; 8 int main() 9 { 10 int N, tmpDis; 11 cin >> N; 12 disVec.resize(N+1,0); 13 for(int i = 2; i <= N; ++ i) 14 { 15 cin >> tmpDis; 16 disVec[i] = disVec[i-1] + tmpDis; 17 } 18 cin >> disVec[0]; 19 int M, tmpSt, tmpEnd; 20 cin >> M; 21 for(int i = 0; i < M; ++ i) 22 { 23 cin >> tmpSt >> tmpEnd; 24 if(tmpSt > tmpEnd) 25 swap(tmpSt, tmpEnd); 26 tmpDis = min (disVec [tmpEnd] -disVec [tmpSt] disVec [tmpSt] + disVec [ 0 ] -disVec [tmpEnd] + disVec [N]); 27 cout << tmpDis << endl; 28 } 29 }
B Student List for Course
Courses list, this topic a bit mean, I was by the cin, cout into printf and scanf it not time out .
Tried set timeout, there is no thought of a better way.
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <set> 5 #include <string> 6 #include <vector> 7 8 using namespace std; 9 vector<int> disVec; 10 vector<string> classInfo[2510]; 11 int main() 12 { 13 int N, K, tmpNum, tmpClass; 14 string tmpStr; 15 cin >> N >> K; 16 for(int i = 1; i <= N; ++ i) 17 { 18 cin >> tmpStr;scanf("%d", &tmpNum); 19 while(tmpNum--) 20 { 21 scanf("%d", &tmpClass); 22 classInfo[tmpClass].push_back(tmpStr); 23 } 24 } 25 for(int i = 1; i <= K; ++ i) 26 { 27 vector<string> tmpVec = classInfo[i]; 28 sort(tmpVec.begin(), tmpVec.end()); 29 printf("%d %d\n", i, tmpVec.size()); 30 for(auto it = tmpVec.begin(); it != tmpVec.end(); ++ it) 31 { 32 printf("%s\n", (*it).c_str()); 33 } 34 } 35 return 0; 36 }
C Find Coins
Since two coins can be sorted, with the double pointer method , looking from both ends to the middle, as before find two coins, coin left plus right once more than the desired coin denomination, the coin is certainly the right it is impossible to meet the conditions.
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <set> 5 #include <string> 6 #include <vector> 7 8 using namespace std; 9 vector<int> coinVal; 10 int main() 11 { 12 int N, M, tmpVal; 13 cin >> N >> M; 14 coinVal.resize(N); 15 for(int i = 0; i < N; ++ i) 16 cin >> coinVal[i]; 17 int tmpLast = N-1; 18 sort(coinVal.begin(), coinVal.end()); 19 for(int i = 0; i < tmpLast; ++ i) 20 { 21 while(coinVal[i] + coinVal[tmpLast] > M) 22 tmpLast --; 23 if(tmpLast != i && coinVal[i] + coinVal[tmpLast] == M) 24 { 25 cout << coinVal[i] << " " << coinVal[tmpLast]; 26 return 0; 27 } 28 } 29 cout << "No Solution"; 30 return 0; 31 }
D Counting Ones
The subject needs to use mathematical knowledge to find some regularity, you will find the number 1 is produced for each bit of regular , see the code while loop.
(Wherein numarray [i] is the i-th bit value, multBase 0-9,0-99,0-999 ... is contained in the number of 1, digitBase is 0,10,100,1000 ...)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <string> 6 #include <vector> 7 8 using namespace std; 9 int charToNum(char c) 10 { 11 return c-'0'; 12 } 13 int n; 14 char numStr[15]; 15 int numArray[15]; 16 int len; 17 int numDeal(int tmpN) 18 { 19 int tmpNum = 0; 20 int digitBase = 1; 21 int multBase = 0; 22 while(tmpN >= 0) 23 { 24 if(numArray[tmpN] == 1) 25 { 26 tmpNum += numArray[tmpN] * multBase + n%digitBase+1; 27 } 28 else if(numArray[tmpN] > 1) 29 { 30 tmpNum += numArray[tmpN] * multBase + digitBase; 31 } 32 multBase = 10 * multBase + digitBase; 33 digitBase *= 10; 34 tmpN--; 35 } 36 return tmpNum; 37 } 38 int main() 39 { 40 41 cin >> n; 42 sprintf(numStr, "%d", n); 43 len = strlen(numStr); 44 for(int i = 0; i < len; ++ i) 45 numArray[i] = charToNum(numStr[i]); 46 int num = numDeal(len-1); 47 cout << num; 48 return 0; 49 }