//實現三個數的交換
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>
void swap(int*x, int*y)
{
int t;
if (*x < *y)
{
t = *x;
*x = *y;
*y = t;
}
}
int main()
{
int a, b, c,t = 0;
printf("請輸入三個數,用空格隔開\n");
scanf("%d%d%d",&a,&b,&c);
swap(&a, &b);
swap(&a, &c);
swap(&b,&c);
printf("%d%d%d\n",a,b,c);
system("pause");
return0;
}
//求兩個數的最大公約數
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int max_cd(int*x, int*y)
{
while (*x % *y != 0)
{
if (*x > *y)
*x = *x - *y;
else*y = *y - *x;
}
return*y;
}
int max_cd1(int*x,int*y)
{
int z = 0;
int min = *x > *y ? *y : *x;
while (z = *x % *y != 0)
{
*x = *y;
*y = *y % z;
}
return*y;
}
int main()
{
int a, b;
int c = 0;
printf("Please enter teo numbers\n");
scanf("%d%d",&a,&b);
c = max_cd(&a, &b);
printf("a和b的最大公約數是:%d \n",c);
c = max_cd1(&a, &b);
printf("a和b的最大公約數是:%d \n", c);
system("pause");
return0;
}
//輸出十個數中最大的一個
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{
int i = 0;
int max = 0;
int a[10] = {1,6,5,9,8,7,3,4,12,2};
for(i = 0; i < 10; i ++ )
{
printf("%d ",a[i]);
}
max = a[0];
for (i = 1; i < 10; i++)
{
if (a[i]>max)
max = a[i];
}
printf("\n最大的值是:%d \n",max);
system("pause");
return0;
}
//不使用臨時變量的交換方法
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{
int i = 0;
int j = 0;
printf("請輸入兩個整數,用空格隔開\n");
scanf("%d%d", &i,&j);
i = i^j;
j = i^j;
i = j^i;
printf("%d%d\n",i,j);
system("pause");
return0;
}
//交換兩個數,創建臨時變量
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{
int a = 0;
int b = 0;
int t = 0;
printf("請輸入兩個數,用空格隔開\n");
scanf("%d%d", &a,&b);
t = a;
a = b;
b = t;
printf("%d%d",a,b);
system("pause");
return0;
}