1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| using System; using System.Collections.Generic; using System.Text; using System.Linq; using System.Collections;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string str = "aaeaabbebbccececddeddd";
List<char> charList = new List<char>(str.ToCharArray()); int len = charList.Count;
Dictionary<char, int> charD = new Dictionary<char, int>();
int lastTime = 0;
while (charList.Count != 0) { int count = CountAChar(charList);
if (count > lastTime) { charD.Clear(); charD.Add(charList[0], count); lastTime = count; }
else if (count == lastTime) { charD.Add(charList[0], count); lastTime = count; }
DeleteAChar(count, charList); }
foreach (KeyValuePair<char, int> kv in charD) { Console.WriteLine("{1}有{0}个", kv.Value.ToString(), kv.Key.ToString()); }
Console.WriteLine("----------------------------------------"); SecondProgram.SecondWay(str); Console.WriteLine("----------------------------------------"); ThirdClass.ThirdWay(str); Console.Read(); }
static void DeleteAChar(int num, List<char> cl) { char c = cl[0];
for (int i = 0; i < num; i++) { cl.Remove(c); } }
static int CountAChar(List<char> cl) { int ret = 0;
foreach (char c in cl) { if (c == cl[0]) { ret++; } }
return ret; } }
// class SecondProgram { public static void SecondWay(string str) { int len = str.Length; char[] strArray = str.ToCharArray(); int[] count = new int[len]; for (int y = 0; y < len; y++) //将count数组的每个单元初始为0 count[y] = 0;
for (int y = 0; y < len; y++) //将当前字符与之后的字符进行比较,相同count数组中对应索引处++ { for (int yy = y; yy < len; yy++) { if (strArray[y].Equals(strArray[yy])) { count[y]++; } } }
ArrayList maxCountArr = new ArrayList(); int maxCount = count[0];
for (int y = 0; y < len; y++) //获得出现次数的最大值 { if (count[y] > maxCount) { maxCount = count[y]; } }
for (int y = 0; y < len; y++) //最大值与count数组中的每个数进行比较,相同则出现次数相同,把索引加入到maxCountArr { if (count[y] == maxCount) { maxCountArr.Add(y); } }
for (int y = 0; y < maxCountArr.Count; y++) //输出 { Console.Write("Str" + (y + 1) + ":" + strArray[(int) maxCountArr[y]] + "/n"); } } }
/// <summary> /// 第三种方式 /// </summary> class ThirdClass { /// <summary> /// 还是LinQ最简便 /// </summary> /// <param name="str"></param> public static void ThirdWay(string str) { var resultGroup = from aChar in str.ToCharArray() group aChar by aChar;
int max = 0;
foreach (var one in resultGroup) { if (one.Count() > 0) { max = one.Count(); } }
foreach (var one in resultGroup) { if (one.Count() == max) { Console.WriteLine("{0}字符出现了{1}次", one.Key, max); } } } } }
|