问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Diagnostics;namespacech07ex01{classProgram{staticvoidMain(string[]args){int[]testArray={4,7,4,2,7,3,7,8,3,9,1,9};int[]maxValIndices;intmaxVal=Maxima(testArray,outmaxValIndices);Console.WriteLine("maximumvalue{0}foundatelementindices:");foreach(intindexinmaxValIndices){Console.WriteLine(index);}Console.ReadKey();}staticintMaxima(int[]integers,outint[]indices){Debug.WriteLine("Maximumvaluesearchstartde.");indices=newint[1];intmaxVal=integers[0];indices[0]=0;intcount=1;Debug.WriteLine(string.Format("Maximumvalueinitializedto{0},atelementindex0.",maxVal));for(inti=1;i<integers.Length;i++){Debug.WriteLine(string.Format("nowlookingatelementatindex{0}.",i));if(integers[i]>maxVal){maxVal=integers[i];count=1;indices=newint[1];indices[0]=i;Debug.WriteLine(string.Format("newmaximumfound.newvalueis{0},atelementindex{1}.",maxVal,i));}else{if(integers[i]==maxVal){count++;int[]oldIndices=indices;indices=newint[count];oldIndices.CopyTo(indices,0);indices[count-1]=i;Debug.WriteLine(string.Format("duplicatemaximumfoundatelementindex{0}.",i));}}}Trace.WriteLine(string.Format("maximumvalue{0}found,with{1}"occurrences.",maxVal,count));Debug.WriteLine("maximumvaluesearchcompieted.");returnmaxVal;}}}
解决方案
解决方案二:
copy到我的vs中,这里错了Trace.WriteLine(string.Format("maximumvalue{0}found,with{1}"occurrences.",maxVal,count));Debug.WriteLine("maximumvaluesearchcompieted.");returnmaxVal;改成:Trace.WriteLine(string.Format("maximumvalue{0}found,with{1}occurrences.maxVal",count));Debug.WriteLine("maximumvaluesearchcompieted.");可以通过编译。
解决方案三:
1楼,手把手,呵呵
解决方案四:
voidMain(){int[]testArray={4,7,4,2,7,3,7,8,3,9,1,9};int[]maxValIndices;intmaxVal=Maxima(testArray,outmaxValIndices);Console.WriteLine("maximumvalue{0}foundatelementindices:");foreach(intindexinmaxValIndices){Console.WriteLine(index);}//Console.ReadKey();}staticintMaxima(int[]integers,outint[]indices){Debug.WriteLine("Maximumvaluesearchstartde.");indices=newint[1];intmaxVal=integers[0];indices[0]=0;intcount=1;Debug.WriteLine(string.Format("Maximumvalueinitializedto{0},atelementindex0.",maxVal));for(inti=1;i<integers.Length;i++){Debug.WriteLine(string.Format("nowlookingatelementatindex{0}.",i));if(integers[i]>maxVal){maxVal=integers[i];count=1;indices=newint[1];indices[0]=i;Debug.WriteLine(string.Format("newmaximumfound.newvalueis{0},atelementindex{1}.",maxVal,i));}else{if(integers[i]==maxVal){count++;int[]oldIndices=indices;indices=newint[count];oldIndices.CopyTo(indices,0);indices[count-1]=i;Debug.WriteLine(string.Format("duplicatemaximumfoundatelementindex{0}.",i));}}}//Trace.WriteLine(string.Format(//"maximumvalue{0}found,with{1}",occurrences,maxVal,count));Debug.WriteLine("maximumvaluesearchcompieted.");returnmaxVal;}
解决方案五:
多了一个“你们给他那么改。。。