二进制数是只有两位 0 和 1 的数字。
格雷码是一种特殊类型的二进制数,其属性是代码的两个连续数字 em> 的差异不能超过一位。格雷码的这一特性使其在 K-map、纠错、通信等方面更加有用。
这使得二进制到格雷码的转换成为必要。那么,让我们看一下将二进制转换为格雷码的算法使用递归。
示例
让我们以格雷码代码为例
Input : 1001Output : 1101
登录后复制
算法
Step 1 : Do with input n : Step 1.1 : if n = 0, gray = 0 ; Step 1.2 : if the last two bits are opposite, gray = 1 + 10*(go to step 1 passing n/10). Step 1.3 : if the last two bits are same, gray = 10*(go to step 1 passing n/10).Step 2 : Print gray.Step 3 : EXIT.
登录后复制
示例
#include using namespace std;int binaryGrayConversion(int n) { if (!n) return 0; int a = n % 10; int b = (n / 10) % 10; if ((a && !b) || (!a && b)) return (1 + 10 * binaryGrayConversion(n / 10)); return (10 * binaryGrayConversion(n / 10));}int main() { int binary_number = 100110001; cout输出
The binary number is 100110001The gray code conversion is 110101001登录后复制
以上就是将以下内容翻译为中文:使用递归在C程序中将二进制转换为格雷码的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582091.html