您的位置:首页 > 编程语言 > Go语言

Algorithm Gossip: 格雷码(Gray Code)

2015-02-04 17:04 260 查看
/****************************************
*  File Name  : arithmetic.c
*  Creat Data : 2015.2.4
*  Author     : ZY
*****************************************/

/*Algorithm Gossip: 格雷码(Gray Code)*/

#include <stdio.h>

#define MAXBIT 20
#define TRUE 1
#define CHANGE_BIT(x) x=((x) == '0'?'1':'0')
#define NEXT(x) x=(1-(x))

int main(void)
{
char digit[MAXBIT];
int i,bits,odd;

printf("输入位元数:");
scanf("%d",&bits);

for(i = 0;i < bits;i++)
{
digit[i] = '0';
printf("0");
}

printf("\n");

odd = TRUE;

while(1)
{
if(odd)
{
CHANGE_BIT(digit[0]);
}
else
{
for(i = 0;i < bits&&digit[i] == '0';i++)
{
;
}
if(i == bits-1)
{
break;
}
CHANGE_BIT(digit[i+1]);
}
for(i = bits-1;i >= 0;i--)
{
printf("%c",digit[i]);
}
printf("\n");
NEXT(odd);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: