人类思维--计算机逻辑思维

逻辑思维--代码实现

写书法:

描红——临摹——碑贴——自成一体——草

章节复习:

数组:一维,二维,多维

一维:豆角。连续,同一类型。

定义:数据类型[] 数组名=new 数据类型[长度]{.,.,.,.};

赋值:数组名[下标] = 值

取值:数组名[下标]

灵活运用:与for循环的结合应用。

1.求最大值,最小值。

2.求总和,平均。

3.随机(生成下标)抽值。

新课:

数组的应用:

(一).冒泡排序。

1.冒泡排序是用双层循环解决。外层循环的是趟数,里层循环的是次数。

2.趟数=n-1;次数=n-趟数。

3.里层循环使用if比较相临的两个数的大小,进行数值交换。

作业:

1.先把冒泡排序写一遍。

2.使用冒泡排序,做青歌赛的打分程序。要求去掉两个最高,两个最低分,求平均得分。

代码。

(二).折半查找。

前提:数组必须是有序的。

思路:用两个变量分别代表上限(top)和下限(bottom)的下标,再用一个变量代表中间(mid)的下标。

1.求中间下标:mid = (top+bottom)/2

2.上限下标下移:top = mid+1. 假设数组是升序排列。

3.下限下标上移:bottom = mid-1;

4.循环条件是:bottom>=top

        static void Main(string[] args)

        {

            int[] a = new int[] { 3, 5, 7, 9, 11, 13, 14, 18 };

            Console.Write("请输入要找的数:");

            int find = Convert.ToInt32(Console.ReadLine());

            int top, bottom, mid;  //上限下标,下限下标,中间下标

            top = 0;

            bottom = a.Length - 1;

            while(bottom>=top)  //只要下限下标还在上限下标的下面,就循环,否则没找到就结束。

            {

                //算中间下标

                mid = (top + bottom) / 2;

                //取中间的值

                int n = a[mid];

                if(n < find)

                {

                    top = mid + 1;      //调整上限的下标

                }

                else if(n>find)

                {

                    bottom = mid - 1;// 调整下限的下标。

                }

                else

                {

                    Console.WriteLine("找到了,在第" + mid + "个元素上");

                    break;

                }

            }

        }

二维数组:

表格的模型。

定义:

数据类型[,] 数组名 = new 数组类型[维度长度,维度长度];

int[,] a = new int[3,4];

int[,] a = new int[3, 4] { { 1, 2, 3, 4 },{ 5, 6, 7, 8 }, { 9, 0, 9, 8 } };

赋值:

数组名[下标,下标] = 值;

a[0,0] = 5;

a[2,3] = 10;

取值:

数组名[下标,下标];

应用:

例:这是冒泡排序

static void ccc(string[] args)

        {

int[] a = new int[8] { 9, 12, 7, 5, 15, 2, 1, 8 };

//冒泡排序。

for(int i=1;i<=a.Length-1;i++) //趟数

            {

for (int j = 1; j <= a.Length - i; j++)//次数

                {

if(a[j-1] > a[j])

                    {

int t = a[j - 1];

                        a[j - 1] = a[j];

                        a[j] = t;

                    }

                }

            }

//显示

for(int k=0;k<a.Length;k++)

            {

Console.WriteLine(a[k]);

            }

        }

例:折半查找

static void Man(string[] args)

        {

int[] a = new int[] { 3, 5, 7, 9, 11, 13, 14, 18 };

Console.Write("请输入要找的数:");

int find = Convert.ToInt32(Console.ReadLine());

int top, bottom, mid;  //上限下标,下限下标,中间下标

            top = 0;

            bottom = a.Length - 1;

while(bottom>=top)  //只要下限下标还在上限下标的下面,就循环,否则没找到就结束。

            {

//算中间下标

                mid = (top + bottom) / 2;

//取中间的值

int n = a[mid];

if(n < find)

                {

                    top = mid + 1;      //调整上限的下标

                }

else if(n>find)

                {

                    bottom = mid - 1;// 调整下限的下标。

                }

else

                {

Console.WriteLine("找到了,在第" + mid + "个元素上");

break;

                }

            }

        }

例;二维数组

static void Main (string[] args)

        {   int[,] a = new int[3, 4];

//输入

for(int i=0;i<3;i++)

            {

//自动生成学号

                a[i, 0] = i+1;

//语文成绩

Console.Write("语文:");

                a[i, 1] = Convert.ToInt32(Console.ReadLine());

//数学成绩

Console.Write("数学:");

                a[i, 2] = Convert.ToInt32(Console.ReadLine());

//计算总分

                a[i, 3] = a[i, 1] + a[i, 2];

            }

//显示

Console.WriteLine("学号\t语文\t数学\t总分");

for(int i=0;i<3;i++)

            {

for(int j=0;j<4;j++)

               {

Console.Write(a[i, j] + "\t");

               }

Console.WriteLine();

           }

        }

最新文章

  1. Which language is best, C, C++, Python or Java?什么编程语言最好
  2. MapReduce Shuffle过程
  3. Android中shell命令语句
  4. Swift 本地推送通知UILocalNotification
  5. 北大ACM(POJ1008-Maya Calendar)
  6. URI、URL以及URN的区别
  7. poj3259
  8. 两个不同于LR和jmeter的性能测试工具
  9. tomcat修改默认web目录
  10. Web Api 2(Cors)Ajax跨域访问
  11. andriod手机签到应用服务器架构
  12. 模拟SPI协议时序
  13. sql备份(导出脚本)
  14. iOS图形手势识别框架SGGestureRecognizer
  15. LeetCode OJ 102. Binary Tree Level Order Traversal
  16. 能够在Linux系统中运行的5款大型耐玩游戏
  17. 017-通过govendor管理依赖包
  18. spring boot 整合案例
  19. Visualizing CNN Layer in Keras
  20. [Node.js]Express web框架

热门文章

  1. H-Index,H-Index II
  2. I - Long Distance Racing(第二季水)
  3. mysql三张表关联查询
  4. mysql的分页存储过程,能够传出总记录数
  5. Hadoop配置文件-hdfs-site.xml
  6. 配置nginx静态资源路径
  7. JS delete 用法(删除对象属性及变量)
  8. 在Struts2中使用poi进行excel操作下载的时候报getOutputStream() has already been called for this response 错误 [转]
  9. javascript中with语句应用
  10. Oracle左连接、右连接、全外连接