C#l练习数组的比较
2018-03-02 08:07
260 查看
用方法来实现:①有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法码实现;
代码实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。
namespace ConsoleApplication8
{
class Fa
{
public static int max(int []b)//形参传输数组
{
int c = -111;
for (int i = 0; i < b.Length; i++)//获得数组长度,并进行比较
{
if (b[i] > c)
c = b[i];
}
return c;
}
}
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
Console.WriteLine("最大值为{0}", Fa.max(a));
Console.ReadKey();//外部调用方法
}
}
}实现结果:
还可以使用另一种比较简单的方法,就是使用C#Array.Sort,升序排序
代码实现:
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
Array.Sort(a);//排序
Console.WriteLine(" 最大数为 {0}", a[a.Length - 1]);//最后一个数为最大值,默认为升序
Console.ReadKey();
}
}②有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
代码实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。
namespace ConsoleApplication8
{
class Program
{
class Fa
{
public static void string1(string[] a1)
{
int c = 0;
for (int i = 1; i < a1.Length; i++)//数组的个数
{
if (a1[i].Length > a1[i - 1].Length)//每个字符串的长度
c = i;
}
Console.WriteLine("{0}", a1[c]);//输出字符串长度最长的
}
}
static void Main(string[] args)
{
string[] a = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
Fa.string1(a);
Console.ReadKey();
}
}
}实现结果:
代码实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。
namespace ConsoleApplication8
{
class Fa
{
public static int max(int []b)//形参传输数组
{
int c = -111;
for (int i = 0; i < b.Length; i++)//获得数组长度,并进行比较
{
if (b[i] > c)
c = b[i];
}
return c;
}
}
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
Console.WriteLine("最大值为{0}", Fa.max(a));
Console.ReadKey();//外部调用方法
}
}
}实现结果:
还可以使用另一种比较简单的方法,就是使用C#Array.Sort,升序排序
代码实现:
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
Array.Sort(a);//排序
Console.WriteLine(" 最大数为 {0}", a[a.Length - 1]);//最后一个数为最大值,默认为升序
Console.ReadKey();
}
}②有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
代码实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。
namespace ConsoleApplication8
{
class Program
{
class Fa
{
public static void string1(string[] a1)
{
int c = 0;
for (int i = 1; i < a1.Length; i++)//数组的个数
{
if (a1[i].Length > a1[i - 1].Length)//每个字符串的长度
c = i;
}
Console.WriteLine("{0}", a1[c]);//输出字符串长度最长的
}
}
static void Main(string[] args)
{
string[] a = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
Fa.string1(a);
Console.ReadKey();
}
}
}实现结果:
相关文章推荐
- CTF/CTF练习平台-各种绕过哟【sha1数组类型比较漏洞】
- 算法题练习(二)------哈夫曼树,比较奇数与偶数个数,查找一个数组的第K小的数
- 练习:有一个有序的数组,想要将一个元素插入到该数组中,还要保证该数组是有序的。如何获取该元素在数组中的位置.(2012.2.17)
- 排序算法性能比较练习——《数据结构、算法与应用》
- JS---------->数组练习!
- 索引器与数组的比较
- iOS中数组遍历的方法及比较
- 数组练习2
- Javascript比较两个数组是否相等(存在重复元素)
- 课堂练习---求数组中子数组和的最大值
- 数组查找数的比较次数
- java数组专项练习
- 数组排序方法的性能比较(1):注意事项及试验
- c# 两个数组比较,将重复部分去掉,返回不重复部分的实现
- 如何比较2个数组的按键值是否相等?
- [睡前灵感and发散思维]由一个简单的数组比较问题而想到的
- 顺序数组的练习
- 效率比较--数组
- 关于比较两个字节数组是否内容相同
- 面向对象程序设计上机练习八(对象数组)