题解 | #小红的字符生成#
小红的字符生成
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; }