找出最大的数并统计个数

想要在Output窗口写入文本只需把ConsoleWriteLine() 用
       Debug.WriteLine()
       Trace.WriteLine()
代替就可以.
但是在Release版本中,Debug()会消失

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Csharp
{
        class Program
        {
                static int Maxima(int[] inputArray, out int[] maxValueArray)
                {
                        Console.WriteLine("Start search the maximum value");
                        int count = 1;
                        int maxValue = inputArray[0];
                        maxValueArray = new int[1];
                        Debug.WriteLine(string.Format(
                                        "Maximum value has initialize to {0}, as index 0", maxValue));

                        for (int i = 0; i < inputArray.Length; ++i)
                        {
                                Debug.WriteLine(string.Format(
                                        "Now looking at element at index {0}", i));
                                if (maxValue < inputArray[i])
                                {
                                        maxValue = inputArray[i];
                                        count = 1;
                                        maxValueArray = new int[1];
                                        maxValueArray[0] = i;
                                        Debug.WriteLine(string.Format(
                                        "New maxValue has found: {0}, at index{1}", maxValue, i));
                                }
                                else
                                {
                                        if (maxValue == inputArray[i])
                                        {
                                                count++;
                                                int[] oldMaxValueArray = maxValueArray;
                                                maxValueArray = new int[count];
                                                oldMaxValueArray.CopyTo(maxValueArray, 0);
                                                maxValueArray[count - 1] = i;
                                                Debug.WriteLine(string.Format(
                                                        "Duplicate maximum found at element index {0}", i));
                                        }
                                }
                        }
                        Trace.WriteLine(string.Format(
                                "Maximum value {0} found with {1}", maxValue, count));
                        Debug.WriteLine(string.Format(
                                "maxValue found has complete"));
                        return maxValue;
                }
                static void Main(string[] args)
                {
                        int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
                        int[] maxValueIndices;
                        int maxValue = Maxima(testArray, out maxValueIndices);
                        Console.WriteLine("Maximum  value {0} has found in element indices", maxValue);

                        foreach (int index in maxValueIndices)
                                Console.WriteLine(index);                }
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值