小示例题

完成下面的方法,要求返回一个验证码,并且不能同时存在容易混淆的0和Q。每个字母取得的概率要相同。

  他先给出了一部分代码:

 //生成一个随机验证码并且0和O不能同时出现
        public string CreateCode(int count)
        {
           //验证码字符串
            string strcode = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] codes = strcode.Split(',');
            //验证码为空
            string validatecode = string.Empty;
            //实例化随机类
            Random rand=new Random();
            do
            {
                validatecode = string.Empty;
                for (int i = 0; i < count; i++)
                {
                    string s = codes[rand.Next(0, 35)];
                    validatecode += s;
                }
            } while (validatecode.Contains('0') && validatecode.Contains('O'));
               return validatecode;
           
        }

一个选择题,写了一个类:

复制代码
  public class ClassA
    {
        public static int Count = 0;
         static ClassA()
        {
            Count++;
        }

        public ClassA()
        {
            Count++;
        }
    }
复制代码

然后求下面代码的输出结果:

 ClassA A1 = new ClassA();
            ClassA A2
= new ClassA();
            Console.WriteLine(ClassA.Count);

因为静态构造函数是属于类的,而不属于任何一个实例,所以这个构造函数只会被执行一次,而且是在创建此类的第一个实例或引用任何静态成员之前,由.NET自动调用。所以结果为:3

3 也是一个选择题。主要是考override与new的区别,题目大概如下

abstract public class BaseClass
    {
       
public virtual void print()
        {
            Console.WriteLine(
"From BaseClass");
        }

    }

   
public class class1 : BaseClass
    {
       
public override void print()
        {
            Console.WriteLine(
"From Class1");
        }
    }

   
public class class2 : BaseClass
    {
       
public new void print()
        {
            Console.WriteLine(
"From Class2");
        }
    }

然后求执行下面代码后的输出结果:

 

BaseClass ct2 = new class2();
            ct2.print();

 

答案会是:From BaseClass.因为new关键字只是隐藏了Class2中BaseClass的print()方法,并没有改变BaseClass中的Print()方法。

 

而BaseClass ct2 = new class2() 实例化的是BaseClass.

 

但如果调用的是:

ClassA A1 = new ClassA();
            ct1.print();

输出会是:From Class1。因为override关键字在Class1中将BaseClass中的Print()方法进行了重写

 

4 数据库题目。用一个update方法更新两个条件不同的记录。大意是这样的:修改Product表,将Price大于1000的产品的Price提高5%,将Price小于1000的产品的Price提高10%,要求只用一个update。

 

想到用case..when语句

update Product set UnitPrice=(case when (UnitPrice>1000) then UnitPrice*1.05 when (UnitPrice<1000) then UnitPrice*1.1 else UnitPrice end)

 

 5.实例化ClassB后输出结果

class ClassA
    {
       
public ClassA()
        {
            Print();
        }

       
public virtual void Print()
        {
        }
    }

   
class ClassB : ClassA
    {
       
int a = 1;
       
int b;
       
public ClassB()
        {
            b
= -1;
            Print();
        }

       
public override void Print()
        {
            Console.WriteLine(
"a="+a+"  b="+b);
        }
    }

结果:a=1,b=0   a=1,b=-1

6.sql删除除主键外的重复数据

  delete from StuInfo where ID not in(select MIN(id) from StuInfo group by Name,Dept,StuNo,Sex)

   用Max或Min函数都行

7.

找出所有科目的成绩都大于80的学生姓名。

思路1:所有大于80分就是名字不在小于80分的学生里面的学生,主要是用到了一个逆向思维

select distinct Name from Student where Name not in(select Name from Student where Score<80)

思路2:所有大于80分即该学生的最小成绩也大于80.

select name,MIN(Score) from Student group by Name having MIN(Score)>80

思路3:所有大于80分则要求该学生大于80分的科目要有3个(即科目数)。

 select Name,COUNT(*) from Student where Score>80 group by Name having COUNT(*)=3

8.将1-100插入到一个长度为100的数组中

 public void Print()
        {  
            int[] array = new int[100];
            Random rand = new Random();
            int newnum;
            for (int i = 0; i <array.Length; i++)
            {
                do
                {
                    newnum=rand.Next(101);
                  
                }while(array.Contains(newnum));//数组是否存在该值
                array[i]=newnum;
            }
            //按升序排序
            int temp;
            for(int j=0;j<array.Length-1;j++)
            {
                for (int k = array.Length-1; k > j; k--)
                {
                    if (array[k - 1] > array[k])
                    {
                        temp = array[k - 1];
                        array[k - 1] = array[k];
                        array[k] = temp;
                    }
               
                }
            }
            //打印输出
            for (int n = 0; n < array.Length; n++)
            {
                Console.WriteLine(array[n]);
            }
        }

9.求数列:1、1、2、3、5、8、13、21...的第N个数是多少

         /// <summary>
        /// 斐波那契数列
       
/// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private static int Fibonacci(int i)
        {
           
if (i <=2)
            {
               
return 1;
            }
           
else
            {
               
return Fibonacci(i - 1) + Fibonacci(i - 2);
            }
        }

10.统计某个字符串在文本中出现的次数。

 

  public int Count(string text,string find)
        {
            int count = 0;//出现的次数
            int position = 0;//位置
            int index = 0;//索引位置
            do
            {
                index = text.IndexOf(find,position);
                if (index > 0)
                {
                    count++;
                    position=index+1;
                }
            } while (index > 0);

 

            return count;
        }

 

 

 

转载于:https://www.cnblogs.com/zcttxs/archive/2012/06/18/2554099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值