温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c语言一些简单算法

发布时间:2020-07-10 10:00:55 来源:网络 阅读:292 作者:走走停停吧 栏目:安全技术

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;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI