Sorts the shape array by the difference size of the specified integer and returns in ascending order of the difference.
Required Implementation method:
Public staticint[] Calctimes (int[] num, int value);
"Input" num: integer array;
The integer specified by value
"Return" returns an array of integers in ascending order, sorted by the difference between each integer and the specified integer
"Note" Only needs to complete the function algorithm, the middle does not need any IO input and output
Example
Input: num = {1,2,3,4,5,10,17,18,19} value = 5
return: {5,4,3,2,1,10,17,18,19}
#include <iostream> #include <math.h>using namespace std;int len;int compare (const void *p,const void *q) { return * (int *) p-* (int *) Q;} void calctimes (int num[],int value) {int i,k,b[100],j=0;for (i=0;i<len;i++) {b[j++]=abs (num[i]-value);} Qsort (b,len,sizeof (int), compare), for (i=0;i<len;i++) for (k=0;k<len;k++) if (ABS (Num[k]-value) ==b[i]) {printf ( "%d", num[k]); break;} printf ("\ n");} int main () {int n,m,i=0,a[100];scanf ("%d", &n), while (GetChar ()! = ' \ n ') {a[i++]=n;scanf ("%d", &n);} A[i++]=n;len = i;scanf ("%d", &m); Calctimes (a,m); return 0;}
Test results, may not be thoughtful, welcome to check the leak:
Huawei Machine test-difference sorting