#include <iostream>
using namespace std;
/*a[]保存二进制数的每一个位,每调用一次,二进制数加1*/
void increment(int a[], int n)
{
int i = 0;
while(i < n && a[i] == 1)
{
a[i] = 0;
i++;
}
if(i < n)
{
a[i] = 1;
}
}
/*测试程序*/
int main(int argc, char const *argv[])
{
int a[5] = {0, 0, 0, 0, 0};
for(int i = 0; i <= 15; i++)
{
increment(a, 5);
}
for(int i = 4; i >= 0; i--)
{
cout << a[i] << " ";
}
return 0;
}
第17章 二进制计数器递增代码
最新推荐文章于 2021-06-26 10:45:12 发布