C#面试题之一

下面是一个由*号组成的4行倒三角形图案。要求:1、输入倒三角形的行数,行数的取值3-21之间,对于非法的行数,要求抛出提示“非法行数!”;2、在屏幕上打印这个指定了行数的倒三角形。

             int n = 0; 
            Console.Write("输入倒三角形的行数,行数的取值3-21之间:"); 
            try 
            { 
                n = int.Parse(Console.ReadLine()); 
            }
            catch 
            { 


            } 
            if (n < 3 || n > 21) 
            { 
                Console.WriteLine("非法行数!"); return; 
            }
            for (int i = n; i >= 1; i--) 
            { 
                Console.WriteLine(new string(' ', n - i + 1) + new string('*', i * 2 - 1)); //new string(' ',数字)--重复指定输出前面符号的次数
            } 
            Console.ReadKey();


2、现有1~100共一百个自然数,已随机放入一个有98个元素的数组a[98]。要求写出一个尽量简单的方案,找出没有被放入数组的那2个数,并在屏幕上打印这2个数。注意:程序不用实现自然数随机放入数组的过程。

int[] arry = new int[98];
            Random rd = new Random();
            List<int> temp = new List<int>();
            //随机产生98个1-100自然数
            while (temp.Count < 98)
            {
                int result = rd.Next(1, 101);//随机产生1~100随机数;
                if (temp.Contains(result))
                {
                    continue;
                }
                temp.Add(result);
            }
            //将98个数填充到数组中;


            for (int i = 0; i < 98; i++)
            {
                arry[i] = temp[i];
            }
            Console.WriteLine("那98个数字为:");
            
            for (int i = 0; i < arry.Length; i++)
            {
                Console.Write(arry[i]+"、");
               
            }
            int[] arrytemp = new int[101];
            for (int i = 0; i < 98; i++)
            {
                arrytemp[arry[i]] = 1;//给98个自然数标识;arrytemp[2]=1
            }
            Console.WriteLine();
            Console.WriteLine("这两个数分别是:");
            for (int i = 1; i <= 100; i++)
            {
                if (arrytemp[i] == 1)
                {
                    continue;
                }
                Console.WriteLine(i);
            }
            Console.ReadKey();


3、一个文本文件含有如下内容:

4580616022644994|3000|赵涛

4580616022645017|6000|张屹

4580616022645090|3200|郑欣夏

上述文件每行为一个转账记录,第一列表示帐号,第二列表示金额,第三列表示开户人姓名。

创建一张数据库表(MS SQLServer数据库,表名和字段名自拟),请将上述文件逐条插入此表中。

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SqlClient;

using System.Diagnostics;


namespace 从文本中提取数据到数据库中

{

class Program

{


static void Main(string[] args)

{

//项目中的Programe.cs文件必须加上以下神奇的代码,对数据库的操作才能生效

string dataDir = AppDomain.CurrentDomain.BaseDirectory;

if (dataDir.EndsWith(@"\bin\Debug\")

|| dataDir.EndsWith(@"\bin\Release\"))

{

dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;

AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);

}


//启用秒表来计时

Stopwatch timer = new Stopwatch();

timer.Start();

string[] lines = System.IO.File.ReadAllLines(@"D:\转账记录.txt", Encoding.Default);

for (int i = 0; i < lines.Length; i++)

{

string[] str=lines[i].Split('|');


using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;

AttachDBFilename=|DataDirectory|\ZhuanZhang.mdf;Integrated Security=True;User Instance=True"))

{

conn.Open();

using (SqlCommand cmd = conn.CreateCommand())

{

cmd.CommandText = "Insert into T_ZhuanZhang (CardNum,Money,Name) values (@CardNum,@Money,@Name)";

cmd.Parameters.Add(new SqlParameter("CardNum", str[0]));

cmd.Parameters.Add(new SqlParameter("Money", str[1]));

cmd.Parameters.Add(new SqlParameter("Name", str[2]));

cmd.ExecuteNonQuery();

}

}

}


Console.WriteLine("数据导入成功!");

timer.Stop();

Console.WriteLine(timer.Elapsed);

Console.ReadKey();

}

}

}

//相传有一群猴子要选出大王,它们采用的方式为:所有猴子站成一个圈,然后从1开始报数,每当数到".

//"N的那一只猴子就出列,然后继续从下一个猴子开始又从1开始数,数到N的猴子继续出列,一直到最后".

//"剩的猴子就是大王了。假如现在有M只猴子,报数数为N,请问第几只猴子是大王?列出选大王的过程。

int M = 10;

int N = 3;

List<int> monkeys =newList<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;

}

}

}

4、一个文本文件含有如下内容,分别表示姓名和成绩:

张三 90

李四 96

王五 78

赵六 82

 

提供用户一个控制台界面,允许用户输入要查询的姓名,输入姓名并且按回车以后,打印出此人的成绩,如果不输入姓名直接按回车则显示所有人的姓名以及成绩。(注意:不能使用数据库)

 Console.Write("请输入姓名:");
            string name = Console.ReadLine();
            string[] strs = File.ReadAllLines(@"d:\1.txt",Encoding.Default);


            foreach (string item in strs)
            {
                if (name != "")
                {
                    if (name==item.Split(' ')[0])
                    {
                        Console.WriteLine("分数为"+item.Split(' ')[1]);
                        break;
                    }
                    
                }
                else
                {
                    Console.WriteLine(item);
                }
            }


            
            Console.ReadKey();


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值