4

我正在尝试在 Python 中编写与以下语句等效的 Ada:L = [[] for i in range(n)]

我正在解决一个动态编程问题,如果第 i 个数组的元素少于第 j 个数组,我的计划是最终将 L 中的第 j 个数组的内容复制到第 i 个数组(j < i)中。

我发现了如何通过以相反的顺序定义其范围来创建一个空数组。因此,例如, arr2 将是一个空数组,创建如下:

arr2:整数数组(2 .. 1);

我的问题是,如何定义更大的数组 L 以包含 n 个这样的 arr2 数组?

请告诉我。

更新:我能够使用以下答案使其正常工作。这是我的代码。

package Integer_Vectors is new Ada.Containers.Vectors
    (Index_Type   => Natural,
    Element_Type => Integer);

    N: Integer;

    Array_Of_Vectors : array(1 .. N) of Integer_Vectors.Vector := (others => Integer_Vectors.Empty_Vector);

    Input_Sequence: Integer_Vectors.Vector;

    Max: Integer_Vectors.Vector;

    Input_List : String := Get_Line;

    IntCast : Integer;

    Last : Positive := 1;
    
    begin

    while Last < Input_List'Last loop
        Get(Input_List(Last..Input_List'Last),IntCast,Last);
        Input_Sequence.Append(IntCast);
        Last := Last + 1;
    end loop;
    
    N := 0;

    for i of Input_Sequence loop
        N := N + 1;
    end loop;
4

1 回答 1

6

在 Ada L 将

L : array (1 .. N, 1 .. 0) of Integer;

但它没用,因为你以后无法扩展它。@Zerte 是对的。

例如,您可以在不定元素上使用向量。像这样

with Ada.Containers.Indefinite_Vectors;

type Integer_Array is array (Positive range <>) of Integer;

Empty : constant Integer_Array := (1 .. 0 => <>);

package Integer_Array_Vectors is new
 Ada.Containers.Indefinite_Vectors
   (Index_Type => Positive, Element_Type => Integer_Array);

L : Integer_Array_Vectors.Vector;

L.Append (Empty, N);

if L (J)'Length > L (I)'Length then
   L.Replace_Element (I, L(J));
end if;
于 2020-09-10T10:21:56.800 回答