题解 | #小红的字符生成#

小红的字符生成

https://www.nowcoder.com/practice/f8659377ca104b1aad45dd2fb564c940

得到一个 a 需要 1 个 a

得到一个 b 需要 2 个 a

得到一个 c 需要 4 个 a

得到一个 d 需要 8 个 a

不难发现对于每个字母就好比二进制上的一位数

例如 n=11 时,二进制为1011,很明显用每个二进制位上对应的字母构造出 n 是字符最少的

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
int __t = 1, n;
void solve() {
    cin >> n;
    for (int i = 25; i >= 0; i--) {
        if (n & (1 << i)) {
            cout << char('a' + i);
        }
    }
    cout << "\n";
    return;
}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    // cin >> __t;
    while (__t--)
        solve();
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 14:10
点赞 评论 收藏
分享
06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务