
Дан массив строк. Упорядочить строки массива по возрастанию количества пробелов в строках. В Паскаль


Ответы на вопрос

// Внимание! Если программа не работает, обновите версию!
begin
var n:=ReadlnInteger('Количество строк: ');
var a:=new string[n];
for var i:=0 to n-1 do a[i]:=ReadlnString;
Writeln('*** РЕЗУЛЬТАТ ***');
a:=a.Select(s->(s,s.ToCharArray.Where(c->c=' ').Count))
.OrderBy(vk->vk[1]).Select(vk->vk[0]).ToArray;
a.Println(NewLine)
end.
Пример
Количество строк: 4
Это первая стро ка
А это вторая строка
Это- третья
Четвертая - последняя
*** РЕЗУЛЬТАТ ***
Это- третья
Четвертая - последняя
Это первая стро ка
А это вторая строка



Sorting Strings in Pascal by the Number of Spaces
To sort an array of strings in Pascal by the number of spaces in each string, you can use a sorting algorithm to compare the number of spaces in each string and rearrange the array accordingly.
Here's a sample code snippet to achieve this:
```pascal program SortStringsBySpaces;
var strings: array[1..5] of string = ('hello world', 'this is a test', 'a b c', 'one two three', 'no spaces'); spacesCount: array[1..5] of integer; i, j, tempCount: integer; tempStr: string;
begin // Count the number of spaces in each string for i := 1 to 5 do begin spacesCount[i] := 0; for j := 1 to length(strings[i]) do begin if strings[i][j] = ' ' then spacesCount[i] := spacesCount[i] + 1; end; end;
// Sort the strings based on the number of spaces for i := 1 to 4 do begin for j := 1 to 5 - i do begin if spacesCount[j] > spacesCount[j + 1] then begin // Swap the number of spaces tempCount := spacesCount[j]; spacesCount[j] := spacesCount[j + 1]; spacesCount[j + 1] := tempCount;
// Swap the strings tempStr := strings[j]; strings[j] := strings[j + 1]; strings[j + 1] := tempStr; end; end; end;
// Output the sorted strings for i := 1 to 5 do writeln(strings[i]);
end. ```
In this code, we first count the number of spaces in each string and store them in an array. Then, we use a simple sorting algorithm to sort the strings based on the number of spaces. Finally, we output the sorted strings.
This code can be adapted to work with any array of strings in Pascal.


Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili