第二次练习

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

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int a = 10;
	int b = 20;
	int c;
	c = b;
	b = a;
	a = c;
	printf("%d,%d", a,b);
	system("pause");
	return 0;
}

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

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int a = 10;
	int b = 20;
	a = a + b;
	b = a - b;
	a = a - b;
	printf("%d,%d",a, b);
	system("pause");
	return 0;
	
}

3.求十个整数中的最大值。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>

int main(){
	int arr[10];
	int i;
	int tmp;
	printf("请输入10个整数:\n");
	for (i = 0; i < 10; i++){
		scanf("%d", &arr[i]);
	}
	for (i = 0; i < 10; i++){
		if (arr[0] <= arr[i + 1]){
			tmp = arr[i + 1];
			arr[i + 1] = arr[0];
			arr[0] = tmp;
		}
	}
	printf("10个整数中最大的是:");
	printf("%d\n", arr[0]);
	system("pause");
	return 0;
}

4.将三个数按从大到小排序。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>

int main(){
	int a, b, c, d;
	scanf("%d,%d,%d", &a, &b, &c);
	if (a < b){
		d = a;
		a = b;
		b = d;
	}
	if (a < c){
		d = a;
		a = c;
		c = d;
	}
	if (b < c){
		d = b;
		b = c;
		c = d;
	}
	printf("%d,%d,%d", a, b, c);
	system("pause");
	return 0;
}

5.求俩个整数最大公约数。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

int main()
{
	int a, b, temp;
	int Division;
		printf("请输入两个整数(a,b):\n");
		scanf("%d,%d", &a, &b);
		if (a < b){
			temp = a;
			a = b;
			b = temp;
		}
		while (a%b != 0){
			temp = a%b;
			a = b;
			b = temp;
		}
		printf("最大公约数为:%d\n", b);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43253845/article/details/82842987