Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Some simple algorithms in C language

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report