前天去面试, 碰到这么一个古怪笔试题, 花了10分钟才看懂题目, 要了两张草稿纸也没作出来, 汗!无脸见人! 大家来讨论一下题目: 将一个交错数据合并为一个一维数组
输入: strJaggedArray[][], 由多个一维数组(长度不定,个数不定)组成的交错数组
输出: strArray[], 由strJaggedArray[r][c]中的元素以"&"为分隔符拼合而成, 是strJaggedArray中数组元素的无重复组合(不考虑顺序)举例:
输入: strJaggedArray[0]=new string[] {"we","are","student"};
strJaggedArray[1]=new string[] {"say","what"};
输出: strArray[]={"we&say","we&what","are&say","are&what","student&say","student&what"}输入: strJaggedArray[0]=new string[] {"cs","app"};
strJaggedArray[1]=new string[] {"good","cool","dev"};
strJaggedArray[2]=new string[] {"king","of","the","world"};
输出: strArray[]={"cs&good&king","cs&good&of","cs&good&the","cs&good&world","cs&cool&king","cs&cool&of","cs&cool&the","cs&cool&world",...}
a[0] = new string[] { "cs", "app" };
a[1] = new string[] { "good", "cool", "dev" };
a[2] = new string[] { "king", "of", "the", "world" };
System.Collections.ArrayList arrlist = new System.Collections.ArrayList(); for (int i = 0; i < a.Length-1; i++)
{
for (int j = i + 1; j < a.Length; j++)
{
for (int m = 0; m < a[i].Length; m++)
{
for (int n = 0; n < a[j].Length; n++)
{
arrlist.Add(a[i][m] + "&" + a[j][n]);
}
}
}
} Array b = arrlist.ToArray(typeof(string));
---------------------------------------------
应该??
lookatliu(独孤常败)的程序的结果是错误的,但思路走对了
cs&good cs&cool cs&dev.....这就是该程序的结果,与楼主给出的参考是不一样的
string[][] a = new string [3][];
a[0] = new string[] { "cs", "app" };
a[1] = new string[] { "good", "cool", "dev" };
a[2] = new string[] { "king", "of", "the", "world" };
System.Collections.ArrayList arrlist = new System.Collections.ArrayList();
int[] index = new int[a.Length]; while (index[0] < a[0].Length)
{
string temp = "";
for (int i = 0; i < index.Length; i++)
{
temp += a[i][index[i]] + "&";
} arrlist.Add(temp.Substring(0, temp.Length-1)); index[index.Length - 1]++; for (int i = index.Length - 1; i > 0; i--)
{
if (index[i] >= a[i].Length)
{
index[i] = 0;
index[i - 1]++;
}
}
} Array b = arrlist.ToArray(typeof(string));
---------------------------------------------
这回这个对了~哈哈,少看了个例子。不过这个题还是有点难度的,如果面试紧张的话很可能答不出来
--------------------------------------------
正解 :)
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
string[][] strJaggedArray= new string[2][];
strJaggedArray[0]= new string[]{"we","are","student"};
strJaggedArray[1]= new string[]{"say","what"};
int width= strJaggedArray.Length;
string strNeedValue;
for(int i=0;i<strJaggedArray[0].Length;i++)
{
strNeedValue=strJaggedArray[0][i]+"&";
for(int j=0;j<strJaggedArray[1].Length;j++)
{
string temp;
temp=strNeedValue;
temp=temp+strJaggedArray[1][j];
Console.WriteLine(temp);
}
}
}
}
}
public static List<string> list = new List<string>();
public static string tstr = string.Empty;
static void Main(string[] args)
{
string[][] array2 = new string[4][];
array2[0] = new string[] { "we", "are", "student" };
array2[1] = new string[] { "good", "cool", "dev" };
array2[2] = new string[] { "king", "of", "the", "world" };
array2[3] = new string[] { "a", "b", "c", "d", "e" };
MergeStringArray(array2);
}
public static void MergeStringArray(string[][] array2)
{
for(int i=0; i<array2[0].Length; i++)
_MergeStringArray(array2, 0, i);
}
public static void _MergeStringArray(string[][] array2, int row, int col)
{
if (row < array2.Length - 1)
{
if (tstr != string.Empty)
tstr += "&";
tstr += array2[row][col]; if (row != array2.Length - 2)
for (int i = 0; i < array2[row + 1].Length; i++)
_MergeStringArray(array2, row + 1, i);
else
_MergeStringArray(array2, row + 1, col); int index = tstr.LastIndexOf('&');
if(index != -1)
tstr = tstr.Substring(0, index);
else
tstr = string.Empty; }
else if (row == array2.Length - 1)
{
for (int i = 0; i < array2[row].Length; i++)
list.Add(tstr + "&" + array2[row][i]);
return;
}
}
for (int j = 0; j <xx.length;j++)
for (int (k = 0; k < yy.length;k++)
[i][j][k]这样的循环吗,这是求所有组合最简单的方法了
a[0]=new string[]{ "cs", "app" };
a[1]=new string[]{ "good", "cool", "dev" };
a[2]=new string[]{ "king", "of", "the", "world" };
for(int i=0;i<a[0].Length;i++)
{
for(int j=0;j<a[1].Length;j++)
{
for(int k=0;k<a[2].Length;k++)
{
string b=a[0][i]+"&"+a[1][j]+"&"+a[2][k];
Console.WriteLine(b);
}
}
}
你怎么知道要嵌套几个for?
public Interlaced(string[] a, string[] b, string[] c)
{
astrTest1 = a;
astrTest2 = b;
astrTest3 = c;
}
public string[] interlaceArray()
{
int i, j, k;
string temp="";
string[] returnValue;
for (i = 0; i < astrTest1.Length; i++)
{
for (j = 0; j < astrTest2.Length; j++)
{
for (k = 0; k < astrTest3.Length; k++)
{
if (temp == String.Empty)
{
temp = astrTest1[i] + "&" + astrTest2[j] + "&" + astrTest3[k];
}
else
{
temp = temp + "," + astrTest1[i] + "&" + astrTest2[j] + "&" + astrTest3[k];
}
}
}
}
returnValue = temp.Split(',');
return returnValue;
}
{ public string[] Marge(string[][] arrayMarged)
{
string[] arrReturn=null ;
foreach (string[] arrTemp1 in arrayMarged )
{
arrReturn = Marge(arrTemp1, arrReturn);
}
return arrReturn;
} private string[] Marge(string[] arrayMarged ,string[] arrTemp)
{
System.Collections.ArrayList altTomarged=new ArrayList(); if (arrTemp == null)
{
foreach (string strTemp in arrayMarged)
{
altTomarged.Add(strTemp);
} }
else
{
foreach (string strTemp1 in arrTemp)
{
foreach (string strTemp2 in arrayMarged )
{
altTomarged.Add(string.Format("{0}&{1}",strTemp1,strTemp2 ));
}
}
} System.Array arrReturn = altTomarged.ToArray(typeof(string));
return (string[]) arrReturn;
} }
{
for (j = 0; j < astrTest2.Length; j++)
{
for (k = 0; k < astrTest3.Length; k++)
如果人家的输入数组是4个怎么办?是n个怎么办?难道再加?
for (k1 = 0; k < astrTest4.Length; k++)
.
.
.
for (kn = 0; k < astrTestN.Length; k++)?
只是嵌套循环行吗?
{
string[][] arrMarge = new string[3][];
arrMarge[0]=new string[]{"a1","b1","c1"};
arrMarge[1]=new string[]{ "a2", "b2" };
arrMarge[2]=new string[]{ "a3", "b3","c3","d3" } ; ArrayMarge am = new ArrayMarge(); string[] arrMarged=am.Marge (arrMarge); }
class Class1
{
/// <summary>
/// 應用程式的主進入點。
/// </summary>
///
private string[][] strJaggedArray = new string[3][];
public Hashtable hash = new Hashtable();
private int count = 0;
private int strArrayLength = 0;
private int StrArrayCurrent = 0;
string [] strArray = null;
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此加入啟動應用程式的程式碼
//
Class1 justTry = new Class1();
justTry.Init();
}
private void Init()
{
strJaggedArray[0] = new string[]{ "guo" , "yong" , "cheng" };
strJaggedArray[1] = new string[]{ "is" , "a" };
strJaggedArray[2] = new string[]{ "good" , "boy" };
count = strJaggedArray.Length;
strArrayLength = 1;
for( int temp = 0 ; temp < count ; temp++ )
{
hash.Add( temp , strJaggedArray[temp].Length );
strArrayLength *= strJaggedArray[temp].Length;
strArray = new string[strArrayLength];
}
for( int temp = 0 ; temp < count ; temp++ )
{
RunFunc( 1 , strJaggedArray[0][temp] );
}
for( int i = 0 ; i < strArray.Length ; i++ )
{
Console.WriteLine( strArray[i].ToString() );
}
Console.ReadLine();
} private void RunFunc( int depth , string preString )
{
if( depth < count )
{
int temp = 0;
for( int i = 0 ; i < int.Parse( hash[depth].ToString() ) ; i++ )
{
temp = depth;
RunFunc( ++temp , preString+"@"+strJaggedArray[depth][i] );
}
}
else
{
strArray[StrArrayCurrent] = preString;
StrArrayCurrent++;
}
}
}
{
if(str.Length==1)
return null;
ArrayList al=new ArrayList();
foreach(string strT in str[0])
al.Add(strT);
int i,j;
for(i=1;i<str.Length;i++)
{
int len=al.Count;
string[] ren=(string[])al.ToArray(typeof(string));
al.Clear();
for(j=0;j<len;j++)
foreach(string strT in str[i])
al.Add(ren[j]+"&"+strT);
}
return (string[])al.ToArray(typeof(string));
}
---------------------------------------------using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace ConsoleApplication1
{
class Program
{
static void wood(string[][] ar)
{
ArrayList list = new ArrayList();
int x = 0;
while (x < ar.GetLength(0))
{
string[] strArr = ar[x];
if (x == 0)
foreach (string str in strArr)
list.Add(str);
else
{
ArrayList temp = new ArrayList();
for (int i = 0; i < list.Count; i++)
temp.Add(list[i].ToString());
list.Clear();
for (int i = 0; i < temp.Count; i++)
for (int j = 0; j < ar[x].GetLength(0); j++)
list.Add(temp[i].ToString() + "&" + ar[x][j]);
}
x++;
}
for (int i = 0; i < list.Count; i++)
Console.WriteLine(list[i].ToString());
Console.WriteLine(list.Count);
}
static void Main(string[] args)
{
string[][] strJaggedArray = new string[3][];
strJaggedArray[0] = new string[] { "cs", "app" };
strJaggedArray[1] = new string[] { "good", "cool", "dev" };
strJaggedArray[2] = new string[] { "king", "of", "the", "world" }; wood(strJaggedArray); string[][] str = new string[4][];
str[0] = new string[] { "cs", "app" };
str[1] = new string[] { "good", "cool", "dev" };
str[2] = new string[] { "good", "cs" ,"wood"};
str[3] = new string[] { "king", "of", "the", "world" }; wood(str);
}
}
}
--------------------------
我写的第二个程序已经满足这个要求了哦
{
int i = 0;
while(i<str[n].Length)
{
if(n<str.Length-1)
{
if(temp!=null)
{
text(str,temp + "&" + str[n][i],n+1);
}
else
{
text(str,temp + str[n][i],n+1);
}
}
else
{
Console.WriteLine(temp+"&"+str[n][i]);
}
i++;
}
public string[] GetStringList(string[] aList, string[] bList)
{
ArrayList sumList = new ArrayList();
foreach (string aitem in aList)
{
foreach (string bitem in bList)
{
string abItem = aitem + "&" + bitem;
sumList.Add(abItem);
}
} return (string[])sumList.ToArray();
}
{
int listCount = inStringList.Length;
string[] returnList = new string[] { };
for (int i = 0; i < listCount; i++)
{
returnList = GetStringList(returnList, inStringList[i]);
}
return returnList;
} public string[] GetStringList(string[] aList, string[] bList)
{
ArrayList sumList = new ArrayList();
foreach (string aitem in aList)
{
foreach (string bitem in bList)
{
string abItem = aitem + "&" + bitem;
sumList.Add(abItem);
}
} return (string[])sumList.ToArray();
}
复制如下代码直接可得结果:
private ArrayList MergeStringArray(ArrayList[] al)
{
if(al.Length==1)return al[0];
return mergeArr(al[0],DecArrLst(al));
}
private ArrayList mergeArr(ArrayList a,ArrayList[] b)
{
if(b==null||b.Length<1)return null;
ArrayList c=new ArrayList(),tmp=b[0];
foreach(object oa in a)
{
foreach(object ot in tmp)
{
c.Add(oa.ToString()+"&"+ot.ToString());
}
}
if(b.Length==1)return c;
return mergeArr(c,DecArrLst(b));
}
/// <summary>
/// 返回去掉第一个元素的ArrayList[],维数少1
/// </summary>
/// <param name="arl"></param>
/// <returns></returns>
private ArrayList[] DecArrLst(ArrayList[] arl)
{
if(arl==null||arl.Length<2)return null;
ArrayList[] tmpar=new ArrayList[arl.Length-1];
for(int i=0;i<tmpar.Length;i++)
{
tmpar[i]=arl[i+1];
}
return tmpar;
}
private ArrayList Str2ArrLst(string[] st)
{
ArrayList ar=new ArrayList();
foreach(string s in st)
{
ar.Add(s);
}
return ar;
}
private void button1_Click(object sender, System.EventArgs e)
{
ArrayList[] al=new ArrayList[3];
string[] a=new string[]{"cs","app"};
string[] b=new string[]{"good","cool","dev"};
string[] c=new string[]{"king","of","the","world"};
al[0]=Str2ArrLst(a);
al[1]=Str2ArrLst(b);
al[2]=Str2ArrLst(c);
ArrayList aa=MergeStringArray(al);
string sr="";
foreach(object o in aa)
{
sr+=","+o.ToString();
}
MessageBox.Show(sr);
//复制到粘贴板
Clipboard.SetDataObject(sr,true);
}
这个题目肯定是考递归的测试正确 string[][] a = new string[3][];
private void button1_Click(object sender, EventArgs e)
{
a[0] = new string[] { "cs", "app" };
a[1] = new string[] { "good", "cool", "dev" };
a[2] = new string[] { "king", "of", "the", "world" };
int allCount = 1;
for (int i = 0; i < a.Length; i++)
{
allCount *= a[i].Length;
}
string[] b = new string[allCount];
int pos = 0;
CalcArray(0, b,ref pos);
textBox1.Text = b[0];
}
void CalcArray(int k ,string[] b,ref int pos)
{
string BaseS = "";
BaseS = b[pos];
for (int i = 0; i < a[k].Length;i++ )
{
if (k == a.Length - 1)
{
b[pos] =BaseS+ a[k][i];
pos++;
}
else
{
b[pos] +=BaseS+ a[k][i] + "&";
CalcArray(k + 1, b,ref pos);
}
}
}
}
strJaggedArray[2][])*
strJaggedArray[3][])*
strJaggedArray[4][])*
strJaggedArray[5][])
......
String temp[]=strJaggedArray[0];所以:
for(int i=1 ; i<strJaggedArray.length;i++ ){
temp[]=GetStringList(temp,strJaggedArray[i]);
}
return temp[];//两个数组的组合
public string[] GetStringList(string[] aList, string[] bList)
{
ArrayList sumList = new ArrayList();
foreach (string aitem in aList)
{
foreach (string bitem in bList)
{
string abItem = aitem + "&" + bitem;
sumList.Add(abItem);
}
} return (string[])sumList.ToArray();
}
{
private static List getString(String[][] a, int[] index, List resultList)
{
String temp = "";
for (int i = 0; i < a.length; i++)
{
temp = temp + a[i][index[i]]; if (i < a.length - 1)
{
temp += "&";
}
}
resultList.add(temp);
//如果都循环到了最后的节点,返回
boolean isAllLeaf = true;
for (int i = 0; i < a.length; i++)
{
if (a[i].length - 1 > index[i])
{
isAllLeaf = false;
break;
}
}
//没有都到最后的节点,把还可以增长的Index加1,并把该index以后的index清0
//因为比如说,第二个数组的循环到了第二个数字,那第三个数组的循环应该从新从0开始
if (!isAllLeaf)
{
for (int i = a.length - 1; i >= 0; i--)
{
if (index[i] < a[i].length - 1)
{
index[i] += 1; for (int j = i + 1; j < a.length; j++)
{
index[j] = 0;
}
break;
}
}
resultList = getString(a, index, resultList);
}
return resultList;
} public static void main(String[] args)
{
String[][] a = new String[4][];
a[0] = new String[] { "cs", "app" };
a[1] = new String[] { "good", "cool", "dev" };
a[2] = new String[] { "king", "of", "the", "world" };
a[3] = new String[] { "king", "of", "the", "world" };
//增加了一个数组index来记录,当前循环到了第几个数
int[] index = new int[] { 0, 0, 0, 0 };
List resultList = new ArrayList();
resultList = getString(a, index, resultList); System.out.println(resultList);
System.out.println(resultList.size());
}
}感觉比较有意思,这种不定长的循环通常都要用递归来实现
先算结果一维数组的大小,
再遍历交错数组, 计算 每个元素应该加到结果一维数组的位置。 没有对比楼上的几个算法的效率,谁能给比较一下时间复杂度。private static int[] getDKJ(string[][] paramArray, int arrayindex)
{
int[] vrlen = new int[2]{1,1};
for (int i = 0; i < paramArray.Length; i++)
{
if(i<arrayindex)
{
vrlen[0] =vrlen[0] * paramArray[i].Length;
}
if(i>arrayindex)
{
vrlen[1] =vrlen[1] * paramArray[i].Length;
}
}
return vrlen;
}
static void ConvertJaggedArray(string[][] paramArray)
{
try
{
string[][] strJaggedArray = paramArray;
int vrlen = Index.getDKJ(paramArray,-1)[1]; string[] vr = new string[vrlen]; for (int i = 0; i < paramArray.Length; i++)
{
int[] tt = Index.getDKJ(paramArray, i);
int predkj = tt[0];
int postdkj = tt[1]; for (int j = 0; j < paramArray[i].Length; j++)
{
int chuxianJianGe = postdkj * (paramArray[i].Length);
int chuxianChangdu = postdkj; int preElementNumberInCurArray = j;
for (int k = 0; k < predkj; k++)
{
for (int m = 0; m < chuxianChangdu; m++)
{
int vrElementPos = k * chuxianJianGe + m + j*chuxianChangdu;
vr[vrElementPos] += "&" + paramArray[i][j];
}
}
}
} return;
}
catch (Exception ex)
{
return;
}
finally
{
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication54
{
public partial class Form1 : Form
{
string[] all=new string[255];
string test;
string[][] a = new string[4][];
int longa;
int num = 0;
public Form1()
{
InitializeComponent();
a[0] = new string[] { "cs", "app" };
a[1] = new string[] { "good", "cool", "dev" };
a[2] = new string[] { "king", "of", "the", "world" };
a[3] = new string[] { "a","b","c","d"}; longa = a.Length;
} private void button1_Click(object sender, EventArgs e)
{
coc( a[0],longa); }
public void coc(string[] s,int i)
{
foreach(string sa in s)
{ if (num == 0)
{
all[num] = sa;
}
else
{
all[num] = "&" + sa;
}
if (num + 1 != i)
{
num += 1;
coc(a[num], i);
}
else
{
//this.richTextBox1.AppendText(allstring+"\n");
for (int alllength = 0; alllength < all.Length; alllength++)
{
test+=all[alllength]; }
this.richTextBox1.AppendText(test+"\n");
test = "";
} for (int t = 0; t < i; t++)
{
if (a[t][a[t].Length - 1] == sa)
{
num -= 1;
}
}
}
}
}
}
见笑了。
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
public static string[] str;
public static int s = -1;
public static int max=0;
public static void check(int NewS)
{
if (NewS > s)
{
s = NewS;
str=new string[s];
}
}
static void Main(string[] args)
{
string[][] strJaggedArray = new string[3][];
strJaggedArray[0] = new string[] { "we", "are", "studnt" };
strJaggedArray[1] = new string[] { "say", "what" };
string[] index=new string[strJaggedArray.Length];
System.Collections.ArrayList list = new System.Collections.ArrayList();
//string[] ss=new string[];
for (int i=0; i < strJaggedArray.Length-1; i++)
{
check(strJaggedArray.Length*strJaggedArray[i].Length);
for (int j = i + 1; j < strJaggedArray.Length - 1; j++)
{
for (int k = 0; k <= strJaggedArray[j].Length - 1; k++)
{
for (int m = 0; m <= strJaggedArray[i].Length - 1; m++)
{
//Console.WriteLine(strJaggedArray[j][k]);
//Console.WriteLine(strJaggedArray[i][m]);
list.Add(strJaggedArray[j][k] + "&" + strJaggedArray[i][m]);
if (max < s)
max += 1;
str[max] = strJaggedArray[j][k] + "&" + strJaggedArray[i][m];
}
}
}
} foreach (string s in list)
{ Console.WriteLine(s);
}
Console.WriteLine(list.Count); foreach (string s in str)
{
Console.WriteLine(s);
}
}
}
}
String[] output;
input = new String[3][];
input[0] = new String[]{"1abc","1safd","1asfdsaf","1sfddsa"};
input[1] = new String[]{"2abc","2safd","2asfdsaf","2sfddsa"};
input[2] = new String[]{"3abc","3safd","3asfdsaf","3sfddsa"};
int titallen;
titallen = 1;
for (int i = 0; i < input.Length; i++)
{
titallen = titallen * input[i].Length;
}
output = new String[titallen];
int[] pos = new int[input.Length];
for (int i = 0; i < input.Length; i++)
{
pos[i] = 0;
}
for (int i = 0; i < titallen; i++)
{
output[i] = input[0][pos[0]];
for (int j = 1; j < input.Length; j++)
{
output[i] = output[i]+ "&"+ input[j][pos[j]];
}
pos[(input.Length - 1)]++;
for (int j = input.Length - 1; j >= 0; j--)
{
if (pos[j] >= input[j].Length)
{
pos[j] = 0;
if (j > 0)
{
pos[j - 1]++;
}
}
}
}
public class ExchangeArray { /**
* @Auth Red_angelX
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[][] strJaggedArray = new String[3][];
strJaggedArray[0] = new String[] {"cs","app"};
strJaggedArray[1] = new String[] {"good","cool","dev"};
strJaggedArray[2] = new String[] {"king","of","the","world"};
String[] strArray = Exchange(strJaggedArray);
for(int i=0; i< strArray.length ;i++)
{
System.out.println(strArray[i]);
}
}
/**
* 函数
* @param strJaggedArray
* @return
*/
public static String[] Exchange(String[][] strJaggedArray)
{
String[][] temp = DoExchange(strJaggedArray);
return temp[0];
}
/**
* 递归
* @param strJaggedArray
* @return
*/
private static String[][] DoExchange(String[][] strJaggedArray)
{
int len = strJaggedArray.length;
if(len >= 2)
{
int len1 = strJaggedArray[0].length;
int len2 = strJaggedArray[1].length;
int newlen = len1*len2;
String[] temp = new String[newlen];
int Index = 0;
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
temp[Index] = strJaggedArray[0][i] + "&"+strJaggedArray[1][j];
Index ++;
}
}
String[][] newArray = new String[len-1][];
for(int i=2;i<len;i++)
{
newArray[i-1] = strJaggedArray[i];
}
newArray[0] = temp;
return DoExchange(newArray);
}
else
{
return strJaggedArray;
}
}}
不明白为什么有人非要说用for循环还很简单
// 输入: strJaggedArray[][], 由多个一维数组(长度不定,个数不定)组成的交错数组
// 输出: strArray[], 由strJaggedArray[r][c]中的元素以"&"为分隔符拼合而成, 是strJaggedArray中数组元素的无重复组合(不考虑顺序) string[] Merge( string[][] str )
{
//第一维德长度
int len1 = str.Length;
//数组每一个二维的长,每个二维的索引初始化为0
int[] lens = new int[ len1];
int[] index = new int[ len1 ];
for( int len = 0; len < len1; len ++ )
{
lens[len] = str[len].Length;
index[len] = 0;
}
//储存目标字符串的链表
ArrayList al = new ArrayList( );
//添加目标字符串
while( index[0] < lens[0] )
{
string strTemp = "";
//连接字符串
for( int i = 0; i < len1; i ++ )
strTemp += ( "&" + str[i][ index[i]] );
//添加连接的字符串
al.Add( strTemp.Substring( 1 ) );
//重新设置索引数组的值,最后一个每次都要加1,如果到了这一维的长度,就清0,上一维也加1
bool bOver = true;
for( int j = len1 - 1; j >= 0; j -- )
{
if( bOver )
{
index[ j ] ++;
if( j < len1 - 1 )
index[j+1] = 0;
bOver = index[j] == lens[j] ? true: false;
continue;
} break;
}// end of for
}//end of while
return al.ToArray( typeof( string) ) as string[];
}//end of Merge
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace JaggedArray
{
class Program
{
static void Main(string[] args)
{
int row, i;
string[][] input;
string[] result;
row = Convert.ToInt16(Console.ReadLine());
if (row < 2)
{
Console.WriteLine("the number must be bigger than 1.");
}
input = new string[row][];
for (i = 0; i < row; i++)
{
input[i] = Console.ReadLine().Split(' ');
}
result = combination(input);
foreach (string r in result)
{
Console.Write(r + " ");
}
Console.ReadLine();
} static string[] combination(string[][] arr)
{
int i;
string[][] tmpArr;
string[] arrA, arrB;
ArrayList res = new ArrayList();
if (arr.Length > 2)
{
tmpArr = new string[arr.Length - 1][];
for (i = 1; i < arr.Length; i++)
{
tmpArr[i - 1] = arr[i];
}
arrA = arr[0];
arrB = combination(tmpArr);
}
else
{
arrA = arr[0];
arrB = arr[1];
}
foreach(string a in arrA)
foreach (string b in arrB)
{
res.Add(a + "&" + b);
}
return ((string[])res.ToArray(typeof(string)));
}
}
}用递归写的,可以动态输入资料,基本上是满足了题目的需求。
没什么好讲的,简单的dfs
_________________________________________________________
不是遍历问题,遍历的话每个节点只走一遍,有很多遗漏的
本质上就是用数组来模拟递归堆栈嘛. 觉得这道题邪门,说明基础不扎实.