一道C#基础题,看你能多长时间做出来?
这是一道java面试题,现用C#来实现。具体题目是这样的:给定6个数字1,2,2,3,4,5,要求找出所有这6个数字组合成的六位数,并满足
(1)3和5不能相连
(2)4不能在第三位
给出你的算法。
我是这样做的。
大家可以给出更好的算法啊,多提宝贵意见!
(1)3和5不能相连
(2)4不能在第三位
给出你的算法。
我是这样做的。
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//char[] a ={ '1','2','2','3','4','5'};
char[] a ={ '1','2','2','3','4','5'};
int i = 0;
IList result=Program.PaiLie(a);
IList result2 = new ArrayList();
//StreamWriter tw = new StreamWriter("c:\\out.txt");
//Console.SetOut(tw);
foreach (string str in result)
{
if (str.Contains("35") || str.Contains("53") || str.IndexOf('4') == 3)
{
i++;
}
else
result2.Add(str);
//Console.WriteLine(str);
}
foreach (string str2 in result2)
{
Console.WriteLine(str2);
}
//tw.WriteLine("i = " + i);
//tw.WriteLine("result:-- " + result.Count);
//tw.WriteLine("Total:-- "+result2.Count);
Console.WriteLine("i = " + i);
Console.WriteLine("result:-- " + result.Count);
Console.WriteLine("Total:-- "+result2.Count);
}
public static IList PaiLie(char[] a)
{
IList resultStr =new ArrayList();
//resultStr.Add(new string(a));
int n = a.Length;
if (n == 2)
{
resultStr.Add(new string(a));
char temp=a[0];
a[0] = a[1];
a[1] = temp;
resultStr.Add(new string(a));
}
if (n > 2)
{
for (int i = 0; i < n; i++)
{
string b = new string(a);
b=b.Remove(i, 1);
char[] c = b.ToCharArray();
IList tempStr = PaiLie(c);
foreach (string s in tempStr)
{
string xx = a[i].ToString()+s;
resultStr.Add(xx);
}
}
}
return resultStr;
}
}
}
去掉部分注释,可以导出到文件。using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//char[] a ={ '1','2','2','3','4','5'};
char[] a ={ '1','2','2','3','4','5'};
int i = 0;
IList result=Program.PaiLie(a);
IList result2 = new ArrayList();
//StreamWriter tw = new StreamWriter("c:\\out.txt");
//Console.SetOut(tw);
foreach (string str in result)
{
if (str.Contains("35") || str.Contains("53") || str.IndexOf('4') == 3)
{
i++;
}
else
result2.Add(str);
//Console.WriteLine(str);
}
foreach (string str2 in result2)
{
Console.WriteLine(str2);
}
//tw.WriteLine("i = " + i);
//tw.WriteLine("result:-- " + result.Count);
//tw.WriteLine("Total:-- "+result2.Count);
Console.WriteLine("i = " + i);
Console.WriteLine("result:-- " + result.Count);
Console.WriteLine("Total:-- "+result2.Count);
}
public static IList PaiLie(char[] a)
{
IList resultStr =new ArrayList();
//resultStr.Add(new string(a));
int n = a.Length;
if (n == 2)
{
resultStr.Add(new string(a));
char temp=a[0];
a[0] = a[1];
a[1] = temp;
resultStr.Add(new string(a));
}
if (n > 2)
{
for (int i = 0; i < n; i++)
{
string b = new string(a);
b=b.Remove(i, 1);
char[] c = b.ToCharArray();
IList tempStr = PaiLie(c);
foreach (string s in tempStr)
{
string xx = a[i].ToString()+s;
resultStr.Add(xx);
}
}
}
return resultStr;
}
}
}
大家可以给出更好的算法啊,多提宝贵意见!
作者:Jackhuclan
出处:http://jackhuclan.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://jackhuclan.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。