【C++ CSES】Gray Code

该篇文章详细描述了如何使用C++编写一个递归函数来生成具有给定长度n的格雷码列表,适合于编程和算法学习者。
题目描述

A Gray code is a list of all 2^n bit strings of length n, where any two successive strings differ in exactly one bit (i.e., their Hamming distance is one).
Your task is to create a Gray code for a given length n.

输入

The only input line has an integer n(1 ≤ n ≤ 16).

输出

Print 2^n lines that describe the Gray code. You can print any valid solution.

样例输入 Copy
2
样例输出 Copy
00
01
11
10
#include <bits/stdc++.h>
using namespace std;
vector<string> f(int n) 
{
    if (n == 0) return vector<string>{"0"};
    else if (n == 1) return vector<string>({ "0", "1" });    
    else 
    {
        vector<string> newcode;
        vector<string> code = f(n - 1);
        for (auto it = code.begin(); it != code.end(); it++)
            newcode.emplace_back("0" + *it);
        for (auto it = code.rbegin(); it != code.rend(); it
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值