C language---little exercises every day, output from big to small

Topic: Output from large to small


  Write code to output three integers from largest to smallest.
  For example:
  input: 2 3 1
  output: 3 2 1

int main() {
	// 初始化
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	scanf("%d %d %d", &a, &b, &c);

	if (a < b) { // 如果 a < b,
		d = a;  // 先把 a的值 赋值给 d
		a = b;  // 因为 a < b ,所以 把 b 的值给 a。,如果 a > b 则不进行操作
		b = d; //  因为 前面 a 的值给了 d,所以 d 的值 赋值给 b
	}
	if (a < c) { // 如果 a < c
		d = a;  // 先把 a的值 赋值给 d
		a = c; // 如果 a < c 就把 c的值赋值个a,如果 a > c 则不进行操作
		c = d; // 因为 前面 a的值赋值个d,所以 d的值赋值给 c
	}
	if (b < c) { // 如果 b < c
		d = b; // 先把 b的值赋值给了 d
		b = c; // 如果 b < c就把c的值赋值给b,如果 b > c则不进行操作
		c = d; // 因为 前面 b的值赋值给了d,所以 d的值赋值给了 c
	}
	// 试一下 是不是 咱们的预期结果一样
	printf("%d > %d > %d", a, b, c);

	return 0;
}

Problem-solving idea: I have already written it in the code. In fact, the three numbers are compared in sequence, the maximum value is obtained, and then the minimum value is obtained. Not difficult. It takes a little understanding to understand.

Supongo que te gusta

Origin blog.csdn.net/m0_58724783/article/details/131885457
Recomendado
Clasificación