面试题目解答!一直会更新

1、 产生一个长度为100的int数组,并按顺序对数组中的变量进行赋值,值为随机值1-100,数组中的值不能重复

static void Main(string[] args)
        {
            //存放1-100个数  
            int[] num = new int[100];
            //indexNum[0]=1;...indexNum[99]=100;(不重复)  
            for (int i = 0; i < num.Length; i++)
            {
                num[i] = i + 1;
            }
            Random r = new Random();
            //存放最终结果  
            int[] result = new int[100];
            int max = 100;//设置随机数最大值  
            for (int j = 0; j < result.Length; j++)
            {
                //计算出随机产生的数组索引(0-99) 注:上界值:100 无法取到  
                int index = r.Next(0, max);
                //取出该索引位置所存的数  
                result[j] = num[index];
                //用最后一个数替换掉已被放入result中的数  
                //这样num数组中从0到max-1又都是未被存放入result而且不重复的数了  
                num[index] = num[max - 1];
                //随机产生的数组索引最大值减一  
                max--;
            }
            //打印结果  
            for (int i = 0; i < 100; i++)
            {
                if (i % 10 == 0)
                {
                    Console.WriteLine();
                }
                Console.Write(result[i] + " ");
            }
            Console.Read();
        }

2、winform请使用代码(属性、委托、事件等),实现一个文本框的ValueChanged事件。

int i = 0;
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            changeEvent += Form1_Change;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            TempValue = i+"";
            label1.Text =TempValue;
            i++;
        }
        void Form1_Change(string value)
        {
            TxtChange.AppendText("当前文本改变的值为:"+value + "\r\n");
        }

        public delegate void ChangeDelegate(string value);
        public event ChangeDelegate changeEvent;
        public string _tempValue;
        public string TempValue
        {
            get { return _tempValue; }
            set
            {
                if (_tempValue != value)
                {
                    changeEvent(value);
                }
                _tempValue = value;
            }
        }

3、int a = 5,int b = 10,请在不使用第三个变量的情况下,交换a和b的值。

a = a + b; --a = 15

b = a - b; --b = 5

a = a - b; --a = 10

4、 仔细读下面的代码,并写出B.a1()输出的结果

 class A
    {
        public A()
        {
            OutPut();
        }
        public void OutPut()
        {
            Console.WriteLine("A-a1");
        }

}

class B : A
    {
        int bb = 0;
        public B()
        {
            bb = 1000;
        }
        public new void OutPut()
        {
            Console.WriteLine("B-a1");
            Console.WriteLine("bb is " + bb);
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            b.OutPut();
            Console.ReadLine();
        }
}

请在下方填写输出结果:
A-a1
B-a1
bb is 1000

  5、写一个函数,计算0N之间出现过的字符”2”的次数

public static int  countBy (int n, int k)
        {
            if (n < 10 && k == 0)
            {
                return 1;
            }
            //出现总次数
            int sum = 0;
		   //1  10 100 1000 10000 递增
            int level = 1;
            while (n / level != 0)
            {
                if (k == 0 && n / (level * 10) == 0)
                {
                    // 最高位不能为0
                    break;
                }
//计算当前位
                int locat = (n / level) % 10;
			   //计算当前位高位
                int hight = n / (10 * level); 
		       //计算当前位的低位
                int lower = n - n / level * level; 
                if (locat > k)
                {
                    sum += (hight + 1) * level;
                }
                else if (locat < k)
                {
                    sum += (hight) * level;
                }
                else
                {
                    sum += (hight) * level + lower + 1;
                }
                level *= 10;
            }
            return sum;
        }

  6、设计一个算法,找到数组中所有和为20的整数对,用List返回

public static ArrayList Find(int[] nums, int sum)
        { 
            int one, two;
            ArrayList arr = new ArrayList();
            for (int i = 0; i < nums.Length; i++)
            {
                one = nums[i];
                two = sum - one;
                for (int j = 0; j < nums.Length; j++)
                {
                    if (i != j)
                    {
                        if (two == nums[j])
                        {
                            arr.Add(one+","+two);
                            //Console.WriteLine ("one:" + one + " two:" + two);
                        }
                    }
                }   
              
            }
            return arr;
	} 
	static void Main(string[] args)
        { 
      int[] numbers = new int[19] { 1, 2, 3, 4, 5, 6, 8, 7, 9, 10,11,12,13,14,15,16,17,18,19 };
          foreach (var item in Find(numbers, 20))
            {               
         Console.WriteLine(item);
            }
          Console.ReadLine();
      }
 

  

 7、 仔细读下面的代码,写出ab的值

static void Main(string[] args)

        {

 

            StringBuilder a = new StringBuilder("A");

            StringBuilder b = new StringBuilder("B");

            operate(a, b);

            Console.WriteLine(a + "," + b);

            Console.ReadLine();

 

        }

 

        static void operate(StringBuilder x, StringBuilder y)

        {

            x.Append(y);

            y = x;

        }

请在下方填写ab的值:

 

a:AB

b:B

8、     学生表如下:

表名 T_ Score

字段

注释

ID

自动编号,不重复

STUNO

学号

Stuname

学生姓名

Prono

课程编号

Proname

课程名称

Score

分数

 

数据

ID

STUNO

Stuname

Prono

Proname

Score

1

2005001

张三

0001

数学

60

2

2005002

李四

0001

数学

80

3

2005003

张三

0001

数学

60

 

写一个SQL,删除除了ID不一样其它数据都一样的重复数据,保留一条:

DELETE FROM [T_ Score] WHERE ID NOT IN

(SELECT MIN(ID) FROM

(

SELECT  * FROM [T_ Score]) AS T GROUP BY  T.Proname);

9、 存在表T(a,b,c,d),要根据字段c排序后取第2130条记录显示,请给出sql

SELECT * FROM (

SELECT   ROW_NUMBER() OVER (ORDER BY C DESC) NUMBER,* FROM T

) T

 WHERE T.NUMBER between 21 and 30;

1.1        仔细读下面的代码,并写出B.a1()输出的结果

 classA

    {

        public A()

        {

            OutPut();

        }

        publicvoid OutPut()

        {

            Console.WriteLine("A-a1");

        }

 

}

 

classB : A

    {

        int bb = 0;

        public B()

        {

            bb = 1000;

        }

        publicnewvoid OutPut()

        {

            Console.WriteLine("B-a1");

            Console.WriteLine("bb is " + bb);

        }

    }

 

classProgram

    {

        staticvoid Main(string[] args)

        {

            B b = new B();

            b.OutPut();

            Console.ReadLine();

        }

}

 

请在下方填写输出结果:

A-a1

B-a1

bb is 1000

猜你喜欢

转载自www.cnblogs.com/Freedom0221/p/12575553.html