Вопрос задан 01.05.2019 в 13:38. Предмет Информатика. Спрашивает Кузьмин Дима.

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

0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Кайратова Динара.
// PascalABC.NET 3.2, сборка 1436 от 01.05.2017
// Внимание! Если программа не работает, обновите версию!

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
Это   первая   стро  ка
А     это    вторая       строка
Это- третья
    Четвертая - последняя
*** РЕЗУЛЬТАТ ***
Это- третья
    Четвертая - последняя
Это   первая   стро  ка
А     это    вторая       строка
0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

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.

0 0

Топ вопросов за вчера в категории Информатика

Последние заданные вопросы в категории Информатика

Задать вопрос