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

1.翻转数列

水题

 

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

2.纸牌游戏         

简单贪心

#include<bits/stdc++.h>
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<bits/stdc++.h>
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<bits/stdc++.h>
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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值