zoukankan      html  css  js  c++  java
  • 编程题

    1、编写程序从以下字符串提取数字?
    string str = "abc";//"大家好呀,hello,2010年10月10日是个好日子。恩,9494.吼吼!886";
    string regstr = @"d+";
    MatchCollection mc= Regex.Matches(str, regstr);
    foreach (Match item in mc)
    {
    Console.WriteLine(item.Value);
    }

    2、编写程序从以下数组中筛选出偶数?Var arrr=new int[]{1,2,3,4,5,6,7,8,9};用linq和lambda表达式实现?
    lamda:
    var array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    IList b =array.Where(p => p % 2 == 0).ToList();
    foreach (var item in b)
    {
    //Console.WriteLine(item);
    }


    linq:
    var t = from item in array
    where item % 2 == 0
    select item;
    foreach (var item in t)
    {
    Console.WriteLine(item);
    }

    3、用泛型实现一个交换两个变量值的方法,变量可以是任意类型?
    例1:有两个整型变量:
    Int x=1;
    Int y=2;
    调用完此方法后变为:
    Int x=2;
    Int y=1;
    例2:有两个整型字符串变量:
    String x = "x";
    String y="y";
    调用完此方法后变为:
    String x="y";
    String y="x";
    public void Exchange<T>(ref T v1,ref T v2)
    {
    T temp=v1;
    v1=v2;
    v2=temp;
    }

    4、请用Lambda表达式实现以下要求:有一个Task实体集合IEnumerable<Task> tasks;每个Task实体的定义:
    public class Task{
        public string Name{get;set;}
        public string Description{get;set;}
        public string Rules{get;set;}
        public string Type{get;set;}
        public string RelatedTaskIds{get;set;}
        public virtual Task Parent{get;set;}
        public virtual IList<Task> Children{get;set}
    }
    一:查询出所有Task的名字,返回一个Ilist<string>
    List<String> list = Tasks.select(t=>t.Name).ToList();
    二:查询出所有Type是Receptionist的Task,返回一个Ilist<Task>
    List<Task> list = tasks.Where(t.Type="Receptionist").ToList();
    三:根据Parent进行分组,统计每个父任务有多少子任务,返回一个Ilist<KeyValuePair<string,int>>
    IList<KeyValuePair<string, int>> list;
    //按Parent分组统计
    list = tasks.GroupBy(t => t.Parent).Select(g => new KeyValuePair<string, int>(g.Key, g.Count())).ToList();
    //输出统计
    list.ForEach(kv=>Console.WriteLine(kv.Key+":"+kv.Value));

    1、在.Net(C# or vb.Net)中如何获得当前窗体或控件的句柄,特别是控件本身的句柄(请列举)。
    答:this(C#)、Me(vb.net).
    2、在.Net(C# or vb.Net)如何启动另一个程序
    答:Process 如启动cmd:Process.Start("cmd");//启动CMD
    3、在.Net(C# or vb.Net)中如何取消一个窗体的关闭
    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e){e.Cancel=true;}
    4、在C#中有一个double型的变量,比如10321.5,比如122235401.21644,作为货币的值如何按各个
    不同国家的习惯来输出。
    比如美国用$10321.50和$122235401.22
    而在英国则为£10 321.50和£122 235 401.22
    答:System.Globalization.CultureInfo MyCulture = new System.Globalization.CultureInfo("en-GB");//为英国货币类型
                decimal y = 999999999999999m;
                string str = String.Format(MyCulture, "My amount={0:c}", y);
                Console.WriteLine(str);
                Console.ReadLine();//{0,10:c} 0→下标,10→字符串要占用的字符数如果字符数不够也会完整显示,:c→格式说明符d,e,f,g,n,p,x
    5、如何把一个array复制到arrayList里
    答:foreach( object o in array )arrayList.Add(o);
             实现1:string[] s={"111","222"};  ArrayList list = new ArrayList(); list.AddRange(s);
             实现2:string[] s={"111","222"};  ArrayList list = new ArrayList(s);
    6、能用foreach遍历访问的对象需要实现_______接口或声明_________方法的类型
    答:IEnumerable 、GetEnumerator。
    7、写出一条Sql语句: 取出表A中第11到第20记录(以自动增长的ID作为主键, 注意:ID可能不是连续的。)
    select * from dbo.MyStudent order by Fid desc
    --第一种 外层查询没有对表A查询,效率大有提高
    select top 10 * from (select top 20 * from dbo.MyStudent order by Fid) as a order by Fid desc
    
    --第二种对表A查询效率较低
    select top 10 * from dbo.MyStudent where Fid not in 
    (select top 10 Fid from dbo.MyStudent)
    
    select top 10 * from dbo.MyStudent where Fid >(
    select max(Fid) from (select top 10 * from MyStudent order by FID) as A
    )
    
    --第三种ROW_NUMBER()函数效率更高
    select * from (select row_number() over(order by Fid) as num,* from dbo.MyStudent) as t
    where t.num between 11 and 20
    8、表A字段Id为numberic(180),哪个SQL语句是错误的:
    select * from A where id='';
    select * from A where id='13';
    select * from A where id=null;
    select * from A where id=' 13';
    答案:A
    9、有一职员表,字段有工卡和姓名,写出所有姓名重复的职员信息的SQL语句。
    Select * from test where Name in (select Name from dbo.test group by name having count(0)>1)
    10、有两张表Table1,Table2 ,都只有一个字段。Table有数据'1''2''3''4',Table2有'1''3'.
    写出一句通用Sql语句将表Table1的记录同步到Table2中!
    insert into dbo.Table_2(tb2ID) 
    select * from dbo.Table_1 where tb1ID not in (select tb2ID from dbo.Table_2)
    11、横纵表转换:
    --纵表结构
    Name    Course    Grade
    张三    语文    75
    张三    数学    80
    张三    英语    90
    李四    语文    95
    李四    数学    55
    --横表结构
    Name    语文    数学    英语
    张三    75    80    90
    李四    95    55           0
    
    
    select Name as 姓名
            ,sum(case Course when '语文' then Grade else 0 end) as '语文'
            ,sum(case Course when '数学' then Grade else 0 end) as '数学'
            ,sum(case Course when '英语' then Grade else 0 end) as '英语'
    from table
    group by Name
    
    --纵表结构
    单号    金额
    RK1    10
    RK2    20
    RK3    -30
    RK4    -10
    --横表结构
    单号    收入    支出
    RK1    10    0
    RK2    20    0
    RK3    0    30
    RK4    0    10
    
    select 单号
    ,case when 金额>0 then 金额 else 0 end as 收入
    ,case when 金额<0 then abs(金额) else 0 end as 支出
    from table
    
    纵表结构
    Date        Name    Score
    2008-8-8    拜仁    胜
    2008-8-9    奇才    胜
    2008-8-9    湖人    胜
    2008-8-10    拜仁    负
    2008-8-8    拜仁    负
    2008-8-12    奇才    胜
    横表结构
    Name    胜    负
    拜仁    1    2
    湖人    1    0
    奇才    2    0
    注意:在中文字符串前加N,比如N''
    select Name
            ,sum(case when Score='' then 1 else 0 end) as N''
            ,sum(case when Score='' then 1 else 0 end) as N''
    from table
    group by Name
    
    横表转纵表:
    --横表
    ID    姓名    语文    数学    英语
    1    张三    80    90    70
    2    李四    90    85    95
    3    王五    88    75    90
    
    --纵表
    ID    姓名    科目    成绩
    1    张三    语文    80
    2    张三    数学    90
    3    张三    英语    70
    4    李四    语文    90
    5    李四    数学    85
    6    李四    英语    90
    7    王五    语文    88
    8    王五    数学    75
    9    王五    英语    90
    select 姓名,'语文' as 科目,语文 as 成绩 from table union all
    select 姓名,'数学' as 科目,数学 as 成绩 from table union all
    select 姓名,'英语' as 科目,英语 as 成绩 from table union all
    order by 姓名,科目 desc;
    
    
    12、如何在表中随机取记录数100条?请写出示例SQL语句?
    答:select top 100 * from dbo.Jokes order by newID()
    13、在SQLServer中求当前时间与2012-01-01 00:0相差的秒数?
    答:select DateDiff(second,'2012-01-01 0:0:0',getDate())
    14、一列数的规则如下: 112358132134...... 求第30位数是多少, 用递归算法实现
    static int foo(int i){
        if(i<=0){
            return 0;
        }else if(i>0 && i<=2){
            return 1;
        }else{
            return foo(i-1)+foo(i-2);
        }
    }
    Console.WriteLine(foo(30));
    15、请编程实现一个冒泡排序算法?
    //第一种方法
    --从小到大
    int[] arrays = new int[]{6,5,3,9,7,4,0};
        int temp=0;
        int i,j;
        for(i=0;i<arrays.Length;i++){
            for(j=i;j<arrays.Length;j++){
                if(arrays[i]>arrays[j]){
                    temp = arrays[i];
                    arrays[i] = arrays[j];
                    arrays[j] = temp;
                }
            }
            Response.Write(arrays[i]);
        }
    
    //第二种方法
    --从小到大
    for(i=0;i<arrays.Length-1;i++){
        for(int j=0;j<arrays.Length-1-i;j++){
            if(arrays[j]>arrays[j+1]){
                temp = arrays[j];
                arrays[j]=arrays[j+1];
                arrays[j+1] =temp;
            }
        }
    }
    for(i=0;i<arrays.Length;i++){
        Response.Write(arrays[i]+"
    ");
    }
    16、求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m
    17、请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
    static int foo(int num){
        int sum =0;
        for(int i=1;i<=num;i++){
            if(i%2==0){
                sum-=i;
            }else{
                sum+=i;
            }
        }
        return sum;
    }
    Console.WriteLine(foo(m));
    foreach(System.Windows.Forms.Control control in this.Controls){
        if(Control is System.Windows.Forms.TextBox){
            System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control;
            tb.Text = String.Empty;
        }
    }
    
    
    18、给定以下XML文件,完成算法流程图。
    给定以下XML文件,完成算法流程图。
     <FileSystem>
               <DriverC>
                    <Dir DirName="MSDOS622">
                            <File FileName =" Command.com" ></File>
                    </Dir>
                            <File FileName ="MSDOS.SYS" ></File>
                            <File FileName =" IO.SYS" ></File>
            </DriverC>
    </FileSystem>
    请画出遍历所有文件名(FileName)的流程图(请使用递归算法)。
    
    //思路
    void FindFile(Directory d){
        FileOrFolders = d.GetFileOrFolders();
        foreach(FileOrFolder fof in FileOrFolders){
            if(fof is File){
                You Found a file;
            }else if(fof is Directory){
                FindFile(fof);
            }
        }
    }
    
    19、产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。(要求使用两种方法)
       //做法1  
        int[] intArr=new int[100];
          ArrayList myList=new ArrayList();
          Random rnd=new Random();
          while(myList.Count<100)
          {
              int num=rnd.Next(1,101);
              if(!myList.Contains(num))
              {
                  myList.Add(num);
              }
          }
          for(int i=0;i<100;i++)
          {
              intArr[i]=(int)myList[i];
          }
    //做法2:
    先把1一100这100个数按顺序放入数组arr,再重复100次下面的操作,,
    生成两个介于>=0,<1oo之间的随机数m、n,颠倒arr[m]和arr[n]的数。
    int[] arr = new int[100];
    //把100个数顺序放入
    for(int i=0;i<100;i++){
        arr[i] = i+1;
    }
    Random rand = new Random();
    for(int i=0;i<100;i++){
        //随机生成2个位置
        int m = rand.Next(0,100);
        int n = rand.Next(0,100);
        //颠倒两个位置
        int temp = arr[m];
        arr[m] = arr[n];
       arr[n] = temp;
    
    
    20、如何把一个array复制到arrayList里
                 int[] arrayInt = new int[] { 1, 2, 3, 4 };
                            ArrayList arr = new ArrayList();
                            //方法1
                            //foreach (int i in arrayInt)
                            //{
                            // arr.Add(i);
                            //}
                            //foreach (var item in arr)
                            //{
                            // Console.WriteLine(item);
                            //}
                            //方法2
                            ArrayList arr2 = new ArrayList();
                            arr2 = ArrayList.Adapter(arrayInt);
                            for (int i = 0; i < arr2.Count; i++)
                            {
                                    Console.WriteLine(arr2[i].ToString());
                            }
                            Console.ReadKey();
    21、删除姓名、年龄重复的记录(只保留Id最大的一条)
    Id    name    age    salary
    1    yzk    80    1000
    2    yzk    80    2000
    3    tom    20    20000
    4    tom    20    20000
    5    im    20    20000
    
    delete from A where ID not in
    (select max(id) from A group by name,age)
    
    22、有一个10个数的数组,计算其中不重复数字的个数。{35981053}
    int[] values = {3,5,9,8,10,2,5,3};
    HashSet<int> set = new HashSet<int>();
    foreach(int i in values){
            set.Add(i);
    }
    Console.WriteLine(set.Count);
    23、下面是一个由*号组成的4行倒三角形图案
    一>要求:
    1、输入倒三角形的行数,
         行数的取值3一21之间,对于非法的行数,,
         要求抛出提示"非法行数!”
    2、在屏幕上打印这个指定了行数的倒三角形。
            *******
             *****
              ***
               *
            while (true)
                {
                    Console.Write("请输入一个3-21的整数:");
                    try
                    {
                        int num = Convert.ToInt32(Console.ReadLine());
                        if (num < 3 || num > 21)
                        {
                            Console.WriteLine("非法行数");
                            continue;
                        }
                        for (int i = num; i >=0; i--)
                        {
                            for (int m =0; m < num-i; m++)
                            {
                                Console.Write(" ");
                               
                            }
                            for (int j = 2 * i - 1; j > 0; j--)
                            {
                                Console.Write("*");
                            }
                            Console.WriteLine();
                        }
                        Console.ReadLine();
                    }
                    catch
                    {
                        Console.WriteLine("非法行数!");
                        continue;
                    }
                }
    
    24、在winfrom中,其中有个窗体类,有四个单选按钮,分别为A,B,C,D如何实现当选中项改变的时候弹出一个警示告诉用户当前所选中的选项名称?
    //设置radio1/radio3/radio4的mouseclick事件都是radioButton2_MouseClick
            private void radioButton2_MouseClick(object sender, MouseEventArgs e)
            {
                RadioButton RB = sender as RadioButton;
                MessageBox.Show(RB.Text.ToString());
            }
    
    25、在winfrom中,A窗体,有一个TextBox和一个Button,如何实现点击Button弹出B窗体,点击B窗体的Button按钮弹出A窗体中TextBox的值
    Form1:
     private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.FormMethod += Method;
                f2.Show();
                
            }
            public void Method()
            {
                MessageBox.Show(textBox1.Text);
            }
    Form2:
    public event Action FormMethod;
            private void button1_Click(object sender, EventArgs e)
            {
               // MessageBox.Show(f1.Tag.ToString());
                if (FormMethod != null)
                {
                    FormMethod();
                }
            }
    
    26
    
    实现如图效果
    divl{
        100px;
        height:100px;
    }
    #dlvl{
        background--color:green;
        position:fixed;
        margin:10px 10px;
        z-Index:1;
    }
    #div2{
        backgroLuld--color:red;
        positlon:fixed;
        margin:20px 20px;
        z一index:2;
    }
    #div3{
        background--color:black;
        posltion:fixed;
        margin:30px 30px;
        z一index:3
    }
    
    27、写出复制表、拷贝表和四表联查的SQL语句。
    答案:,
    复制表(只复制结构,源表名:A,新表名:B)
    Select * into B from A where 1=0;
    拷贝表(拷贝数据,源表名:A,新表名:B):
    Select * into B from A
    四表联查:,·
    Select * from A,B,C,D where 关联条件·
    
    28//相传有一群猴子要选出大王,它们采用的方式为:所有猴子站成一个圈,然后从1开始报数,每当数到".
    //"N的那一只猴子就出列,然后继续从下一个猴子开始又从1开始数,数到N的猴子继续出列,一直到最后".
    //"剩的猴子就是大王了。假如现在有M只猴子,报数数为N,请问第几只猴子是大王?列出选大王的过程。
                               int M = 10;
                int N = 3;
                List<int> monkeys = new List<int>();
                for (int i = 1; i <= M; i++)
                {
                    monkeys.Add(i);
                }
                int currentIndex = 0;
                while (true)
                {
                    for (int i = 1; i <= N; i++)
                    {
                        if (i == N)
                        {
                            monkeys.RemoveAt(currentIndex);
                            if (monkeys.Count == 1)
                            {
                                Console.WriteLine(monkeys[0]);
                                return;
                            }
                        }
                        currentIndex++;
                        if (currentIndex >= monkeys.Count)
                        {
                            currentIndex = 0;
                        }
                    }
                }
    29、a->b->c->d用链表的形式实现:d->c->b->a
    第一种方法:
    Stack<string> stack = new Stack<string>();
                stack.Push("a");
                stack.Push("b");
                stack.Push("c");
                stack.Push("d");
                List<string> list = new List<string>();
                list.AddRange(stack);
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(stack.Pop());
                }
    第二种方法:
    public partial class Item {
                    public string Value { get; set; }
                    public Item Prev { get; set; }
                    public Item Next { get; set; }
            }
      static void Main(string[] args)
                    {
                            //定义一个Item[]数组模拟链表
                            Item[] items =
                            {
                                    new Item() { Value = "a" }, 
                                    new Item() { Value = "b" }, 
                                    new Item() { Value = "c" }, 
                                    new Item() { Value = "d" }
                            };
                            //指定Next指向下一个
                            for (int i = 0; i < items.Length-1; i++)
                            {
                                    items[i].Next = items[i + 1];
                            }
                            //给Prev指定上一个
                            for (int i = items.Length - 1; i > 0; i--)
                            {
                                    items[i].Prev = items[i - 1];
                            }
                            //temp指向链表的第一个元素
                            Item temp = items[0];
                            //正序输出temp的value
                            while (temp!=null)
                            {
                                    Console.WriteLine(temp.Value);
                                    temp = temp.Next;
                            }
                            Console.WriteLine("---------倒序--------------");
                            //倒序输出temp的value
                            //将temp指向链表的末尾
                            temp = items[3];
                            while (temp!=null)
                            {
                                    Console.WriteLine(temp.Value);
                                    temp = temp.Prev;
                            }
                            Console.Read();
    30、求n的阶乘:
    public static int jie(int n)
            {         
                int result = 1;
                if (n <= 1)
                {
                    result= 1;
                }
                else
                {
                    result= n * jie(n - 1);
                }
                return result;
            }
    double result = 1;
                //第二种方法           
                for (int i = 1; i <= n; i++)
                {
                    result= result * i; 
                }
                return result;
    求1/1!+2/2!+3/3!+n/n!的和?
                    static double sum = 0;
            public static double add(int n)
            {
                int i;
                double sum = 0;
                for (i = 1; i <= n; i++)
                {
                    sum += i / jie(i);
                }
                return sum;
            }
    
            public static double jie(int n)
            {
                double result = 1;
                if (n <= 1)
                {
                    result= 1;
                }
                else
                {
                    result= n * jie(n - 1);
                }
                return result;
            }
    
    31、Jquery实现全选、反选
                $("#btnCheckAll").click(function () {
                                      var obj = $(':checkbox[name="song"]');
                    if (($(this).attr("checked")) == "checked") {
                        obj.attr("checked", true);
                    } else {
                        obj.attr("checked", false);
                    }
                });
                $("#btnCheckNoAll").click(function () {
                    $(':checkbox[name="song"]').attr("checked", false);
                });
                $("#btnNoCheckAll").click(function () {
                    var obj = $(':checkbox[name="song"]');
                    obj.each(function (k, v) {
                        $(this).attr("checked", !$(this).attr("checked"))
                    });
                });
    
    32、有1、23、4四个数字,编程实现能组成多少个互不相同且无重复数字的三位数?都是多少?
            static void Main(string[] args)
            {
                int count = 0;
                for (int i = 1; i < 5; i++)
                {
                    for (int j = 1; j < 5; j++)
                    {
                        for (int k = 1; k < 5; k++)
                        {
                            if (i != j && i != k && j != k)
                            {
                                count++;
                                Console.WriteLine(i * 100 + j * 10 + k);
                            }
                        }
                    }
                }
                Console.WriteLine("由1,2,3,4共可以组成"+count+"个互不相同且无重复数字的三位数!");
    33、计算字符串中每种字符出现的次数(面试题)。
            //Welcome to Chinaworld;不区分大小写,
            //打印W e l
            static void Main(string[] args)
            {
                string world = "1Welcome to Chinaworld";
                //将字符串转为小写形式
                world = world.ToLower();
                //建立泛型集合
                Dictionary<char,int> dict = new Dictionary<char,int>();
                //将字符串变成字符数组
                char[] chs = world.ToCharArray();
                //遍历每一个字符
                for (int i = 0; i < chs.Length; i++)
                {
                    //通过char的方法判断是否为字母,如果为字母,则进行下面的操作
                    if (char.IsLetter(chs[i]))
                    {
                        //如果不包含该键,则加入集合中
                        if (!dict.ContainsKey(chs[i]))
                        {
                            //将每个字符加入集合中对应键,其值初始为1                       
                            dict.Add(chs[i], 1);
                        }
                        else
                        {
                            //否则,包含该键,只将其对应的值+1即可
                            dict[chs[i]] = dict[chs[i]] + 1;
                        }
                    }
                }
                //循环遍历输出集合中的键 值 
                foreach (KeyValuePair<char, int> kv in dict)
                {
                    Console.WriteLine("字母:{0}出现了{1}次。",kv.Key,kv.Value);
    
                }
    34、从文件中读取字符串,编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并将压缩后的文件输出到另一文件中,
    例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"
                   //
    umber  
               //Matches the contents of the group of the same number. 第一个match的group。
                //1 是捕获组 ,就是第一个小括号内的值(从左向→)
                string str = "xxxyyybbyxyb";
                str = Regex.Replace(str, @"([a-zA-Z])1*", x =>
                {
                    string result = x.Groups[1].Value;
                    if (x.Length > 1)
                    {
                        result = x.Length + result;
                    }
                    return result;
                });
    27、存储过程语法,写分页存储过程
    语法:
    create proc[edure] usp_存储过程名
    参数名 类型名[=默认值] [output]
    ,参数名 类型名[=默认值] [output]
    ,。。。
    as
    begin
            脚本
    End
    
    --分页
               create proc usp_FenYe
            @pageIndex int, --当前第几页
            @pageSize int,  --每页显示几条
            @pageCount int output, --总页数
            @totalCount int output --总条数
            as
            begin   
               select * from
            (
                    select row_number() over(order by stuId) as num
                    ,* from Student
            ) as t
            where t.num between(@pageIndex-1)* @pageSize+1 and @pageIndex* @pageSize;
            // --计算总条数
             set @totalCount=(select count(0) from dbo.Students)
            // --计算总页数
             set @pageCount=ceiling(@totalCount*1.0/@pageSize)
             end
    
     --调用存储过程
            declare @pc int,@rc int
            exec usp_FenYe @pageindex=2,@pagesize=7,@pagecount=@pc output ,@totalCount=@rc output
  • 相关阅读:
    (二)处理视频
    vim下多行注释与解注释
    (一)读取显示图片
    解决死锁的方法
    死锁、活锁和饿死的理解(转)
    C# winfrom 窗体的StartPosition 属性
    Show()和ShowDialog()
    WinForm应用程序之注册模块的设计与实现
    Epplus使用教程1(基本介绍)
    C#中操作txt,抛出“正由另一进程使用,因此该进程无法访问此文件”
  • 原文地址:https://www.cnblogs.com/xiaoweigogo/p/7798145.html
Copyright © 2011-2022 走看看