本篇主要关注下,.net面试题中经常用的算法问题
1、有一群猴子,它们每天要吃桃子,它们第一天吃的数量是总量的一半再多一个,第二天吃的是第一天剩下的一半再多一个,第三天吃的是第二天剩下的一半多一个,以此类推直到第N天的时候只剩下一个桃子了,请问一共有多少桃子?用用递归算法实现
代码:
[code lang="csharp"]
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//假设猴子吃了一个月的桃子
for (int i = 0; i < 30; i++)
{
total = 1;
Test1(i);
}
Label1.Text = "30天前,这群猴子有" + total + "个桃子";
}
}
private static Int64 total;
//猴子吃桃的问题
private void Test1(int n)
{
//根据猴子吃桃的问题,可以表示成:n-n/2-1=m(m是剩余的,n是前一天剩余的总量)
//及:n=(m+1)*2
if (n > 0)
{
total = (total + 1) * 2;
n--;
Test1(n);
}
else
{
return;
}
}
[/code]
2、不准用系统函数,请写出一个函数,传入一个日期,判断是星期几?
代码:
[code lang="csharp"]
//每n-1月有多少天
private static int WhatMonthDay(int month, bool isLeapYear)
{
int result = 0;
switch (month)
{
case 1:
result = 0;
break;
case 2:
result = 31;
break;
case 3:
result = 31 + 28;
break;
case 4:
result = 31 + 28 + 31;
break;
case 5:
result = 31 + 28 + 31 + 30;
break;
case 6:
result = 31 + 28 + 31 + 30 + 31;
break;
case 7:
result = 31 + 28 + 31 + 30 + 31 + 30;
break;
case 8:
result = 31 + 28 + 31 + 30 + 31 + 30 + 31;
break;
case 9:
result = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
break;
case 10:
result = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
break;
case 11:
result = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
break;
case 12:
result = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
break;
}
result = result + (isLeapYear == true ? 1 : 0);
return result;
}
// 不准用系统函数,请写出一个函数,传入一个日期,判断是星期几?大家有什么思路?
private static int WhatWeek(int year, int month, int day)
{
int temp = 1; //星期一
int basicYear = 1990; //1990年是闰年
int basicMonth = 01;
int basicDay = 01;
int yearGap = year - basicYear; //两个时间年的差距
int yearLeapCount = 0; //闰年的个数
int allday = 0;
if (yearGap % 4 == 0)
{
yearLeapCount = year - yearGap;
allday = yearLeapCount * (365 + 365 + 365 + 366) + WhatMonthDay(month, true) + day;
}
else
{
yearLeapCount = year - yearGap;
allday = (yearLeapCount - 1) * (365 + 365 + 365 + 366) + 365 * (yearGap % 4) + WhatMonthDay(month, false) +
day;
}
return allday % 7;//返回是星期几
}
[/code]
3、a=10,b=15,在不用第三方变题的前提下,把a,b的值互换
代码:
private static int[] NoChance()
{
int a = 10;
int b = 15;
b = b - a;
a = a + b;
b = a - b;
return new int[]{a,b};
}
4、session喜欢丢值且占内存,Cookis不安全,请问用什么办法代替这两种原始的方法
ViewState、stateserver
5、对数据的并发采用什么办法进行处理较好
(1)最简单,最直接的方法,控制IIS连接数,但这个方法对公司平台不利,限制了用户的访问
(2)对不经常变动的内容,采用缓存减少查询数量,对经常需要查询的,采用nolock关键词进行查询,防止死锁。
(3)对数据库进行分库,分表,进行细化
(4)如有可能,对硬件进行提升。采用分布式和数据复制,对网站流量进行分流,把站点分布在不同的地区,指定区域访问指定服务器,根据不同地区的需求,对服务器进行配置,有效提升网络。
但不是说什么方法都是唯一的,真正用的时候,还是根据实际情况,多种方法结合使用,效果最好
6、当对数据库进行海量级的数据插入时,数据库出现报错,错误原因可能有哪些,以你的经验谈谈你的解决办法
(1)数据库死锁,有多条数据同时操作
(2)网络超时,无响应
(3)数据库磁盘空间不足
7、简述面向对象的多态的特性及意义
8、javascript问题,已知a,b,现在点鼠标a会向b游动,鼠标停,a会停下来
代码如下:
[code lang="csharp"]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>JavaScript</title>
<style>
html
{
background-color:silver;
}
.point1
{
position:absolute;
left:10px;
top:40px;
}
.point2
{
position:absolute;
left:100px;
top:40px;
}
.hr1
{
position:absolute;
top:60px;
}
</style>
<script type="text/JavaScript">
document.onmousedown = mousedown;
document.onmouseup = mouseup;
var intervalProcess;
var direct = true;
function mousedown() {
intervalProcess = setInterval("MovePoint()", 1);
}
function mouseup() {
clearInterval(intervalProcess);
}
function MovePoint() {
with (document.getElementById("point1").style) {
if (isNaN(parseInt(left)))
left = "10px";
else {
document.getElementById("point2").style.left = "200px";
if (parseInt(left) < 0)
direct = true;
if (parseInt(left) > parseInt(document.getElementById("point2").style.left))
direct = false;
if (direct)
left = parseInt(left) + 1 + "px";
else
left = parseInt(left) - 1 + "px";
}
}
}
</script>
</head>
<body>
<div class="point1" id="point1"><font color=blue>a</font></div>
<div class="point2" id="point2"><font color=red>b</font></div>
<hr class="hr1" />
</body>
</html>
[/code]
9、产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
//产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
private int[] Test()
{
int[] intArr=new int[100];
ArrayList tempArr=new ArrayList();
Random random=new Random();
while (tempArr.Count<100)
{
int temp = random.Next(1, 100);
if (!tempArr.Contains(temp))
{
tempArr.Add(temp);
}
}
for (int i = 0; i < tempArr.Count; i++)
{
intArr[i] = (int)tempArr[i];
}
return intArr;
}
10、short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
short s1 = 1; s1 = s1 + 1; (s1+1运算结果是int型,需要强制转换类型)
short s1 = 1; s1 += 1;(可以正确编译)