也许你是一个老实的人,没有谈过朋友,也许你是一个花花公子,追过很多女孩子,也被许多女孩子追过,总之你的一生就像是一个很长很长的字符串,每一个字符代表在你的人生中出现过的女孩子,到你快要离开人世的时候,你在回想,每个女孩你追过多少次呢,现在就帮你解答:
一:正则求解
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Console.WriteLine("Please input a string");
string appearWord="";
string test = Console.ReadLine();
char[] c = test.ToCharArray();
foreach (char cc in c)
{
if(!list.Contains(cc.ToString()))
{
list.Add(cc.ToString());
}
}
list.Sort();
for(int i=0;i<list.Count;i++)
{
appearWord+=list[i].ToString();
}
Console.WriteLine("You input a string just:{0}", test);
Console.WriteLine("你刚输入的字符串中一共有{0},字符出现,它们是:{1}", list.Count, appearWord);
for (int j = 0; j < list.Count; j++)
{
Console.WriteLine("{0}出现的次数是:{1}", list[j].ToString(), returnAppareNum(test, list[j].ToString()));
}
Console.Read();
}
//一个字符或者字符串在另一个字符串出现的次数
static int returnAppareNum(string source, string patten)
{
MatchCollection mc = Regex.Matches(source, patten);
return mc.Count;
}
{
ArrayList list = new ArrayList();
Console.WriteLine("Please input a string");
string appearWord="";
string test = Console.ReadLine();
char[] c = test.ToCharArray();
foreach (char cc in c)
{
if(!list.Contains(cc.ToString()))
{
list.Add(cc.ToString());
}
}
list.Sort();
for(int i=0;i<list.Count;i++)
{
appearWord+=list[i].ToString();
}
Console.WriteLine("You input a string just:{0}", test);
Console.WriteLine("你刚输入的字符串中一共有{0},字符出现,它们是:{1}", list.Count, appearWord);
for (int j = 0; j < list.Count; j++)
{
Console.WriteLine("{0}出现的次数是:{1}", list[j].ToString(), returnAppareNum(test, list[j].ToString()));
}
Console.Read();
}
//一个字符或者字符串在另一个字符串出现的次数
static int returnAppareNum(string source, string patten)
{
MatchCollection mc = Regex.Matches(source, patten);
return mc.Count;
}
二:递归求解
static void Main(string[] args)
{
string test = "ubaadfasd";
abc(test);
Console.Read();
}
public static void abc(string source)
{
string aaa = "";
if (source.Length > 0)
{
string a = source.Substring(0, 1);
Console.WriteLine("{0}出现{1}次",a,ab(source, a));
aaa=source.Replace(a, "");
abc(aaa);
}
}
public static int ab(string dd, string aad)
{
int num = 0;
char[] c = dd.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i].ToString().Equals((object)aad))
{
num += 1;
}
}
return num;
}
{
string test = "ubaadfasd";
abc(test);
Console.Read();
}
public static void abc(string source)
{
string aaa = "";
if (source.Length > 0)
{
string a = source.Substring(0, 1);
Console.WriteLine("{0}出现{1}次",a,ab(source, a));
aaa=source.Replace(a, "");
abc(aaa);
}
}
public static int ab(string dd, string aad)
{
int num = 0;
char[] c = dd.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i].ToString().Equals((object)aad))
{
num += 1;
}
}
return num;
}