由浅入深

1.给定两个整形变量的值,将两个值的内容进行交换。

#include <stdio.h>
int main ()
{
	int a = 5;
	int b = 15;
	int temp = 0;
	printf("初始值: a=%d, b=%d\n",a,b);
 
	temp = a;//temp=5
	a = b;//a=15
	b = temp;//b=5
	printf("交换后: a=%d, b=%d\n",a,b);
 
	return 0;
}

2.不允许创建临时变量,交换两个数的内容

#include <stdio.h>
int main()
{
	int a,b;
	printf("输入a,b:");
	scanf("%d%d",&a,&b);
	a=a+b;
	b=a-b;
	a=a-b;
	printf("交换后的数:\na=%d\nb=%d\n",a,b);
 
}

3.求10 个整数中最大值。

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int max = arr[0];
	int sz = sizeof(arr) / sizeof(arr[0]);
	int i = 0;
	for (i = 1; i < sz; i++)
	{
		if (arr[i]>max)
		{
			max = arr[i];
		}
	}
	printf("max=%d\n", max);
	system("pause");
	return 0;
}

4.将三个数按从大到小输出。

#include<stdio.h>
 
int main()
{
	int a,b,c,t;
	printf("请输入三个数:\n");
	scanf("%d%d%d",&a,&b,&c);
 
 if(a<b)
 {
	 t=a;
	 a=b;
	 b=t;
 }
 if(a<c)
 {
	 t=a;
	 a=c;
	 c=t;
 }
 if(b<c)
 {
	 t=b;
	 b=c;
	 c=t;
 }
 printf("%d %d %d\n",a,b,c);
}

5.求两个数的最大公约数。

#define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
int  main(void)
{
	int x, y, temp;
	int r;
	printf("请输入两个正整数:\n");
	scanf("%d %d", &x, &y);
	r = x % y;
	temp = y;
	while (r != 0)
	{
		x = y;
		y = r;
		r = x % y;
	}
	printf("它们的最大公约数为:%d\n", y);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43869411/article/details/84678999