一道C#面试题 面试c# 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是写成这样你会弄不? “,1,2,34,5,”当然如果你愿意把indexof也叫循环,那我表示无语 indexof 内部用到了循环 可能那个公司的意思就是不让自己写foreach之类的代码“死写”,那么用A.Contains('2')就行了。 string test = "1,2,34,5"; int i = 0; Console.WriteLine(IsContains(test.Split(','), i)); Console.Read();方法体部分: public static bool IsContains(string[] arr, int i) { if (i == arr.Length) { return false; } i++; if (arr[i - 1].Contains("p")) { return true; } else { return IsContains(arr, i); } }写的很简陋,感觉本质上和循环了没区别- - string s = "1,2,34,5"; bool result = s.Split(',').Contains("2"); string test = "1,2,34,5"; Console.WriteLine(test.Split(',').Where(x => x.Contains("2")).Count() > 0 ? true : false); string str = "1,2,34,5"; bool bContainedTwo = false; if (str[0] == '2') { bContainedTwo = true; } else if (str[1] == '2') { bContainedTwo = true; } ...... else if (str[7] == '2') { bContainedTwo = true; } MessageBox.Show(bContainedTwo ? "2" : "不2");谁说这是循环我跟谁急! split(',').ContainsContains,IndexOfRegex.IsMatch递归 string s = "1,2,34,5";bool result = s.Split(',').Contains("2"); 都不try,也不判断非int,唉... bool t=Regex.IsMatch("字符串",@"(?<=\D|^)2(?=[,,]|$)"); 你这个方法跟循环都神马区别?假如人家A里面包含1W个元素,你也这样一直if else 下去?那你说跟循环都神马却别啦?其实用Contains('2')就好了,无论A里面有多少个元素也好··代码简洁 这题明显的意思是不要让你用循环,系统自带的内部循环可以的所以方法很多如: string A = "1,2,34,5"; if (A.Split(',').Count(x => x == "21") > 0) MessageBox.Show("包含"); else MessageBox.Show("不包含"); 这里是2不是21,多写了一个1 if (A.Split(',').Count(x => x == "2") > 0) if((","+A+",").indexof(",2,")>-1) Goto + if, 就像<天龙八部>的"天下任何招数武功,都能自行化在这‘六路折梅手’之中 "再去学学, C 语言的的Duff's device, 你就知道, 有了goto就是 "天山折梅手"using System;namespace ff { class Program { static void Main(string[] args) { string A = "1,2,34,5"; int i = 0; int x = -1;lb_S: if('2' == A[i]) { x = i; goto lb_F; } ++i; if(i < A.Length) goto lb_S;lb_F: Console.WriteLine(-1 == x ? "not find" : ("index=" + x)); } }} 不要被培训班的,灌输了各种教条主义.以致对goto是"谈goto色变"goto, 大有用处. 在各方舆论呼喊着,要取消 Goto时. C#作为现代编程语言,丢弃了各种传统语言的特性,却依然雷打不动地 支持goto.大量的elseif 判断,基本可化为 goto + switch (switch,又是某些OOP痴迷者呼喊的,要消灭switch) goto很少用,这个算不算循环了??? 我觉得这题的本意不在考察面试者用index contains 等之类简单方法来实现 string A=","+“1,2,34,5”+",";在用 indexof(",2,") 就可以了。 汇编里面, 有 Jcc 等Jump系列指令也有 LOOPcc 等Loop系列不能认为高级语言里面的 goto 是循环. 就像不能认为汇编中 Jcc 是 LOOPcc虽然,在某些需求上,两者可以等价地实现 其实直接string.Contains("2")就可以了啊 plit(',').ContainsContains,IndexOfRegex.IsMatch递归 不用循环可以用goto任何循环都可以改写成goto比如while (...){}可以写loopstart:if (!...) goto loopend:...goto loopstart:loopend: return ("," + A + ",").Contains(",2,"); 写 string.split, .contains , linq, 基本是too young, too simple .这题目,不用循环, 用递归和goto, 很明显地,goto可读性远远大于递归. 却非要被教条主义桎梏, 写出性能和可读性都差的递归 应该是考用indexof方法,注意2加上逗号 用 比较的字符串也加上逗号用((","+A+",") as string).indexof(",2,") 来判断,不要用直接用A.indexof('2')国为如用是串是1,22,34,5就会出错 获取MP3文件的总时长 怎么按钮不起作用了? 进度条控件的使用 winform WebBrowser控件不能用,一运行整个程序退出。 如何查询2个不同数据库的表 格式化输出的小问题,请大家帮帮忙 请问这个过程怎么实现,为DataGrid对象动态加一个模板列,列名字为"****",自定义绑定表达式为"*****" COM 组件调用 QueryInterface 失败原因? 窗口加载问题 关于writexml和readxml的问题! vs安装mono for android问题 COM口如何用serialport.write 发送下面这个指令 ESC [ / 1
int i = 0;
Console.WriteLine(IsContains(test.Split(','), i));
Console.Read();方法体部分:
public static bool IsContains(string[] arr, int i)
{
if (i == arr.Length)
{
return false;
}
i++;
if (arr[i - 1].Contains("p"))
{ return true;
}
else
{
return IsContains(arr, i);
}
}写的很简陋,感觉本质上和循环了没区别- -
bool result = s.Split(',').Contains("2");
Console.WriteLine(test.Split(',').Where(x => x.Contains("2")).Count() > 0 ? true : false);
bool bContainedTwo = false;
if (str[0] == '2')
{ bContainedTwo = true; }
else if (str[1] == '2')
{ bContainedTwo = true; }
......
else if (str[7] == '2')
{ bContainedTwo = true; }
MessageBox.Show(bContainedTwo ? "2" : "不2");
谁说这是循环我跟谁急!
Contains,IndexOf
Regex.IsMatch
递归
bool result = s.Split(',').Contains("2");
都不try,也不判断非int,唉...
你这个方法跟循环都神马区别?假如人家A里面包含1W个元素,你也这样一直if else 下去?那你说跟循环都神马却别啦?其实用Contains('2')就好了,无论A里面有多少个元素也好··代码简洁
所以方法很多
如:
string A = "1,2,34,5";
if (A.Split(',').Count(x => x == "21") > 0)
MessageBox.Show("包含");
else
MessageBox.Show("不包含");
这里是2不是21,多写了一个1
if (A.Split(',').Count(x => x == "2") > 0)
class Program {
static void Main(string[] args) {
string A = "1,2,34,5";
int i = 0;
int x = -1;
lb_S:
if('2' == A[i]) { x = i; goto lb_F; }
++i;
if(i < A.Length) goto lb_S;
lb_F:
Console.WriteLine(-1 == x ? "not find" : ("index=" + x));
}
}
}
不要被培训班的,灌输了各种教条主义.以致对goto是"谈goto色变"goto, 大有用处. 在各方舆论呼喊着,要取消 Goto时. C#作为现代编程语言,丢弃了各种传统语言的特性,却依然雷打不动地 支持goto.大量的elseif 判断,基本可化为 goto + switch (switch,又是某些OOP痴迷者呼喊的,要消灭switch)
在用 indexof(",2,") 就可以了。
汇编里面, 有 Jcc 等Jump系列指令
也有 LOOPcc 等Loop系列不能认为高级语言里面的 goto 是循环. 就像不能认为汇编中 Jcc 是 LOOPcc
虽然,在某些需求上,两者可以等价地实现
Contains,IndexOf
Regex.IsMatch
递归
while (...)
{}
可以写
loopstart:
if (!...) goto loopend:
...
goto loopstart:
loopend:
却非要被教条主义桎梏, 写出性能和可读性都差的递归
用
((","+A+",") as string).indexof(",2,") 来判断,
不要用直接用A.indexof('2')
国为如用是串是1,22,34,5就会出错