110,545
社区成员
发帖
与我相关
我的任务
分享
using System;
namespace CsdnTest
{
class Program
{
static char[] Items;
static int TLength = 6;
static void Main(string[] args)
{
Items = "01723240".ToCharArray();
Array.Sort(Items);
PrintOut(0, TLength);
Console.ReadKey();
}
private static void PrintOut(int currentIndex, int length)
{
if (length == 0)
{
Console.WriteLine(new string(Items, 0, TLength));
return;
}
char currentValue = (char)0;
for (int i = currentIndex; i < Items.Length; i++)
{
if (currentValue != Items[i])
{
SwapItem(currentIndex, i);
PrintOut(currentIndex + 1, length - 1);
SwapItem(i, currentIndex);
currentValue = Items[i];
}
}
}
private static void SwapItem(int indexA, int indexB)
{
int m = indexA > indexB ? 1 : -1;
char value = Items[indexB];
while (indexA != indexB)
{
Items[indexB] = Items[indexB + m];
indexB += m;
}
Items[indexA] = value;
}
}
}
public bool getAllString(string inputString, int len)
{
if (len > inputString.Length) { return false; }
try
{
Char[] chr = inputString.ToCharArray();
File.WriteAllText(@"C:\file0.txt", "", Encoding.Default);
for (int i = 0; i < len; i++) // 处理 i个字符长度的
{
StreamReader sr = new StreamReader(@"C:\file" + i + ".txt", Encoding.Default);
StreamWriter sw = new StreamWriter(@"C:\file" + (i + 1) + ".txt", false, Encoding.Default);
string str = "";
while (true)
{
str = sr.ReadLine();
if (str == null && i != 0) { break; }
if (i == 0) { str = ""; }
for (int j = 0; j < chr.Length; j++)
{
// 保证0和2出现两次,其他数字只出现1次
Regex reg = new Regex(chr[j].ToString(), RegexOptions.Singleline);
switch (chr[j])
{
case '0':
case '2':
if (reg.Matches(str).Count <=1)
{
sw.WriteLine(str + chr[j].ToString());
}
break;
default:
if (reg.Matches(str).Count <=0)
{
sw.WriteLine(str + chr[j].ToString());
}
break;
}
}
if (i == 0) { break; }
}
sr.Close();
sw.Close();
}
File.Delete(@"C:\file0.txt");
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; }
return true;
}
// 测试用例
private void button1_Click(object sender, EventArgs e)
{
getAllString("01723240", 6);
}
// 因结果集较大,写到了文件中.
private void button1_Click(object sender, EventArgs e)
{
Char[] chr = "01723240".ToCharArray();
File.WriteAllText(@"C:\file0.txt", "", Encoding.Default);
for (int i = 0; i < chr.Length; i++) // 处理 i个字符长度的
{
StreamReader sr = new StreamReader(@"C:\file" + i + ".txt", Encoding.Default);
StreamWriter sw = new StreamWriter(@"C:\file" + (i + 1) + ".txt", false, Encoding.Default);
string str = "";
while (true)
{
str = sr.ReadLine();
if (str == null && i != 0) { break; }
if (i == 0) { str = ""; }
for (int j = 0; j < chr.Length; j++)
{
sw.WriteLine(str + chr[j].ToString());
}
if (i == 0) { break; }
}
sr.Close();
sw.Close();
}
if (i == 6) break; //计算到第6位
}