从一个整数数组中取出最大的整数
int[] nums = { 1, 3, 5, 2, 7, 4 };
int max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] > max)
{
max = nums[i];
}
max = nums[i + 1];
max = max > nums[i] ? max : nums[i];
}
Console.WriteLine(max);
计算一个整数数组的所有元素的和
int[] nums = { 1,3,5,2,7,4};
int he=0;
for (int i = 0; i < nums.Length; i++)
{
he =he+ nums[i];
}
Console.WriteLine(he);
1将一个字符串数组输出为|分割的形式
int[] a = { 1, 2, 3, 4, 5 };
string b = " ";
for (int i = 0; i < a.Length; i++)
{
b += a[i] + "|";
}
Console.WriteLine(b);
2.将两个Int类型数组连接为一个string类型数组
int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 5, 6, 7, 8, 9};
string[] c = new string[10];
for (int i = 0; i <a.Length; i++)
{
string abc=a[i].ToString() ;
string def = b[i].ToString();
Console.Write("{0}"+" "+"{1}"+" ",a[i],b[i]);
Console.Write(abc);
Console.Write(def);
c[i] = abc + def;
Console.Write(c[i]);
}
Console.WriteLine(a);
3.有一个整数数组,请声明一个字符串数组,将整数数组中的每一个元素的值转换为字符串保存到字符串数组中
int[] a = { 1, 2, 3, 4, 5 };
string[] b = new string[5];
for (int i = 0; i < a.Length; i++)
{
b[i] = a[i].ToString();
Console.Write(b[i]);
4.将一个字符串数组的元素的顺序进行反转
int[] a = { 1, 2, 3, 4, 5 };
for (int i = a.Length-1; i >= 0; i--)
{
Console.Write(a[i]);
}
string[] a = { "a","b","c","d","e","d","f" };
string temp = string.Empty;
for (int i = 0; i < a.Length / 2; i++)
{
temp = a[i];
a[i] = a[a.Length - 1 - i];
a[a.Length - 1 - i] = temp;
}
foreach (string item in a)
{
Console.Write(item + " ");
}
5.将一组整数按从大到小排序
int[] a = { 7, 4, 8, 3, 5 };
int max;
for (int i = 0; i < a.Length-1; i++)
{
for (int j = 0; j < a.Length-1; j++)
{
if (a[j] < a[j + 1])
{
max = a[j];
a[j] = a[j + 1];
a[j + 1] = max;
}
}
}
foreach (int item in a)
{
Console.Write(item);
}
6.将一个int数组向左平移一次
int[] a = { 1, 2, 3, 4, 5 };
int left = 0;
for (int i = 0; i < a.Length-1; i++)
{
left = a[i];
a[i] = a[i + 1];
a[i+1] = left;
}
foreach (int item in a)
{
Console.Write(item);
}