当前位置:网站首页>2019 Tencent summer intern formal written examination

2019 Tencent summer intern formal written examination

2022-07-06 11:36:00 Geek Yunxi

Looking for a regular , If 1+2 Can be said 3 All numbers within ,1+2+4 Express 7 All numbers within ,1+2+4+8 Express 15 All numbers within ,,, law :a[n]<=sum[1~n-1]+1 when , Can be said sum[1~n] All numbers in

AC Code

 Copy code

The second question follows 01 Position is irrelevant , Directly count the number

​
#include <bits/stdc++.h>
using namespace std;
int a[105];
typedef long long LL;
int n, m;
LL sum = 0;
int who(int i)
{
    for(int j=i; j<n; j++)
    {
        if(a[j]>sum+1)
            return j-1;
        else if(a[j]==sum+1)
            return j;
    }
    return n-1;
}
int main()
{
    cin>>m>>n;
    for(int i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a+n);
    int ans= 0;
    for(int i=0; i<n; i++)
    {
        int j = who(i);
        i = j;
        sum += a[j];
        ans++;
        if(sum >= m) break;
    }
    while(sum < m)
    {
        sum += a[n-1];
        ans++;
    }
    printf("%d\n", ans);
    return 0;
}



​

 Copy code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, a = 0, b = 0;
    cin>>n;
    char s[200004];
    scanf("%s", s);
    int l = strlen(s);
    for(int i=0; i<l; i++)
    {
        if(s[i]-'0' == 0) a++;
        else b++;
    }
    cout<<abs(a-b)<<endl;
    return 0;
}

 Copy code

原网站

版权声明
本文为[Geek Yunxi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060912542594.html