1、写一个函数返回参数二进制中 1 的个数。比如: 15:0000 1111 (4 个 1)。
程序原型:
int count_one_bits(unsigned int value)
{
// 返回 1的位数
}
int count_one_bits(unsigned int value)
{
int i = 0;
int count = 0;
while (value)
{
count++;
value = value&(value - 1);
}
return count;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int value = 15;
int count = 0;
int ret = count_one_bits(value);
printf("%d\n", ret);
system("pause");
return 0;
}
2、获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num = 0, i = 0;
scanf("%d", &num);
printf("奇数序列为:");
for (i = 31; i >= 0; i -= 2)
{
printf("%d ", (num >> i) & 1);
}
printf("\n");
printf("偶数序列为:");
for (i = 30; i >= 0; i -= 2)
{
printf("%d ", (num >> i) & 1); //
}
printf("\n");
system("pause");
return 0;
}
3、输出一个整数的每一位。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
void print(int n)
{
if (n>9)
{
print(n / 10);
}
printf("%d ", n % 10);
}
int main()
{
print(1234);
system("pause");
return 0;
}
4、编程实现:两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?
输入例子:1999 2299
输出例子:7
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int count_diffrent_bits(int x, int y)
{
int i = 0;
int tmp = x ^ y;
int count = 0;
while (tmp)
{
tmp = tmp & (tmp - 1);
count++;
}
return count;
}
int main()
{
int m = 0;
int n = 0;
printf("请输入两个整数m,n:\n");
scanf("%d\n%d", &m, &n);
int ret = count_diffrent_bits(m, n);
printf("两个整数%d,%d有%d位不同\n", m, n, ret);
system("pause");
return 0;
}
5、5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果:
A选手说:B第二,我第三;
B选手说:我第二,E第四;
C选手说:我第一,D第二;
D选手说:C最后,我第三;
E选手说:我第四,A第一;
比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <windows.h>
int main()
{
int a, b, c, d, e;
int num = 0;
for (a = 1; a <= 5; a++)
{
for (b = 1; b <= 5; b++)
{
for (c = 1; c <= 5; c++)
{
for (d = 1; d <= 5; d++)
{
for (e = 1; e <= 5; e++)
{
if (((b == 1) + (a == 3) == 1)
&& ((b == 2) + (e == 4) == 1)
&& ((c == 1) + (d == 2) == 1)
&& ((c == 5) + (d == 3) == 1)
&& ((e == 4) + (a == 1) == 1))
{
num = 0;
num |= (1 << (a - 1));//a=3
num |= (1 << (b - 1));
num |= (1 << (c - 1));
num |= (1 << (d - 1));
num |= (1 << (e - 1));
while (num) //有可能有并列,但是绝不可能有第一名第三名,没有第二名
{
if (num % 2 == 0)
break;
num = num / 2;
}
if (num == 0)
{
printf("A第%d名 B第%d名 C第%d名 D第%d名 E=第%d名\n", a, b, c, d, e);
}
}
}
}
}
}
}
system("pause");
return 0;
}
6、日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个。嫌疑犯的一个。以下为4个嫌疑犯的供词。
A说:不是我。
B说:是C。
C说:是D。
D说:C在胡说
已知3个人说了真话,1个人说的是假话。
现在请根据这些信息,写一个程序来确定到底谁是凶手。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=0,b=0,c=0,d=0;
printf("0代表不是凶手;1代表是凶手\n");
for(a=0;a<2;a++) //有四个人,而每个人说的情况只用两种
{
for (b = 0; b<2; b++)
{
for (c = 0; c<2; c++)
{
for (d = 0; d<2; d++)
{
if (((a == 0) && (c == 1) && (d == 0) == 1) + ((a == 0) && (d == 1) && (d == 0) == 1) + ((a == 0) && (c == 1) && (d == 1) == 1) + ((a == 0) && (d == 1) && (d == 0) == 1))
{
if (a + b + c + d == 1)
printf("a=%d b=%d c=%d d=%d\n", a, b, c, d);
system("pause");
return 0;
}
}
}
}
}
}
7、在屏幕上打印杨辉三角。
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10][10] = { 0 };
for (int i = 1; i < 11; i++) {
for (int j = 1; j < i; j++) {
if (i == 1) {
printf("%4d\n", a[1][1] = 1);
}
else {
if ((j == 1) || (i == j)) {
printf("%4d", a[i][j] = 1);
}
else {
printf("%4d", a[i][j] = a[i - 1][j - 1] + a[i - 1][j]);
}
}
}
printf("\n");
}
system("pause");
return 0;
}