武汉工程大学2020GPLT选拔赛(重现赛)1-6

L1-1 I LOVE WIT (10)

签到,直接输出就行了。

print('''I

  L
   O
    V
     E

       W
        I
         T''')

L1-2 单位换算

签到,直接算注意特判整数

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
typedef long long ll;

inline int read() {
    int s = 0, w = 1; char ch = getchar();
    while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}

const double eps = 1e-6;

int main() {
    int n = read();
    double ans = n * 12 * 25.4;
    if (ans - int(ans) > eps)
        printf("%.1f\n", n * 12 * 25.4);
    else
        printf("%d\n", int(ans));
    return 0;
}

L1-3 Pokémon

直接计算,用C的scanf格式化输入可能简单一点,注意0.99(我不会说我因为这个WA了

#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
typedef long long ll;

inline int read() {
    int s = 0, w = 1; char ch = getchar();
    while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}

int main() {
    double a[10];
    for (int i = 0; i <= 6; ++i)
        scanf("%lf%%", a + i);
    int n,m;
    scanf("%d %d",&n,&m);
    if (m)
        printf("%.2f%%\n", 0.01 * a[n]);
    else
        printf("%.2f%%\n", a[n]);
    return 0;
}

L1-4 颠倒阴阳

位运算基本操作,我写的可能复杂一点不过是按照题意翻译的,用了个bitset

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
typedef long long ll;

inline ll read() {
    ll s = 0, w = 1; char ch = getchar();
    while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}


int main() {
    ll n = read(); //注意int 是 2^31-1不是32……
    ll cnt = 0, st = -1;
    string s = "";
    while (n) {
        if (n & 1)    s += "1", st = cnt;
        else    s += "0";
        n >>= 1;
        ++cnt;
    }
    reverse(s.begin(), s.end());
    bitset<40> ans(s);
    for (int i = st; i >= 0; --i)
        if (ans[i])    ans[i] = 0;
        else    ans[i] = 1;
    s = ans.to_string();
    reverse(s.begin(), s.end());
    ll sum = 0;
    for (int i = 0; i < 32; ++i)
        if (s[i] == '1')    sum += 1ll << (31 - i);
    printf("%lld\n", sum);
    return 0;
}

再给份标答吧,标答比较简洁

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
typedef long long ll;

inline ll read() {
    ll s = 0, w = 1; char ch = getchar();
    while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}


int main() {
    unsigned int n;
    cin>>n;
    unsigned int ans=0;
    for (int i=0;i<32;i++) {
        ans<<=1;
        if (n && ((n&1)==0)) {
            ans++;
        }
        n>>=1;
    }
    cout<<ans<<endl;
    return 0;
}

E-L1-5 演唱会

直接计算,换成秒,对比就行,注意等号

#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
typedef long long ll;

inline int read() {
    int s = 0, w = 1; char ch = getchar();
    while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}

int main() {
    double a[10];
    for (int i = 0; i <= 6; ++i)
        scanf("%lf%%", a + i);
    int n,m;
    scanf("%d %d",&n,&m);
    if (m)
        printf("%.2f%%\n", 0.01 * a[n]);
    else
        printf("%.2f%%\n", a[n]);
    return 0;
}

L1-6 分鸽子

二分
对于每个人只能吃到的鸽子肉只能来自于一只鸽子但是一只鸽子可以给分割被很多人吃。那么我们假设mid可以满足当前分配,自然而然地就知道想要这个答案更大一点的,那么如果这个mid不够给人分配,那么答案一定比mid更小,这样就满足了二分的性质。这里还要特判0因为mid做了除数,我写的就用了个累加,因为我写的二分是[l,r]的写法,不能直接吧0写进去,也可以用(l,r]的写法,把l输出,如果一次l都没有更新,那初值0输出。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
typedef long long ll;

inline ll read() {
    ll s = 0, w = 1; char ch = getchar();
    while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}

const int N = 1e5 + 7;
int n, m;
ll a[N];

bool check(ll x) {
    ll cnt = 0;
    for (int i = 1; i <= n; ++i) {
        if (a[i] < x)    break;
        cnt += a[i] / x;
        if(cnt>=m)    return true;
    }
    return false;
}

int main() {
    n = read(), m = read();
    ll sum=0;
    for (int i = 1; i <= n; ++i)    a[i] = read(),sum+=a[i];
    if(sum<m){
        puts("0");
        return 0;
    }
    sort(a + 1, a + 1 + n, greater<ll>()); //从大到小排序
    ll l = 1, r = 1e9, mid, ans;
    while (l <= r) {
        mid = l + r >> 1;
        if (check(mid))    ans = mid, l = mid + 1;
        else    r = mid - 1;
    }
    printf("%lld\n", ans);
    return 0;
}
全部评论

相关推荐

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