//程序一定有些不好、不对的地方,还请大大们一定告诉我
//1.如何按随机顺序输出1到10这10个数,这是我一次面试时人家问的?
static void Main(string[] args)
{
int[] intArray = new int[10];
for (int i = 0; i < 10; i++)
{
intArray[i] = i + 1;
}
Random r = new Random();
for (int i = 9; i >= 0; i--)
{
int index = r.Next(i+1);
Console.WriteLine(intArray[index].ToString());
if (index != i)
{
intArray[index] = intArray[i];
}
}
}
//2.统计字符串中重复次数最多的字符
//方法一:
static void CountRepeat(string s)
{
List<char> list = new List<char>();
list.AddRange(s.ToCharArray());
list.Sort();
char c = list[0];
int temp = 1;
char maxChar = ' ';
int maxCount = 0;
for (int i = 1; i < list.Count; i++)
{
if (list[i] == c)
{
temp++;
}
if (list[i] != c || i == list.Count - 1)
{
if (temp > maxCount)
{
maxCount = temp;
maxChar = c;
}
temp = 1;
}
c = list[i];
}
Console.WriteLine(maxChar.ToString() + " " + maxCount.ToString());
}
//方法二:
static void CountRepeat(string s)
{
Dictionary<char, int> dict = new Dictionary<char, int>();
char maxChar = Char.MinValue;
int maxCount = 0;
foreach (char c in s)
{
if (dict.ContainsKey(c))
{
dict[c]++;
}
else
{
dict.Add(c, 1);
}
}
foreach (KeyValuePair<char, int> kvp in dict)
{
if (kvp.Value > maxCount)
{
maxCount = kvp.Value;
maxChar = kvp.Key;
}
}
Console.WriteLine(maxChar.ToString() + " " + maxCount.ToString());
}