Huawei Machine test-difference sorting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.