首页 新闻 会员 周边

索引位置算法.

0
悬赏园豆:100 [已关闭问题]

ds32Christchurch   2 b   1200       - Rosamond J.S3.     
ston         2 a     916       - Sidney 34
5 a     408       - Thomas A. 12 Youkq k

3hitehaven  10 b   1317 

要求返回一行中相对数字比较集中最后的一个索引

以上结果应为

1200 后面的第一个位置

916       后面的一个位置

408  后面的一个位置

1317   后面的一个位置

 

问题补充: 我意思是相对集中的数字块中最后一个位置 第一行 32, 2 , 1200 即需要求得1200后面的位置 rch 2 b 2 12 - Rosamond J.S33. 是要求得12后面的位置
Harry.Liu的主页 Harry.Liu | 初学一级 | 园豆:0
提问于:2009-09-18 15:05
< >
分享
其他回答(1)
0

什么叫一行中相对数字比较集中最后的一个索引?

第一行 32, 2 , 1200 和 3 都是数字,为什么1200就相对集中?

eaglet | 园豆:17139 (专家六级) | 2009-09-18 15:44
0

基本算法:先把字符串转成一个字符数组,然后循环,取三个变量maxLength,tempMax,maxIndexPosition,,当连续为数字时,tempLength++,然后与maxLength比较,记下maxLength和maxIndexPosition,直到行结束。

 //static string str = @"ds32Christchurch   2 b   1200       - Rosamond J.S3.     ";
/// <summary>
/// 获取一行字符串中数字连续了大值的位置,如
/// static string str = @"ds32Christchurch 2 b 1200 - Rosamond J.S3. ";
/// 中的位置为1200后,即29
/// downmoon(邀月) 2009.9.18 3w@live.cn
/// </summary>
/// <param name="sOrg">原始字符串</param>
/// <returns></returns>
public static int GetPosition(string sOrg)
{
int result = -1;
if (string.IsNullOrEmpty(sOrg)) { return result; }
int maxlength = 0;//最大连续长度
int maxIndex = -1;//怪大连续值的索引位置
int index = -1;//临时索引
int temp = 0;//临时变量,用于和最大连续值的比较
foreach (char c in sOrg)
{
index
++;//索引位置累加
if (Char.IsDigit(c))
{
temp
++;
if (temp > maxlength)
{
maxlength
= temp;//记录连续数字最大值
maxIndex = index + 1;//记录连续数字最大值的索引位置
result = maxIndex;
}
}
else { temp = 0; }

}
return result;
}


测试:

 public static void Main(string[] args)
{
string str = @"ds32Christchurch 2 b 1200 - Rosamond J.S3. ";
Console.WriteLine(
"该行中数字比较集中的位置是:" + GetPosition(str));
Console.ReadKey();
}


结果:29

邀月 | 园豆:25475 (高人七级) | 2009-09-18 16:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册