✋编程题
输⼊n(n<100)个数,找出其中最⼩的数,将它与最前⾯的数交换后输出这些数。
c# //35 33 45 56 2 131 435
string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for(int i = 0; i < strArray.Length; i++)
{
int number = Convert.ToInt32(strArray[i]);
intArray[i] = number;
} //得到一个有序的数组
int min = intArray[0];//4
int minIndex = 0;
for(int i = 1; i < intArray.Length; i++)
{
if (intArray[i] < min)
{
min = intArray[i];
minIndex = i;
}
}
int temp = intArray[0];
intArray[0] = intArray[minIndex];
intArray[minIndex] = temp;
foreach(int t in intArray)
{
Console.Write(t + " ");
}
插入X =6 →→→→ 2 35 33 45 56 6 131 435
✋编程题
有n(n<=100)个整数,已经按照从⼩到⼤顺序排列好,现在另外给⼀个整数x,请将该数插⼊到序列中,并使新的序列仍然有序。
3 5 34 56 123 456
插入x = 10
输出新的序列
3 5 10 34 56 123 456
c# string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
int number = Convert.ToInt32(strArray[i]);
intArray[i] = number;
} //得到一个有序的数组
int x = Convert.ToInt32(Console.ReadLine());
int m = intArray.Length - 1;
for (int i = 0; i < intArray.Length-1; i++)
{
if(x>=intArray[i] && x <= intArray[i + 1])
{
m = i;
break;
}
}
if (x < intArray[0])
{
m = -1;
}
//创建一个新的数组
int[] intArrayNew = new int[intArray.Length + 1];
//0-m = 0~-1
for (int i = 0; i < m+1; i++)
{
intArrayNew[i] = intArray[i];
}
intArrayNew[m + 1] = x; //插入X值
//m+1~length-1 的数移动到新数组
for(int i = m + 1; i < intArray.Length; i++)
{
intArrayNew[i+1] = intArray[i];
}
foreach(int temp in intArrayNew)
{
Console.Write(temp + " ");
}
✋编程题
输⼊⼀个字符串,判断其是否是C#的合法标识符。
c# string str = Console.ReadLine();
// 判断 数字 字母 _
// 数字开头
bool isRight = true;
for(int i = 0; i < str.Length; i++)
{
if( (str[i]<'0' || str[i]>'9') && //不是数字
(str[i] < 'a' || str[i] > 'z') && //不是小写字母
(str[i] < 'A' || str[i] > 'Z') && //不是大写字母
(str[i] != '_')) //不是下划线
{
isRight = false;
break;
}
}
if(str[0] >= '0' && str[0] <= '9') //不是数字开头
{
isRight = false;
}
if (isRight)
{
Console.WriteLine("是合法标识符");
}
else
{
Console.WriteLine("不是合法标识符");
}
✋编程题
“回⽂串”是⼀个正读和反读都⼀样的字符串,⽐如“level”或者“noon”等等就是回⽂串。请写⼀个程序判断读⼊的字符串是否是“回⽂”。
c# string str = Console.ReadLine();
// 8 8/2=4 0-3
// 7 7/2=3 0-2
// 7
// 0 - 6 1-5 2-4 i ~ length-1-i
bool isHui = true;
for(int i = 0; i < str.Length / 2; i++)
{
// i length-1-i
if(str[i]!=str[str.Length - 1 - i])
{
isHui = false;
break;
}
}
if (isHui)
{
Console.WriteLine("是回文串");
}
else
{
Console.WriteLine("不是回文串");
}
✋编程题
最近夏⽇炎热,令张三⾮常的不爽。最近张三开始研究天⽓的变化。历经千⾟万苦,他收集了连续N(1<N<1000000)天的最⾼⽓温数据。现在他想知道⽓温⼀直上升的最⻓连续天数。
样例输⼊:
1 3 5 2 3 5 7 8 6 9
样例输出
5
c# string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
int number = Convert.ToInt32(strArray[i]);
intArray[i] = number;
} //获得一组数字
int maxDays = 1;//记录最高 气温连续升高的天数
int count = 1;//记录气温连续升高的天数
for(int i = 0; i < intArray.Length - 1; i++)
{
if (intArray[i] < intArray[i + 1])
{
count++;
}
else
{
if (count > maxDays)
{
maxDays = count;
}
count = 1;
}
}
if (count > maxDays)
{
maxDays = count;
}
Console.WriteLine("气温连续升高的最长天数" + maxDays);
✋编程题
输⼊是个不相等的正整数,输出这10个正整数中的第⼆⼤的数。
样例输⼊
3 5 7 2 9 5 3 10 3 8
样例输出
9
c# string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
int number = Convert.ToInt32(strArray[i]);
intArray[i] = number;
} //获得一组数字
int max1 = 0;int max2 = 0;
for(int i = 0; i < intArray.Length; i++)
{
if (intArray[i] > max1)
{
max2 = max1;
max1 = intArray[i];
}
else
{
if (intArray[i] > max2)
{
max2 = intArray[i];
}
}
}
Console.WriteLine("第一大值是" + max1 + " 第二大值是" + max2);
✋编程题
描述给定⼀个只包含⼩写字⺟的字符串,请你找出第⼀个仅出现⼀次的字符。如果没有,输出
no。
c#方法1:
string str = Console.ReadLine();
char temp = ' ';// 20x20
for(int i = 0; i < str.Length; i++)
{
int count = 0;
for(int j = 0; j < str.Length; j++)
{
if (str[j] == str[i])
{
count++;
if (count == 2)
{
break;
}
}
}
if (count==1)
{
temp = str[i];
break;
}
}
if(temp==' ')
{
Console.WriteLine("no");
}
else
{
Console.WriteLine(temp);
}
c#方法2:
string str = Console.ReadLine();
int[] countArray = new int[26];//计数,保存每个字符出现的个数
for(int i = 0; i < str.Length; i++)
{
countArray[str[i] - 'a'] = countArray[str[i] - 'a'] + 1;
}
bool isFind = false; //是否找到
char c = ' ';
for(int i = 0; i < str.Length; i++)
{
if(countArray[str[i] - 'a'] == 1)
{
isFind = true;
c = str[i];
break;
}
}
if (isFind == false)
{
Console.WriteLine("no");
}
else
{
Console.WriteLine(c);
}
✋编程题
⼤⼩写字⺟互换
把⼀个字符串中所有出现的⼤写字⺟都替换成⼩写字⺟,同时把⼩写字⺟替换成⼤写字⺟。
c# string str = Console.ReadLine();
//a-z A-Z
int cha = 'A' - 'a';
for(int i = 0; i < str.Length; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
char c = (char)(str[i] + cha);
Console.Write(c);
}else if (str[i] >= 'A' && str[i] <= 'Z')
{
char c = (char)(str[i] - cha);
Console.Write(c);
}
else
{
Console.Write(str[i]);
}
}
✋在⼀个数组中查找⼀个给定的值,输出第⼀次出现的位置(从1开始)
输⼊第⼀⾏是数组中的元素
第⼆⾏是要查找的数
输出输出第⼀次出现的位置
输⼊
3 8 23 45 2 5
23
输出3
c# string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
int n = Convert.ToInt32(strArray[i]);
intArray[i] = n;
} //得到一组数字
int number = Convert.ToInt32(Console.ReadLine());
int location = -1;
for(int i = 0; i < intArray.Length; i++)
{
if (number == intArray[i])
{
location = i + 1;
break;
}
}
Console.WriteLine(location);