腾讯2018春招技术类编程题汇总

1.翻转数列

水题

 

#include
using namespace std;
typedef long long LL;
int main()
{
    LL m, n;
    cin >> m >> n;
    cout << ((m * n) >> 1) << endl;
    return 0;
}

2.纸牌游戏         

简单贪心

#include
using namespace std;
typedef long long LL;
LL a[100005];
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
        scanf("%lld", &a[i]);
    sort(a, a + n);
    LL sum1 = 0, sum2 = 0;
    for(int i = 0 ; i < n; i++)
    {
        if(i % 2)
            sum1 += a[i];
        else
            sum2 += a[i];
    }
    cout << abs(sum1 - sum2) << endl;
    return 0;
}

贪吃的小Q

二分查找,条件有点难确定

#include
using namespace std;
typedef long long LL;
int n, m;
int Sum(int m)
{
    LL sum1 = 0;
    for(int i = 0; i < n; i++)
    {
        sum1 += m;
        m = (m + 1) / 2;
    }
    return sum1;
}
int Find(int n, int m)
{
    if(n == 1) 
        return m;
    int low = 1, high = m;
    while(low <= high)
    {
        int mid = (low + high) >> 1;
        if(Sum(mid) == m) return mid;
        else if(Sum(mid) > m)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return high;
}
int main()
{
    cin >> n >> m;
    cout << Find(n, m) << endl;
    return 0;
}

小Q的歌单 

01背包(考完研后蒟蒻已经忘了许多东西了....)

#include
using namespace std;
int dp[1005];
int main()
{
    memset(dp, 0, sizeof(dp));
    int k, a, b, x, y;
    cin >> k;
    cin >> a >> x >> b >> y;
    dp[0] = 1;
    for(int i = 1; i <= x; i++)
        for(int j = k; j >= a; j--)
            dp[j] = (dp[j] % 1000000007 + dp[j - a] % 1000000007) % 1000000007;
 
    for(int i = 1; i <= y; i++)
        for(int j = k; j >= b; j--)
                dp[j] = (dp[j] % 1000000007 + dp[j - b] % 1000000007) % 1000000007;
    cout << dp[k] << endl;
    return 0;
}

5.安排机器

此题并未想出正解。。。运行下面的贪心代码发现

1 2

999 999

2 1

1 100

这个过不去

恳请大佬们解答

6.画家小Q

简单搜索即可

n, m = map(int, input().split())
graph = []
for i in range(n):
    color = input()
    graph.append(color)
def colorY(i, j, graph):
    while i < n and j < m:
        if graph[i][j] == 'Y':
            t = list(graph[i])
            t[j] = 'X'
            graph[i] = ''.join(t)
            i += 1
            j += 1
        elif graph[i][j] == 'G':
            t = list(graph[i])
            t[j] = 'B'
            graph[i] = ''.join(t)
            i += 1
            j += 1
        else:
            break
def colorB(i, j, graph):
    while i < n and j >= 0:
        if graph[i][j] == 'B':
            t = list(graph[i])
            t[j] = 'X'
            graph[i] = ''.join(t)
            i += 1
            j -= 1
        elif graph[i][j] == 'G':
            t = list(graph[i])
            t[j] = 'Y'
            graph[i] = ''.join(t)
            i += 1
            j -= 1
        else:
            break
ans = 0
for i in range(n):
    for j in range(m):
       if graph[i][j] == 'Y':
           colorY(i, j, graph)
           ans += 1
       elif graph[i][j] == 'B':
           colorB(i, j, graph)
           ans += 1
       elif graph[i][j] == 'G':
           colorY(i, j, graph)
           ans += 1
           colorB(i, j, graph)
           ans += 1
print(ans)

 

你可能感兴趣的:(公司真题)