1.最近张三开始研究天⽓的变化。历经千⾟万苦,他收集了连续N(1<N<1000000)天的最⾼⽓温数据。现在他想知道⽓温⼀直上升的最⻓连续天数。
样例输⼊:1 3 5 2 3 5 7 8 6 9
样例输出:5
string sum = "1 3 7 2 3 5 7 8 6 9";
int[] temperatures = Array.ConvertAll(sum.Split(' '), int.Parse);
int longestStreak = 1; // 当前最长连续上升天数,默认为1 2 3 4 5
int currentStreak = 1; // 当前连续上升天数,默认为1 2 3 1 2 3 4 5
for (int i = 1; i < temperatures.Length; i++)
{
if (temperatures[i] > temperatures[i - 1])
{
currentStreak++;// 如果温度上升,当前连续上升天数加1
}
else
{
currentStreak = 1;
}
// 更新最长连续上升天数
if (currentStreak > longestStreak) longestStreak = currentStreak;
}
Console.WriteLine(longestStreak); // 输出最长连续上升天数
2.描述给定⼀个只包含⼩写字⺟的字符串,请你找出第⼀个仅出现⼀次的字符。如果没有,输no。
string input = "abaccabad";
char result = FindFirstUniqueCharacter(input);
if (result == '\0')
{
Console.WriteLine("no");
}
else
{
Console.WriteLine(result);
}
public static char FindFirstUniqueCharacter(string str)
{
int[] charCount = new int[26];
foreach (char c in str)
{
int index = c - 'a';
charCount[index]++;
}
foreach (char c in str)
{
int index = c - 'a';
if (charCount[index] == 1)
{
return c;
}
}
return '\0';
}
3.有n(n<=100)个整数,已经按照从⼩到⼤顺序排列好,现在另外给⼀个整数x,请将该数插⼊到
序列中,并使新的序列仍然有序。
输出新的序列
Console.WriteLine("请输入数组的长度");
int n = int.Parse(Console.ReadLine());
int[] sequence = new int[n];
for (int i = 0; i < n; i++) sequence[i] = int.Parse(Console.ReadLine());
Console.WriteLine("请输入插入数组里的一个数字");
int x = int.Parse(Console.ReadLine());
int[] newSequence = new int[n + 1];
int insertIndex = 0;
bool xInserted = false;
for (int i = 0; i < n; i++)
{
if (!xInserted && sequence[i] > x)
{
newSequence[insertIndex++] = x;
xInserted = true;
}
newSequence[insertIndex++] = sequence[i];
}
if (!xInserted) newSequence[insertIndex] = x;
for (int i = 0; i < n + 1; i++) Console.WriteLine(newSequence[i]);