Hello, I can not understand how to solve this problem, please, who can, give the code with explanations ... Thanks in advance.

Task:

The number n is entered from the keyboard - the order of substitution, and then a table of two lines. Determine whether this table is an nth order permutation.

Пример: 5 1 2 3 4 2 4 2 1 3 5 NO 5 1 5 3 4 2 4 7 1 3 5 NO 4 3 2 1 4 4 2 1 3 YES 5 1 2 3 4 4 2 1 3 NO 5 1 5 3 4 2 4 3 5 3 5 NO 5 1 5 3 4 2 4 2 1 3 5 YES 

Here is my solution (to display an error - help):

 type DinArr = array of longint; function Sort(arr: DinArr): DinArr; var i, j: longint; b: word; begin for i:= 1 to length(arr)-1 do begin for j:= 1 to length(arr)-i do begin if (arr[j] > arr[j+1]) then begin b:= arr[j]; arr[j]:= arr[j+1]; arr[j+1]:= b; end; end; end; Sort:= arr; end; var a, b: DinArr; a2, b2: DinArr; i, j, n: longint; begin readln(n); setlength(a, 1); setlength(b, 1); for i:= 1 to length(a) do begin setlength(a, length(a)+1); readln(a[i]); end; for i:= 1 to length(b) do begin setlength(b, length(b)+1); readln(b[i]); end; setlength(a2, 100); setlength(b2, 100); a2:= Sort(a); b2:= Sort(b); for i:= 1 to length(a) do begin if (a2[i] <> b2[i]) then begin writeln('NO'); exit; end; end; writeln('YES'); readln; end. 

    1 answer 1

    And everything is simple. And programming is an art. And art is MATAN! In general, we read the definition:

    Definition 1.14. The substitution (permutation) of the set N {1,2, .. n} consisting of n first natural numbers is called a one-to-one mapping of the set N onto itself.

    Opacha! Checking our examples:

     5 1 2 3 4 2 4 2 1 3 5 NO 

    As you can see from the example, there are 2 deuces at the top, and one O_O at the bottom, which means the answer is NEIN (German no)

     4 3 2 1 4 4 2 1 3 YES 

    And here's all okey docks. Well, in general, the code can be written like this (perfect code): http://paste.org.ru/?ruv214 I wrote it manually, did not compile it, but it should work. In general, the essence is that there is just an array, it will be an array of occurrences of different numbers in the set, well, here are the array indices - these are all the different numbers that you are allowed to enter. Well, here we enter both the numbers and under the appropriate indices we increase the elements of the array. And when we enter the second line, we also reduce it in the same way, and if the array will eventually be the same as at the beginning (zero by default), then everything is fine. The answer is yes, otherwise no.

    • one
      Judging by the fourth example, we must also verify that the number of numbers is correct. - dzhioev
    • How would my code demonstrate this? - Anton Lakotko
    • How could your code be impossible to read. - dzhioev
    • Know how to throw away too much. Everything is clear there, if you do not look at the heap of writeln's. This is a half-page program, and not some kind of undocumented project. paste.org.ru/?ou318v without comment - Anton Lakotko
    • And if you use dynamic arrays (FreePascal)? Just the length of the string should not be entered, I wrote the code here, but the error comes out, can you help me figure it out? - Kirill Egorov