首页 > 试题广场 >

小红的字符生成

[编程题]小红的字符生成
  • 热度指数:1760 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小红每次可以把一个字符变成两个字母表中比它小一位的字符。例如,可以把'b'变成两个'a',可以把'z'变成两个'y'。
小红希望最终可以生成 x 个'a',你能帮小红求出初始的字符串吗?请你输出长度最短的合法字符串,有多解时输出任意即可。

输入描述:
一个正整数x,代表最终的'a'的数量。



输出描述:
一个字符串,代表小红操作前的字符串。如果有多个合法解,输出任意一个合法字符串即可。但需要保证输出的是最短字符串。
示例1

输入

5

输出

ca

说明

"ca"->"bba"->"aaaaa"
输出ac也是可以的
# 获取用户输入的整数并转换为二进制字符串,去掉前缀'0b'
x = list(bin(int(input()))[2:])

# 初始化第一个字母的ASCII码
first = ord("a")

# 反转二进制列表并从低位到高位遍历
for i in x[::-1]:
    if i == "1": 
        print(chr(first), end="")
    first += 1  # 无论是否输出,都递增以准备下一个可能的字母

发表于 2025-01-21 13:50:18 回复(0)
#include <iostream>
using namespace std;
/*
*   注意到这个其实就是把x分解成2的幂级数表示
*/
int main() {
    int x;
    cin>>x;
    char ch = 'a';
    while(x){
        if(x&1)
            cout<<ch;
        ch++;
        x>>=1;
    }
}
// 64 位输出请用 printf("%lld")

发表于 2023-04-26 15:49:17 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
//        System.out.println((int) ('z'-'a'));
        StringBuilder res = new StringBuilder();
        int log = (int) (Math.log(n) / Math.log(2));
        int yu = n % 2;
        while (log != 0) {
            res.append((char) ('a' + log));
            n -= (int) Math.pow(2, log);
            if (n == 0)break;
            log = (int) (Math.log(n) / Math.log(2));
        }
        if (yu != 0) {
            res.append('a');
        }
        System.out.println(res);
    }
}

发表于 2023-03-16 16:20:02 回复(0)
import sys

n=eval(input())
#print (n)
n1 = bin(n)[2:]
#print (str(n1))
n1=n1[::-1]
r=''
cl='abcdefghijklmnopqrstuvwxyz'
for i in range(len(n1)):
    #print (n1[i],cl[i])
    if n1[i]=='1':
        r=r+cl[i]
print (r[::-1])

发表于 2025-01-17 21:11:30 回复(0)
const rl = require('readline').createInterface({
    input:process.stdin
});

rl.question('',(len)=>{
    len = parseInt(len);
    const count = [
        ['a',1],
        ['b',2],
        ['c',4],
        ['d',8],
        ['e',16],
        ['f',32],
        ['g',64],
        ['h',128],
        ['i',256],
        ['j',512],
    ];
    const ans = [];
    while(len !== 0){
        for(let i=count.length-1;i>=0;i--){
            if(len>=count[i][1]){
                len -= count[i][1];
                ans.push(count[i][0]);
                break;
            }
        }
    }

    console.log(ans.join(''));
    rl.close();
});

发表于 2025-01-07 14:52:29 回复(0)
根据题意易得:b=2个a,c=4个a,d=8个a,依次类推。
那么实际上题目可以转化为将x分解为若干个2的次方的和。即x = a * 2^0 + b * 2^1 + c * 2^2 +...
领会了这一点,那么代码就十分简单了。

#include <iostream>
using namespace std;

int main() {
    int x;
    cin >> x;
    string str = "";
    char c = 'a';
    while (x) {
        if (x & 1) {
            str += c;
        }
        x >>= 1;
        ++c;
    }
    cout << str;
}

发表于 2024-12-16 15:00:30 回复(0)
实质上就是10进制向二进制的转化,转化后取对应位置的字母拼接上就可以了
import sys

for line in sys.stdin:
    a = int(line.split()[0])
    a_2 = format(a,'b')[::-1]
    res = ''
    for index,c in enumerate(a_2):
        if c == '1':
            res = res + chr(ord('a')+index)
    print(res[::-1])



发表于 2024-11-22 15:49:51 回复(0)
实际上是将正整数n分解成2的次方的合集。
void decomposeIntoPowersOfTwo(unsigned int n, std::vector<int>& powers) {
    for (int i = 0; n > 0; ++i) {
        if (n & 1) { // 如果当前位是1
            powers.push_back(i); // 将对应的指数添加到集合中
        }
        n >>= 1; // 右移一位
    }
}

发表于 2024-11-21 22:57:44 回复(0)
#include <iostream>
#include<cmath>
using namespace std;

int main() {
   int x;
   cin>>x;
   while(x)
   {
    int t=log2(x);
    cout<<(char)('a'+t);
    x-=pow(2,t);
   }
}

发表于 2024-11-20 23:21:34 回复(0)
n = int(input())
i = 0
res = ''
while n:
    if n&1:
        res = chr(ord('a') + i) + res
    i += 1
    n >>= 1

print(res)

发表于 2024-11-20 15:12:14 回复(0)

位运算

x = int(input())
ans = ""
for i in range(26):
    if x >> i & 1:
        ans += chr(97 + i)
print(ans)
发表于 2023-09-01 18:45:03 回复(0)
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int num;
    cin >> num;
    string str = "";
    char c;
   
    for(int i = 9; i>0; i--)
    {
        if(num>=pow(2,i))
        {
            c = 'a'+i;
            str += c;
            num-=pow(2,i);
        }
    }

    for(int i = 0; i<num; i++)
    {
        str += 'a';
    }

    cout << str << endl;

    return 0;

}
// 64 位输出请用 printf("%lld")
编辑于 2023-07-15 15:25:45 回复(0)
#include <iostream>
using namespace std;
#include<math.h>

int main() {
    int x;
    cin >> x;
    string ans;
    for (int i = 25; i>=0;i--){
        int t = x>>i;
        if (t){
            ans.push_back('a'+i);
            x -= pow(2,i);
        }
    }
    cout<<ans;
}
// 64 位输出请用 printf("%lld")

发表于 2023-04-18 21:21:47 回复(0)
n = int(input())
dic = {1: 'a', 2: 'b', 4: 'c', 8: 'd', 16: 'e', 32: 'f', 64: 'g', 128: 'h', 256: 'i', 512: 'j'}
res = ''
len_x = len(dic)
for i in range(len_x - 1, -1, -1):
    if 2**i == n:
        res += dic[2 ** i]
        break
    if 2**i < n:
        res += dic[2 ** i]
        n = n - 2**i
print(res)

发表于 2023-04-07 10:13:24 回复(0)
n = int(input())
base = 0
res = []
while n!=0:
    if n % 2 == 1:
        res.append(chr(97+base))
    n //= 2
    base += 1
print(''.join(res))
// 相当于把数字跟转成二进制,0~26位上的1对应字母
发表于 2023-03-14 22:21:19 回复(0)
# 9a --> 最短 --->
# b --> aa , c --> aaaa, d --> aaaaaaaa
# 最终的数目就是在 1 2 4 8 16 组合
# 比如 5 --> 4 + 1 ca
init = 'a'
num = int(input())
pre = 1
res = [1]
out = ''
while(pre < 1000):
    pre = pre * 2
    res.append(pre)
for i in range(len(res)-1,-1,-1):
    if res[i] == num:
        out += chr(ord('a')+i)
        break
    if res[i] < num:
        out += chr(ord('a')+i)
        num = num - res[i]
print(out)

发表于 2023-03-09 17:12:07 回复(0)