一直很讨厌面试的时候答题,但有的时候一道很简单的题能反映出你的计算机水平。比如就出一道很简单的题目,求值:1+2+......100 ?
很有好几种方法,如下:

Code
1. int tol = 0;
for (int i = 1; i <= 100; i++)
{
tol += i;
}
Response.Write(tol);
2. int n = 100;
int reset=n*(n+1)/2;
Response.Write(reset);
3. int total = 0;
for (int i = 1; i <= 50; i++)
{
total = total + i + (100 - i);
}
Response.Write(total + 50);
4. int max = 100;
if(max%2==0)
{
Response.Write((1 + max) * max / 2);
}
else
{
Response.Write((1 + max) * (max + 1) / 2 - max);
}
不管哪种方法都可以得出正确的结果,但可以看出你是否全部依靠计算机,自己有没有思考。