2018研发岗美团笔试题目

1、第一题
题目描述:
在十进制表示中,任意一个正整数都可以用字符‘0’-‘9’表示出来。但是当‘0’-‘9’这些字符每种字符的数量有限时,可能有些正整数就无法表示出来了。比如你有两个‘1’ ,一个‘2’ ,那么你能表示出 11,12,121 等等,但是无法表示出 10,122,200 等数。

现在你手上拥有一些字符,它们都是‘0’-‘9’的字符。你可以选出其中一些字符然后将它们组合成一个数字,那么你所无法组成的最小的正整数是多少?

输入
第一行包含一个由字符’0’-‘9’组成的字符串,表示你可以使用的字符。

· 1 ≤字符串长度≤ 1000

输出
输出你所无法组成的最小正整数。

样例输入
55

样例输出
1

Hint
Input Sample 2
123456789
Output Sample 2
10
下面的代码不能被AC,只能过70%

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int find_num(vector<int> sort_temp_f, vector<int> num_count_temp)
{
    int i_save = 1;
    int i_s = 1;
    int i_s_y = 1;
    //vector<int> num_count_temp_temp(num_count_temp);

    while (1)
    {
        vector<int> num_count_temp_temp(num_count_temp);
        while (i_s_y)
        {

            i_s_y = i_s % 10; //不断取余
            i_s = i_s / 10;   //不断取整
            cout << i_s << "        " << i_s_y << endl;
            if (i_s == 0 && i_s_y == 0)            //如果两个都是零,则计算下一个数,这个逻辑忘记了,,
            {
                break;
            }
            if (find(sort_temp_f.begin(), sort_temp_f.end(), i_s_y) != sort_temp_f.end() && num_count_temp_temp[i_s_y] > 0)    //如果找到了这个数,并且个数大于零(这个数字还可以拿来用)
            {
                num_count_temp_temp[i_s_y] -= 1;
            }
            else
            {
                cout << "不能合成的数是  " << i_save << endl;
                return 0;
            }

        }
        i_save++;
        i_s = i_save;
        i_s_y = 1;
    }

}

int main()
{
    string s_temp;
    vector<int> v_temp;
    vector<int>  sort_temp;
    int num_count[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    int temp_count = 0;
    int i_temp = 0;
    cin >> s_temp;
    int n;
    for (int i = 0; i<s_temp.size(); ++i)
    {
        n = s_temp[i] - '0';
        v_temp.push_back(n);
    }
    sort(v_temp.begin(), v_temp.end());


        for (int i = 0; i<v_temp.size(); ++i)
        {
            for (int j = 0; j<10; ++j)           //找出每个数字的个数
            {
                if (v_temp[i] == j)
                {
                    num_count[j] += 1;
                }
            }

        }


        for (int i = 0; i < 10; ++i)
            cout << num_count[i] << endl;


    vector<int> num_count_temp;
    for (int i = 0; i < 10; ++i)                                                 //把每个数字出现的次数压入一个新的容器中
    {
        num_count_temp.push_back(num_count[i]);
    }

    int it = unique(v_temp.begin(), v_temp.end()) - v_temp.begin();            //删除重复的个数

    for (int i = 0; i < it; ++i)                                              //将没有重复的数字压入一个新的容器中
        sort_temp.push_back(v_temp[i]);

    for (int i = 0; i < num_count_temp.size(); ++i)                        
        cout << num_count_temp[i] << endl;

    cout << endl;
    find_num(sort_temp, num_count_temp);

    system("pause");
    return 0;
}

2、第二题
给出两个相同长度的由字符 a 和 b 构成的字符串,定义它们的距离为对应位置不同的字符的数量。如串”aab”与串”aba”的距离为 2;串”ba”与串”aa”的距离为 1;串”baa”和串”baa”的距离为 0。下面给出两个字符串 S 与 T,其中 S 的长度不小于 T 的长度。我们用|S|代表 S 的长度,|T|代表 T 的长度,那么在 S 中一共有|S|-|T|+1 个与 T 长度相同的子串,现在你需要计算 T 串与这些|S|-|T|+1 个子串的距离的和。

输入
第一行包含一个字符串 S。

第二行包含一个字符串 T。

S 和 T 均由字符 a 和 b 组成,1 ≤ |T| ≤ |S| ≤105 。

输出
输出对应的答案。

样例输入
aab

aba

样例输出
2

Hint
Input Sample 2
aaabb
bab
Output Sample 2
5
在样例 2 中,”aaa”与”bab”的距离为 2,”aab”与”bab”的距离为 1,”abb”与”bab”的距离为 2,
所以最后答案为 5。

#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    string a;
    string b;

    int res = 0;

    cin >> a >> b;

    int a_size = a.size();
    int b_size = b.size();

    int s_count = a_size - b_size;
    int count = 0;

    for (int i = 0; i <= s_count; ++i)
    {
        for (int j = 0; j < b_size; ++j)   //对两个字符串数组进行遍历操作,得到字符串的距离之和
        {
            if (a[i + j] != b[j])
                count++;
        }
    }
    cout << count << endl;
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u014077947/article/details/79715046