我有多個陣列,我需要回傳客戶輸入的訂單的值,這些陣列有重復的值,因此即使在下訂單時將它們從低到高排序后,他也會獲取重復的值。我怎樣才能洗掉重復的?前任。int testA1[] = { 25000, 20000, 29499, 10000, 20000, 29000, 25000, 20000, 25000, 10000 }; 國際訂單1 = 3; <-- 它應該列印出 29000 但它列印出 25000
#include<stdio.h>
int lowestPrice(int array[], int size, int order){
int tempArray[size];
for (size_t i = 0; i < size; i )
tempArray[i] = array[i];
for (size_t i = 0; i < size; i )
for (size_t j = i; j < size; j )
if (tempArray[j] < tempArray[i]) {
int tmp = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = tmp;
}
int j = 0;
for (size_t i = 0; i < size -1; i ){
if(tempArray[i] != tempArray[i 1])
{
tempArray[j] = tempArray[i];
j ;
}
tempArray[j] = tempArray[i-1];
}
if(order > size || order < 0){
return -1;
}
else{
return tempArray[order];
}
}
uj5u.com熱心網友回復:
main(void){
int testA1[] = { 25000, 20000, 29499, 10000, 20000, 29000, 25000, 20000 , 25000 , 10000 };
int size = sizeof(testA1) / sizeof(int); //size of array
// if your orders are only going to be positive numbers as it should for your wealth :
// (change duplicate value by -1)
for (int i = 0; i < size ; i)
{
int is_duplicate = testA1[i];
if (is_duplicate == -1)
continue ;
int j = i 1;
while ( j < size) {
if (is_duplicate == testA1[j])
testA1[j] = -1;
j;
}
}
// sort it ;) :
for (size_t i = 0; i < size; i )
for (size_t j = i; j < size; j )
if (testA1[j] < testA1[i]) {
int tmp = testA1[i];
testA1[i] = testA1[j];
testA1[j] = tmp;
}
int new_index = 0;
while (testA1[new_index] == -1)
new_index;
// here is for you to see the new state (you can remove this part)
for (int i = new_index; i < size; i)
printf("%d\n", testA1[i]);
// now get your order price from the new index (index your_desired_value)
return testA1[new_index -->your_desired_value<--];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365839.html
上一篇:為什么<constchar*>和<constchar[]>有非常不同的記憶體或指標行為?
下一篇:在多維陣列中搜索值
