如題,出現了兩段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();
}
}
}