美团点评2018笔试编程题

来源:互联网 发布:情侣礼物知乎 编辑:程序博客网 时间:2024/05/12 13:40

不好意思,没有全部AC,两个题每个都只过了80%,但是到目前为止还是不知道那20%卡在哪里???

第一题:将一个整数拆成一些非0的数字,要求,长度尽可能长,数字的和等于这个整数,相邻两个数不能相等。

eg: 1 --->   1

       2--->   2

       3--->   12

       4--->   121

思路:数字尽可能小,长度就尽可能大了;还要满足相邻的数不同,那么可以选择间隔:

代码:

#include<iostream>#include<vector>#include<string>#include<deque>using namespace std;#include<map>int main(){int s;cin >> s;vector<int> vc;if (s == 1){cout << 1 << endl;return 0;}if (s == 2){cout << 2 << endl;return 0;}if (s > 2){int yu = s % 3;int sh = s / 3;while (sh){vc.push_back(1);vc.push_back(2);--sh;}if (yu != 0){vc.push_back(yu);}}for (int i = vc.size() - 1; i >= 0; --i){cout << vc[i];}cout << endl;return 0;}

第二题:

输入一个n表示数组长度,输入m表示放在数组前的数的个数,例如:

输入4 2

        3 2

输出 2 3 1  4


代码:


int main(){deque<int> vcn;vector<int> vcm;int n, m;int num;cin >> n >> m;for (int i = 0; i < n; ++i){vcn.push_back(i + 1);}for (int i = 0; i < m; ++i){cin >> num;vcm.push_back(num);}for (int i = 0; i < m; ++i){for (int j = 0; j < n; ++j){deque<int>::iterator it = vcn.begin();if (vcn[j] == vcm[i]){int mm = vcn[j];it += j;vcn.erase(it);vcn.push_front(mm);}}}for (int i = 0; i < n; ++i){cout << vcn[i] << endl;}return 0;}




原创粉丝点击