溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

c語言一些簡單算法

發(fā)布時(shí)間:2020-07-10 10:00:55 來源:網(wǎng)絡(luò) 閱讀:301 作者:走走停停吧 欄目:安全技術(shù)

1.將數(shù)組A中的內(nèi)容和數(shù)組B中的內(nèi)容進(jìn)行交換。(數(shù)組一樣大)

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.獲取一個(gè)數(shù)二進(jìn)制序列中所有的偶數(shù)位和奇數(shù)位,分別輸出二進(jìn)制序列。

#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("偶數(shù)位:");

printf("%d ", num & 1);

}

else

{

printf("奇數(shù)位:");

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.將三個(gè)數(shù)按從大到小輸出。

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.求兩個(gè)數(shù)的最大公約數(shù)。

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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI