有个0和1组成的数组,比如0000111000111000,需要求出出现了几段1,每段1的位置

如题,出现了两段1,第一段1的起点是5,终点是7。需要用C#语言实现,请大家提供点思路。谢谢

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace codeForCSDN
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("请输入数据:");

            string data = Console.ReadLine();

            int index, i;

            bool flag = false;

            System.Collections.ArrayList indexList = new System.Collections.ArrayList();

            for(index = 0; index < data.Length; index++){

                if(data[index] == '1'){

                    if(!flag){

                        indexList.Add(index);

                        flag = true;
                    }
                }
                else{

                    if (flag) {

                        flag = false;

                        indexList.Add(index);
                    }
                }

            }

           // System.Console.WriteLine(indexList.Count);

            int cont = 1;

            string res;

            for (i = 0; i < indexList.Count; i++) {

                if (i % 2 == 0)
                {

                    res = "第" + cont + "段的起始位置为" + indexList[i];

                    System.Console.WriteLine(res);
                }
                else {

                    res = "第" + cont + "段的结束位置为" + indexList[i];

                    System.Console.WriteLine(res);

                    cont++;
                }


            }

           Console.ReadKey();
        }

    }
}

我有一个想法:循环遍历数组,每个元素相加。如果和变大了就是1,否则就是0.至于记录位置和几段就简单了。

通过写这个程序我发现了我上个程序中有漏掉的情况(就是以1111结尾时),lz可以根据我这个程序中的first和end方法进行修改

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace ConsoleApplication2
{
    public class dataReg
    {
        public int first, end, data;

        public dataReg(int f, int e, int d) 
        {
            this.data = d;

            this.first = f;

            this.end = e;
        }

    }

    class mySort : System.Collections.IComparer
    {
        public int Compare(object a, object b) 
        {
            return ((dataReg)a).data - ((dataReg)b).data;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("请输入数据:");

            string data = Console.ReadLine();

            int first = 0, last;

            ArrayList list = new ArrayList();

            for (last = 0; last < data.Length; last++)
            {
                if (data[first] != data[last])
                {
                    dataReg node = new dataReg(first, last - 1, data[first] - '0');

                    list.Add(node);

                    first = last;
                }
                if (last == data.Length - 1)
                {
                    dataReg node = new dataReg(first, last, data[first] - '0');

                    list.Add(node);
                }
            }
            mySort mysort = new mySort();

            list.Sort(mysort);

            int cont = 1;

            for (int i = 1; i < list.Count; i++)
            {
                if (((dataReg)list[i]).data != ((dataReg)list[i - 1]).data)
                {
                    Console.WriteLine("data为:" + ((dataReg)list[i - 1]).data + "的有" + cont + "段");

                    for (int j = 1; j <= cont; j++)
                    {
                        Console.WriteLine("起始位:" + ((dataReg)list[i - j]).first + ", 终止位:" + ((dataReg)list[i - j]).end);
                    }

                    cont = 1;
                }

                else cont++;

                if (i == list.Count - 1)
                {
                    Console.WriteLine("data为:" + ((dataReg)list[i]).data + "的有" + cont + "段");

                    for (int j = 1; j <= cont; j++)
                    {
                        Console.WriteLine("起始位:" + ((dataReg)list[i + 1 - j]).first + ", 终止位:" + ((dataReg)list[i + 1 - j]).end);
                    }
                }

            }

            Console.ReadLine();
        }
    }
}