Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Some simple algorithms in C language

Shulou Source: shulou.com Published: 2022-06-01 05:07:23 10月04日 Update

1. Swap the contents of array A with the contents of array B. (the array is the same size)

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 #include #include #include 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 ("even digit:")

Printf ("% d", num & 1)

}

Else

{

Printf ("Odd bits:")

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. Output the three numbers from large to small.

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 ("averse% d baked% d cased% d", a, b, c)

Getchar ()

Return 0

}

4. Find the greatest common divisor of two numbers.

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

}

Tags: Digit array number binary content sequence output maximum two common divisor maximum common divisor algorithm language Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux Shulou Tech Info MySQL Xiaomi