【发布时间】:2021-06-05 13:32:00
【问题描述】:
我需要编写一个程序以特定格式打印数组的所有子数组。
Example-
I/o:
n = 3
A = (1,2,3) where n is the size of the array and A is the array itself.
O/p:
(1),(2),(3)
(1),(2,3)
(1,2),(3)
(1,2,3)
我能够使用两个循环获取所有子数组,但无法按此特定顺序生成输出。我正在使用 Java 进行编码。 我的代码如下: - a[]→n个元素的整数数组
for(int i=0;i<a.length;i++){
String s = "";
for(int j=i;j<a.length;j++){
s = s + a[j] + " ";
System.out.println(s);
}
这段代码给出了所有可能的子数组,但不是所有可以从数组形成的传染性子数组组合。
【问题讨论】:
标签: java arrays dynamic-programming sub-array