第一题 跳高跳远 AC1
//实际没看太看懂题目,按着输出的意思写了写
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <unordered_map> using namespace std; int mp1[27], mp2[27]; int main() { int n; cin >> n; int c1 = 0, c2 = 0; bool f1 = false; for(int i = 0; i < n; i++) { char temp; cin >> temp; if(temp == 'X') { f1 = true; continue; } if(!f1) { mp1[temp-'A']++; } else { mp2[temp-'A']++; } } bool f2 = false; for(int i = 0; i < n; i++) { char temp; cin >> temp; if(temp == 'X') { f2 = true; continue; } if(!f2) { if(mp1[temp - 'A'] != 0) c1++; } else { if(mp2[temp-'A'] != 0) c2++; } } cout << c1+1 << " " << (n-c2) << endl; return 0; }第二题 斐波那契+旋转输出(剑指offer29) AC1
//记得用long long 即可
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { int n; cin >> n; long long m = n*n; vector<long long> dp(m, -1); dp[0] = 1; dp[1] = 1; for(int i = 2; i < m; i++) { dp[i] = dp[i-1] + dp[i-2]; } //cout << dp[m-1] << endl; vector<vector<long long>> res(n, vector<long long>(n, 0)); int start = 0; int pre = m-1; //int endy = n-1 - start; //int endx = n-1- start; while(n > 2*start) { int endy = n-1 - start; int endx = n-1- start; for(int i = start; i <= endy; i++) { //cout << dp[pre] << " "; res[start][i] = dp[pre--]; } if(start < endx) { for(int i = start+1; i <= endx; i++) { //cout << dp[pre] << " "; res[i][endy] = dp[pre--]; } } if(start < endx && start < endy) { for(int i = endy-1; i >= start; i--) { //cout << dp[pre] << " "; res[endx][i] = dp[pre--]; } } if(start < endy && start < endx-1) { for(int i = endx-1; i > start; i--) { //cout << dp[pre] << " "; res[i][start] = dp[pre--]; } } start++; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(j != 0) cout << " "; cout << res[i][j]; } cout << endl; } return 0; }
全部评论
(1) 回帖