下面的代碼顯示了三個數字中的中間數字。我怎樣才能對五個數字做同樣的事情?
通過對五個數字重復相同的邏輯,或者有更好的方法嗎?
#include <stdio.h>
#include <stdlib.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers\n");
scanf("%d %d %d", &num1, &num2, &num3); // takes input from user
// checking num1 is a middle number or not
if (num2 > num1 && num1 > num3 || num3 > num1 && num1 > num2) {
printf("\n%d is a middle number", num1);
}
//checking num2 is a middle number or not
if (num1 > num2 && num2 > num3 || num3 > num2 && num2 > num1) {
printf("\n%d is a middle number", num2);
}
//checking num3 is a middle number or not
if (num1 > num3 && num3 > num2 || num2 > num3 && num3 > num1) {
printf("\n%d is a middle number", num3);
}
return 0;
}
uj5u.com熱心網友回復:
通過對五個數字重復相同的邏輯,或者有更好的方法嗎?
這取決于您所說的“重復相同的邏輯”的確切含義以及您如何衡量“更好”。
僅使用六次比較(但不少于)就可以確定五個專案的中位數。原則上,這可以編碼為純決策樹(不交換或使用臨時變數)。這將是問題中提供的代碼的非常接近的五元素模擬。它會非常高效,并且不會修改輸入,但很難閱讀或理解(更不用說長篇大論了)。
也可以在該決策樹方法中引入一些臨時變數,使其更短,更容易遵循,但代價是少量開銷,并且可能會降低計算效率。
或者可以對輸入進行排序并從結果中選擇中間元素。這可能很容易理解,具體取決于實作細節,但效率會低得多。
或者可以實作部分排序以節省一點時間而不是完整排序,但仍然非常清楚(在使用時,但不一定是整體)。提前終止選擇排序可以做到這一點,或者快速選擇實作。這些仍然比決策樹風格的方法效率低,而且代碼更多。
如果效率是一個關鍵考慮因素,那么我至少會在“臨時決策樹”方法上測驗這種變化:
int median_of_five(int a, int b, int c, int d, int e) {
struct pair { int x, y; };
struct pair temp1;
struct pair temp2;
struct pair pair1;
struct pair pair2;
// arrange a, b, c, and d into two pairs, each having its elements in order
if (a <= b) {
temp1 = (struct pair) { a, b };
} else {
temp1 = (struct pair) { b, a };
}
if (c <= d) {
temp2 = (struct pair) { c, d };
} else {
temp2 = (struct pair) { d, c };
}
// order the two pairs by their first elements
if (temp1.x <= temp2.x) {
pair1 = temp1;
pair2 = temp2;
} else {
pair1 = temp2;
pair2 = temp1;
}
/*
* These mathematical relationships now hold:
*
* pair1.x <= pair1.y
* pair1.x <= pair2.x <= pair2.y
*
* The order of e relative to the others determines how we proceed.
*/
if (pair1.y <= e) {
// pair1.x <= pair1.y <= e; pair1.x <= pair2.x <= pair2.y
if (pair2.x < pair1.y) {
// pair1.x <= pair2.x < pair1.y <= e; pair2.x <= pair2.y
// return the lesser of pair1.y and pair2.y
return (pair1.y <= pair2.y) ? pair1.y : pair2.y;
} else {
// pair1.x <= pair1.y < e, pair2.x; pair2.x <= pair2.y
// return the lesser of e and pair2.x
return (e <= pair2.x) ? e : pair2.x;
}
} else {
// pair1.x, e <= pair1.y; pair1.x <= pair2.x <= pair2.y
if (pair2.x <= e) {
// pair1.x <= pair2.x <= e <= pair1.y; pair2.x <= pair2.y
// return the lesser of e and pair2.y
return (e <= pair2.y) ? e : pair2.y;
} else {
// pair1.x, e <= pair1.y, pair2.x; pair2.x <= pair2.y
// return the lesser of pair1.y and pair2.x
return (pair1.y <= pair2.x) ? pair1.y : pair2.x;
}
}
}
這當然可以適用于以陣列的形式輸入,而不是五個單獨的值。
六級“全決策樹”版本并不是我真正想寫的東西。
uj5u.com熱心網友回復:
- 使用陣列,而不是一組變數。
- 然后對陣列進行排序
- 然后取陣列的中間。
#include <stdio.h>
#include <stdlib.h>
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
int cmp(void* a, void* b)
{
return *(int*)b - *(int*)a;
}
int main()
{
int num[5];
printf("Enter %d numbers\n", COUNT_OF(num));
for(int i=0; i<COUNT_OF(num); i)
{
scanf("%d",&num[i]);
}
qsort(num, COUNT_OF(num), sizeof(int), cmp);
printf("Middle value at index %d is %d\n", COUNT_OF(num)/2, num[COUNT_OF(num)/2]);
return 0;
}
uj5u.com熱心網友回復:
不是比較最優的,但可能相當快,這是一個偽代碼:
int R[]= { 0, 0, 0, 0, 0 };
R[n1<n2 ? 1 : 2] ; R[n1<n3 ? 1 : 3] ; R[n1<n4 ? 1 : 4] ; R[n1<n5 ? 1 : 5] ;
R[n2<n3 ? 2 : 3] ; R[n2<n4 ? 2 : 4] ; R[n2<n5 ? 2 : 5] ;
R[n3<n4 ? 3 : 4] ; R[n3<n5 ? 3 : 5] ;
R[n4<n5 ? 4 : 5] ;
告訴您 10 次比較中所有五個元素的排名。中位數是秩為 2 的元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487159.html
標籤:C
