1.给定两个整形变量的值,将两个值的内容进行交换。
#include <stdio.h>
int main()
{
int a = 30;
int b = 20;
int *p1 = &a;
int *p2 = &b;
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
a = *p1;
b = *p2;
printf("a=%d b=%d", a, b);
system("pause");
return 0;
}
2.不允许创建临时变量,交换两个数的内容.
#include <stdio.h>
int main()
{
int a = 30;
int b = 20;
a = a^b;
b = a^b;
a = a^b;
printf("a=%d b=%d", a, b);
system("pause");
return 0;
}
3.求10 个整数中最大值。
#include <stdio.h>
int main()
{
int a[10];
int i, j;
int max = 0;
for (j = 0; j < 10; j++)
{
scanf_s("%d", &a[j]);
}
for (i = 0; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
printf("%d\n", max);
system("pause");
return 0;
}
4.写一个函数返回参数二进制中 1 的个数
#include<stdio.h>
int count_one_bits(unsigned int value)
{
int a = value; //把函数中value的值给a
int i = 0; //记录1的值
while (a)
{
if (a % 2 == 1)
{
i++;
}
a = a / 2;
} //while循环是将十进制转换成二进制的算法
return i;
}
int main()
{
printf("the number of 1 is: %d\n", count_one_bits(15)); //15指value的值 system("pause");
return 0;
}