本文主要是介绍顺丰20180917,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <iostream>
#include <vector>
#include <numeric>
#include <limits>using namespace std;/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
******************************开始写代码******************************/
int candy(vector < double > numbers) {int sum = 0;vector<int> v1;if (numbers.size() != 0){int num = 1;v1.push_back(num);if (numbers.size() == 1)return 1;for (size_t i = 1; i != numbers.size(); ++i){if (numbers[i] > numbers[i - 1]){num++;}else{num = 1;}v1.push_back(num);}sum += num;for (size_t i = numbers.size() - 2; i != -1; --i){if (numbers[i] > numbers[i+1]){num=v1[i+1]+1;if (num < v1[i])num = v1[i];v1[i] = num;}else{num = v1[i];}sum += num;}} return sum;}
/******************************结束写代码******************************/int main() {int res;int _numbers_size = 0;cin >> _numbers_size;cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); vector<double> _numbers;double _numbers_item;for(int _numbers_i=0; _numbers_i<_numbers_size; _numbers_i++) {cin >> _numbers_item;cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');_numbers.push_back(_numbers_item);} res = candy(_numbers);cout << res << endl;return 0;}
#include <iostream>
#include <vector>
#include <numeric>
#include <limits>using namespace std;int minimum(int a, int b, int c){int m = a;if(b<m)m=b;if(c<m)m=c;return c;
}/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
******************************开始写代码******************************/
int change(string str1, string str2) {int n = str1.length();int m = str2.length();if (m == 0) return n;if (n == 0) return m;typedef vector< vector<int> > Tmatrix;Tmatrix matrix(n + 1);for (int i = 0; i <= n; i++) matrix[i].resize(m + 1);for (int i = 1; i <= n; i++) matrix[i][0] = i;for (int i = 1; i <= m; i++) matrix[0][i] = i;for (int i = 1; i <= n; i++){const char si = str1[i - 1];for (int j = 1; j <= m; j++){const char dj = str2[j - 1];int cost;if (si == dj){cost = 0;}else{cost = 1;}const int above = matrix[i - 1][j] + 1;const int left = matrix[i][j - 1] + 1;const int diag = matrix[i - 1][j - 1] + cost;matrix[i][j] = minimum(above, left, diag);}}return matrix[n][m];
}/******************************结束写代码******************************/int main() {int res;string _str1;getline(cin, _str1);string _str2;getline(cin, _str2);res = change(_str1, _str2);cout << res << endl;return 0;}
第二题:有 N 个小孩站成一列,每个小孩手中有一个随机数,按照以下规则分配糖果:
1、如果某个小孩手中的随机数比旁边的大,他分到的糖果数目就要比旁边的多;
2、每个小孩至少得到一颗糖果;
问:需最少准备多少糖果?
第一题:实现两个字符串之间的Levenshtein Distance(编辑距离)
这篇关于顺丰20180917的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!