1.将数组A中的内容和数组B中的内容进行交换。(数组一样大)
void swap(int *a, int *b)
{
int ten = 0;
ten =* a;
*a = *b;
*b = ten;
}
int main()
{
int tem = 0;
int arr1[4] = { 1, 2, 3, 4 };
int arr2[4] = { 5, 6, 7, 8 };
int i = 0;
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]);i++)
{
swap(&arr1[i], &arr2[i]);
}
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
printf("%d ", arr1[i]);
printf("\n");
for (i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++)
printf("%d ", arr2[i]);
system("pause");
return 0;
}
2.获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include <math.h>
void fun(int value)
{
int count = 0;
int num = value;
while (num)
{
count++;
num = num >> 1;
}
while (count--)
{
num = value;
num = value >> count;
if (count % 2 != 0)
{
printf("偶数位:");
printf("%d ", num & 1);
}
else
{
printf("奇数位:");
printf("%d ", num & 1);
}
printf("\n");
}
}
void ba(int value)
{
int count = 0;
int num = value;
while (num)
{
count++;
num = num >> 1;
}
while (count--)
{
num = value;
num = value >> count;
if ((num & 1) == 1)
printf("%d", 1);
else
printf("%d", 0);
}
}
int main()
{
int a =10;
fun(a);
ba(a);
system("pause");
return 0;
}
3.将三个数按从大到小输出。
void swap(int *a, int *b, int *c)
{
int tem = 0;
if (*a < *b)
{
tem = *a;
*a = *b;
*b = tem;
}
if (*a < *c)
{
tem = *a;
*a = *c;
*c = *a;
}
if (*b < *c)
{
tem = *b;
*b = *c;
*c = tem;
}
}
int main()
{
int a = 10;
int b = 30;
int c = 5;
swap(&a, &b, &c);
printf("a=%d b=%d c=%d", a, b, c);
getchar();
return 0;
}
4.求两个数的最大公约数。
int main()
{
int a = 28;
int b = 24;
int count = 0;
if (a%b == 0)
count = b;
else if (b%a == 0)
count = a;
else
{
count = a%b;
while (count != 0)
{
a = b;
b = count;
count = a%b;
}
}
printf("%d", b);
system("pause");
return 0;
}
原文地址:http://10810512.blog.51cto.com/10800512/1755473