日期:2014-05-18  浏览次数:20790 次

有一个行读取的问题,求教大家!!!
有一个txt文件,里面存储这多行的数字,比如:
000000000000111010010101010000000000000000
111010101000111100000000000000000110010000
000010101010101010010000000000000000000000
100001010100101000000000110000000010001001
110010101010000000000000000000000000000000
................
循环每行,在每行的数组下标0到7,8到15,16到31这样的范围区间内,判断如果是1就输出数组下标。8到15(其它范围也是)范围内有1输出的数组下标要从0开始,不是按原来8到15的下标。谢谢!!

------解决方案--------------------
那就通过查表的方式,要增加分支就加在 spa 数组中
C# code
var array = new string[] { "000000000000111010010101010000000000000000", 
    "111010101000111100000000000000000110010000", 
    "000010101010101010010000000000000000000000", 
    "100001010100101000000000110000000010001001", 
    "110010101010000000000000000000000000000000" };

var spa = new int[] { 0, 8, 16, 32 };

foreach (var item in array)
{
    var index = -1;
    while ((index = item.IndexOf('1', index + 1)) >= 0)
    {
        for (int i = 1; i < spa.Length; i++)
        {
            if (index < spa[i])
            {
                var r = index - spa[i - 1];
                Console.WriteLine(r);
                break;
            }
        }
    }
}