回顾
编写-一个应用程序用来输入的字符串进行加密,对于字母字符串加密规则如下:
'a'→'d' 'b'→'e' 'W'→'Z .... 'x'→'a"y'→b' 'Z→'c"A'→'D'B'→'E'
'W'>Z' .... X→'A''Y'→'B' Z'→'C?对于其他字符,不进行加密。
a到z所有字符要+3,x,y,z -23,A到W所有字符+3 XYZ -23
先有一个字符数组
char[] charArray = Console.ReadLine().ToCharArray();
遍历字符数组
for(int i = 0
{
if ('A' <=charArray[i]&&charArray[i] <='Z'|| 'a' < charArray[i] && charArray[i] < 'z')
{
if ('W' < charArray[i] && charArray[i]<='Z'|| 'w' < charArray[i])
{
charArray[i] = (char)(charArray[i] - 23)
}
else
{
charArray[i] = (char)(charArray[i] + 3)
}
}
}
Console.WriteLine(charArray)

今天
冒泡排序
总比对的轮数等于长度-1 比对的最大下标等于长度-1-上一轮数-1
循环数组 判断相邻两个数之间的大小
方法:
int[] num = { 2, 5, 3, 7, 4, 17, 1 }
Array.Sort(num)
Console.WriteLine(num)
string str = Console.ReadLine()
string[] strArray = str.Split(" ")
int[] intArray = new int[strArray.Length]
for (int i = 0
{
intArray[i] = Convert.ToInt32(strArray[i])
}
for (int i = 0
{
//比对两个相邻的数
for (int j = 0
{
if (intArray[j] > intArray[j + 1])//如果前一个比后一个大那么就交换值
{
int temp = intArray[j]
intArray[j] = intArray[j + 1]
intArray[j + 1] = temp
}
}
}
Console.WriteLine(String.Join(" ", intArray))

例 有n(n<=1001个整数,已经按照从小到大顺序排列好,现在另外给一个整数x, 请将该数插入到序列中,并使新的序列仍然有序。
string str = Console.ReadLine()
string[] strArray = str.Split(" ")
int[] intArray = new int[strArray.Length]
for (int i = 0
{
intArray[i] = Convert.ToInt32(strArray[i])
}
Array.Sort(intArray)
先确定好要插入的下标位置
先输入一个数
int m = Convert.ToInt32(Console.ReadLine())
定义一个变量存下标位置
int numIndex = 0
定义一个新数组,长度加一
int[] numArray = new int[intArray.Length + 1]
先确定好要插入的下标位置
for (int i = 1
{
if (intArray[i] <= m&&m<= intArray[i+1])
{
numIndex = i + 1
break
}
}
循环结束有两种情况出现就是插入到下标位置前,插入到最后一个下标
if (intArray[intArray.Length - 1] < m)
{
numIndex = intArray.Length
}
给数组赋值
for (int i = 0
{
numArray[i] = intArray[i]
}
numArray[numIndex] = m
for (int i = numIndex + 1
{
numArray[i] = intArray[i - 1]
}
Console.WriteLine(String.Join(" ", numArray))

例 最近转秋,气温逐渐下降,小明决定研究天气的变化,他收集了连续N(1<N<100000)天的最低气温数据,现在想知道气温一直下降的最长连续天数
样例输入:
36 32 30 33 28 23 22 30 32
样例输出:
4
先创建一个字符串存输入进来的数
string str = Console.ReadLine()
string[] strArray = str.Split(" ")
int[] temp = new int[strArray.Length]
for (int i = 0
{
temp[i] = Convert.ToInt32(strArray[i])
}
创建一个变量记录每次降温的天数,降一次温加一
int count = 1
创建一个变量记录最大连续降温的天数的值
int maxCount = 1
遍历气温数组
for(int i = 1
{
if (temp[i + 1] < temp[i])
{
count++
}
else//43 32 23 44 43 42 32
{
//记录一下是否是最大值
if (maxCount < count) maxCount = count
count = 1
}
}
/*if (maxCount < count) maxCount = count
maxCount = maxCount < count ? count : maxCount
Console.WriteLine("最大的连续气温天数是{0}", maxCount)
