|
2008年9月24日
#
面试时遇到的二道题,有点意思.贴出来做个纪念!
1.(C#的题目)1,1,2,3,5,8,13...,利用递归方法计算出第30位的结果?
 Code
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
/// <summary>
/// 递归调用,实现如1,1,2,3,5,8,13
/// </summary>
class Program
{
static void Main(string[] args)
{
Program p=new Program();
for (int i = 1; i < 31; i++)
{
Console.WriteLine("参数:{0},递归调用的结果是:{1}",i, p.ReturnValue(i));
}
}
/// <summary>
/// 递归调用的方法
/// </summary>
/// <param name="Num"></param>
/// <returns></returns>
int ReturnValue(int Num)
{
int re = new int();
if (Num == 1 || Num == 2)
re = 1;
else
re = ReturnValue(Num - 1) + ReturnValue(Num - 2);
return re;
}
}
}
2.(数据库题目)部门表unitBM如下
unitID parentID unitName
员工表personYG如下
autoID unitID nameYG
现知道部门ID(unitID)为'un001',找出该部门及该部门下N级子部门的所有员工信息.
其实这是一个无限分类的例子,当时想到的是用游标.后来回来想一下才改成这样!
 Code
if object_id('unitBM') is not null
drop table unitBM
go
create table unitBM
(
AutoID int identity(1,1) primary key,
unitID varchar(50),
parentID varchar(50),
unitName varchar(50)
)
insert into unitBM(unitID,parentID,unitName)
select 'un001','','总经办' union all
select 'un002','un001','财务部' union all
select 'un003','un001','电脑部' union all
select 'un006','un002','会计部' union all
select 'un009','','人事部'
go
if object_id('personYG') is not null
drop table personYG
go
create table personYG
(
AutoID int identity(1,1) primary key,
unitID varchar(50),
ygName varchar(50)
)
go
insert into personYG(unitID,ygName)
select 'un001','张三' union all
select 'un002','李四' union all
select 'un001','王五' union all
select 'un003','张六' union all
select 'un006','周八' union all
select 'un005','蔡九'
go
create function get_chileID(@unitID varchar(50))
returns @returntb table(unID varchar(50))
as
begin
insert into @returntb select @unitID
while @@rowcount>0
insert into @returntb
select a.unitID from unitBM a inner join @returntb b on a.parentID=b.unID
where a.unitID not in (select unID from @returntb)
return
end
select * from get_chileID('un001')
select * from personYG where unitID in(select * from get_chileID('un001'))
|