欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

百度2016实习生真题编程题

程序员文章站 2022-06-03 11:38:02
...

ACM模版

题目

百度2016实习生真题编程题

代码

#include <cstdio>
#include <iostream>
#include <stack>
#include <cstring>

using namespace std;

const int MAXN = 1e4 + 10;
const int MAXM = 1e3 + 10;

char strs[MAXN];
char str[MAXM];
stack<string> ss;

int main()
{
    while (gets(str) != NULL)
    {
        strcat(strs, str);
    }

    string _str(strs, strlen(strs));

    int len = (int)_str.size();

    for (int i = 0; i < len; )
    {
        if (_str[i] == '<' && _str[i + 1] != '!' && _str[i + 1] != '/')
        {
            int j = i + 1;
            for (; j < len; j++)
            {
                if (_str[j] == '>')
                {
                    break;
                }
            }

            string tmp = _str.substr(i + 1, j - 1 - i);
            ss.push(tmp);
            i = j + 1;
        }
        else if (_str[i] == '<' && _str[i + 1] == '/')
        {
            int j = i + 2;
            for (; j < len; j++)
            {
                if (_str[j] == '>')
                {
                    break;
                }
            }

            string tmp = _str.substr(i + 2, j - 2 - i);
            if (0 != strcmp(tmp.c_str(), ss.top().c_str()))
            {
                cout << "Invalid";
                return 0;
            }
            else
            {
                ss.pop();
                i = j + 1;
            }
        }
        else
        {
            i++;
        }
    }

    if (ss.empty())
    {
        cout << "Valid";
    }
    else
    {
        cout << "Invalid";
    }

    return 0;
}

题目

百度2016实习生真题编程题

代码

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <cctype>

using namespace std;

bool isrc(string &s)
{
    if (s.size() < 2)
    {
        return false;
    }
    if (!(s[0] == 'R' && isdigit(s[1])))
    {
        return 0;
    }
    int i = 1;
    while (i < s.size() && s[i] != 'C')
    {
        i++;
    }
    if (i == s.size())
    {
        return 0;
    }
    return 1;
}

string int2str(int num)
{
    string s = "";
    while (num)
    {
        int i = num % 26;
        if (i == 0)
        {
            char c = 'Z';
            s = c + s;
            num = num / 26 - 1;
        }
        else
        {
            char c = 'A' + num % 26 - 1;
            num /= 26;
            s = c + s;
        }
    }
    return s;
}

string rc2(string &rc)
{
    string ans = " ";
    int i = 1;
    while (rc[i] != 'C')
    {
        i++;
    }
    int col = atoi(rc.substr(i + 1).c_str());
    string s = int2str(col);
    s += rc.substr(1, i - 1);

    return s;
}

string fun(int num)
{
    string s = "";
    while (num)
    {
        char c = '0' + num % 10;
        s = c + s;
        num /= 10;
    }

    return s;
}

string i2rc(string &s)
{
    int i = 0;
    int col = 0;
    while (!isdigit(s[i]))
    {
        col = col * 26 + (s[i] - 'A' + 1);
        i++;
    }
    string r = "R";
    r += s.substr(i);
    r += "C";
    r += fun(col);

    return r;
}

int n;
string rc, s;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> rc;
        if (isrc(rc))
        {
            s = rc2(rc);
        }
        else
        {
            s = i2rc(rc);
        }

        cout << s << endl;
    }

    return 0;
}

题目

百度2016实习生真题编程题

代码

#include <iostream>

using namespace std;

typedef long long ll;

ll solve (ll n, ll m, ll k)
{
    ll l = 0, r = n * m + 1;
    while (r - l > 1)
    {
        ll mid = (l + r) / 2;
        ll cnt = 0;
        for (int i = 1; i <= m; i++)
        {
            cnt += min (mid / i, n);
        }
        if (cnt >= k)
        {
            r = mid;
        }
        else
        {
            l = mid;
        }
    }

    return r;
}

ll n, m, k;

int main ()
{
    while (cin >> n >> m >> k)
    {
        cout << solve (n, m, k) << endl;
    }

    return 0;
}
相关标签: 笔试题