京东3.18笔试

顺便贴一下我第二题和第三题代码

第二题:

#include<bits/stdc++.h>
# define DEBUG ios::sync_with_stdio(false);cin.tie(nullptr)
using namespace std;
typedef pair<int,int>PII;
typedef long long ll;
# define x first
# define y second
const int N = 200010; 
int n,a[N];
int f[N];

int main()
{
	DEBUG;
    cin >> n;
    ll ans = 0;
    for(int i=1;i<=100000;++i) f[i] = i-1;
    for(int i=3;i<=100000;++i){
        f[i] = min(f[i],f[i-1] + 1);
        int num = i;
        for(int j=2;j*j<=num;++j){
            if(num % j == 0) {
                f[i] = min(f[i],f[j] + f[num / j] + 1);
            }
        }
    }
    for(int i=1;i<=n;++i){
        cin >> a[i];
        ans += f[a[i]];
    }
    cout << ans << '\n';
	return 0;
} 

第三题:

#include<bits/stdc++.h>
# define DEBUG ios::sync_with_stdio(false);cin.tie(nullptr)
using namespace std;
typedef pair<int,int>PII;
typedef long long ll;
# define x first
# define y second
const int N = 200010;
char str[N];
int l[N];

int main()
{
	DEBUG;
    cin >> (str + 1);
    int n = strlen(str+1);
    stack<int> s;
    for(int i=1;i<=n;++i){
        if(str[i] == '(') s.push(i);
        else{
            if(s.empty()) continue;
            l[i] = s.top();
            s.pop();
        }
    }
    ll ans = 0;
    for(int i=1;i<=n;++i){
        if(str[i] == ')'){
            if(l[i] == 0) continue;
//             cout << i << " " << l[i] << "\n";
            ans += 1LL * l[i] * (n - i + 1) * 2;
        }
    }
    cout << ans << '\n';
	return 0;
} 

#投票#
全部评论
京东的限制语言不?比如我投递的岗位是C++,做题的时候,可以用python不?
点赞
送花
回复
分享
发布于 2023-03-21 10:39 湖北
这是机试吧?京东的是给多长上时间做题?
点赞
送花
回复
分享
发布于 2023-03-21 10:54 辽宁
蔚来
校招火热招聘中
官网直投
我AK了 ,但是在投递简历的时候意向bg选的 不限,听说会在大池子里泡死。现在官网流程还是在笔试中,这正常么。
点赞
送花
回复
分享
发布于 2023-03-22 01:12 四川

相关推荐

点赞 评论 收藏
转发
点赞 3 评论
分享
牛客网
牛客企业服务