腾讯-客户端-第三次笔试(1,1,1,1,0.4)
希望能过笔试吧,第五道题FFT最远距离求代码。
其他四道代码奉上。
其他四道代码奉上。
第一道:企鹅王国的电话号码
#include <iostream> #include <string> #include <algorithm> using namespace std; bool isTrue(vector<char> s, int n){ for (int i = 0; i < n; i++){ if (s[i] == '8' && n - i >= 11) return true; else{ continue; } } return false; } int main(){ int t; cin >> t; for (int i = 0; i < t; i++){ int n; cin >> n; vector<char> s(n); for (int i = 0; i < n ;i++) cin >> s[i]; if (n < 11){ cout << "NO" << endl; } else{ if (isTrue(s, n)) cout << "YES" << endl; else cout << "NO" << endl; } } //system("Pause"); return 0; }
第二道:使用红绿蓝,粉刷房屋
#include <iostream> #include <vector> #include <algorithm> using namespace std; int minValue(vector<vector<int>>& nums, int n){ if (n <= 0 || nums.empty() || nums[0].empty()) return 0; vector<vector<int>> dp(n, vector<int>(3, 0)); dp[0][0] = nums[0][0]; dp[0][1] = nums[0][1]; dp[0][2] = nums[0][2]; for (int i = 1; i < dp.size(); ++i){ dp[i][0] = min(dp[i - 1][1] + nums[i][0], dp[i - 1][2] + nums[i][0]); dp[i][1] = min(dp[i - 1][0] + nums[i][1], dp[i - 1][2] + nums[i][1]); dp[i][2] = min(dp[i - 1][0] + nums[i][2], dp[i - 1][1] + nums[i][2]); } return min(min(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]); } int main(){ int n; cin >> n; vector <vector<int>> nums(n, vector<int>(3, 0)); for (int i = 0; i < n; i++){ cin >> nums[i][0]; cin >> nums[i][1]; cin >> nums[i][2]; } cout << minValue(nums, n) << endl; //system("Pause"); return 0; }
第三道:排成圆圈,身高的最大差异最小
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; sort(nums.begin(), nums.end()); vector<int>res; if (n % 2 == 0){ for (int i = 0; i < n; i += 2){ res.push_back(nums[i]); } for (int i = n - 1; i > 0; i -= 2){ res.push_back(nums[i]); } for (int i = 0; i < n; i++){ cout << res[i]; i == n - 1 ? cout << endl : cout << " "; } } else{ for (int i = 0; i < n; i += 2){ res.push_back(nums[i]); } for (int i = n - 2; i > 0; i -= 2){ res.push_back(nums[i]); } for (int i = 0; i < n; i++){ cout << res[i]; i == n - 1 ? cout << endl : cout << " "; } } //system("Pause"); return 0; }
第四道:木板刷漆
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; vector<int> temp(n); temp[0] = 0; int count = 0; for (int i = 1; i < n; i++){ temp[i] = nums[i] - nums[i - 1]; if (temp[i] != 0) count++; } count + 1 <= n ? cout << count + 1 << endl : cout << n << endl; //system("Pause"); return 0; }