Question about two arrays?

Source: Internet
Author: User
Question about two arrays? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiBase/html/delphi_20061217001324138.html
Problem 1: Find the values and locations of different numbers and common numbers of the two arrays, and store the values at the same position as the same value to the array S1 ...... S9
For example:
Array value r1 is {1, 6, 23, 15, 5, 28, 8, 11, 10, 19}
Array value R2 is {, 3}

You need to find out:
Array values of different numbers of R1: S1}
Positions of different numbers of array values r1: S2 {0, 1, 5, 6, 8, 9}
Value of array R2: S3 {2, 4, 3}
Positions of different numbers of array values R2: S4 {0, 2, 5, 6, 8}
Total number of two Arrays: S5 {, 15, 5, 11}
The two arrays have a total of R1 positions: S6 {2, 3, 4, 7}
The two arrays have a total of numbers. The positions of R1 are S7 {1, 3, 4, 7}
Values of the two arrays with the same value in the same position: S8 {, 11}
Location where the two arrays share the same value: S9 {3, 4, 7}

Note: in practice, dynamic arrays are used, and they are large. You need a streamlined approach !!




Question 2: Sort arrays in ascending order of values. Same as above !!!

VaR
R1: array of integer;
R2: array of integer;
Sl1: tstringlist;
Sl2: tstringlist;
Idx: integer;
Index: integer;
S1: array of integer;
S2: array of integer;
S3: array of integer;
S4: array of integer;
S5: array of integer;
S6: array of integer;
S7: array of integer;
S8: array of integer;
S9: array of integer;
Begin
Setlength (R1, 10 );
Setlength (R2, 9 );
R1 [0]: = 1;
R1 [1]: = 6;
R1 [2]: = 23;
R1 [3]: = 15;
R1 [4]: = 5;
R1 [5]: = 28;
R1 [6]: = 8;
R1 [7]: = 11;
R1 [8]: = 10;
R1 [9]: = 19;

R2 [0]: = 2;
R2 [1]: = 23;
R2 [2]: = 4;
R2 [3]: = 15;
R2 [4]: = 5;
R2 [5]: = 17;
R2 [6]: = 12;
R2 [7]: = 11;
R2 [8]: = 3;

Sl1: = tstringlist. Create;
Sl2: = tstringlist. Create;

// '%. 8d' has two purposes,
// One is sorting, and the other is meeting the requirements of big data

For idx: = low (R1) to high (R1) Do
Sl1.add (format ('%. 8d', [R1 [idx]);

For idx: = low (R2) to high (R2) Do
Sl2.add (format ('%. 8d', [R2 [idx]);

// For ease of display, I used ListBox.
// In your actual situationProgram You can use tstringlist instead.

Listbox1.sorted: = true;
Listbox2.sorted: = true;
Listbox3.sorted: = true;
Listbox4.sorted: = true;
Listbox5.sorted: = true;
Listbox6.sorted: = true;
Listbox7.sorted: = true;
Listbox8.sorted: = true;
Listbox9.sorted: = true;

For idx: = 0 to PRED (sl1.count) Do
Begin
Index: = sl2.indexof (sl1 [idx]);
If index <0 then
Begin
Listbox1.items. Add (sl1 [idx]);
Listbox2.items. Add (format ('%. 8d', [idx]);
End else
Begin
Listbox5.items. Add (sl1 [idx]);
Listbox6.items. Add (format ('%. 8d', [idx]);
Listbox7.items. Add (format ('%. 8d', [Index]);
If idx = index then
Begin
Listbox8.items. Add (sl1 [idx]);
Listbox9.items. Add (format ('%. 8d', [idx]);
End;
End;
End;

For idx: = 0 to PRED (sl2.count) Do
Begin
If sl1.indexof (sl2 [idx]) <0 then
Begin
Listbox3.items. Add (sl2 [idx]);
Listbox4.items. Add (format ('%. 8d', [idx]);
End;
End;

// The rest of the work is to put the ListBox ---> S array,
// I only write one image, and the rest will be painted like a gourd}

Setlength (S1, listbox1.items. Count );
For idx: = 0 to PRED (listbox1.items. Count) Do
S1 [idx]: = strtoint (listbox1.items [idx]);

// Above Algorithm Prerequisites
// 1. Each array has no duplicates.
// 2. The R1 length is greater than or equal to the R2 length.

// If you want to meet any conditions, the algorithm does not change much, but only has one more traversal.


End;

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.