您好,登錄后才能下訂單哦!
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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。