美文网首页
c#Lesson04

c#Lesson04

作者: Class_Lee | 来源:发表于2018-01-09 17:12 被阅读0次

using System;

namespace Day4
{
class MainClass
{
public static void Main (string[] args)
{

        //第一种方法
        //          int temp = 0;
        //          int count = 1;
        //          while (count <= 100) {
        //              if (count % 2 != 1) {
        //                  count++;
        //                  continue;
        //              }
        //              temp += count;
        //              count++;
        //          }
        //          Console.WriteLine (temp);
        //第二种方法
        //          int count = 1;
        //          int temp = 0;
        //          while (count <= 100) {
        //              if (count % 2 == 1) {
        //                  temp += count;
        //              }
        //              count++;
        //          }
        //          Console.WriteLine (temp);
        //          第三种方法
        //          int sum = 0;
        //          for (int i = 1; i <= 99; i++) {
        //              if (i % 2 == 1) {
        //                  sum += i;
        //              }
        //          }
        //          Console.WriteLine (sum);
        //       //第四种方法
        //          int sum = 0; 
        //          for (int i = 1; i <= 99; i += 2) {
        //              sum += i;
        //          }
        //
        //          Console.WriteLine (sum);
        //          for (int i = 10;; i--) {
        //              Console.WriteLine ("lanou");
        //          }
        //          int x = 0;
        //          while(){
        //              Console.WriteLine ("hahha");
        //              x++;
        //          }
        //循环嵌套
        //外层循环
        //          int n = 1;
        //          for (int i = 0; i < 3; i++) {
        //              //里层循环
        //              for (int j = 0; j < 3; j++) {
        //                  Console.Write (n++);
        //              }
        //              Console.WriteLine ();
        //          }
        //外层循环:行数
        //          for (int i = 0; i < 3; i++) {
        //              //里层循环:列数
        //              for (int j = 0; j < i + 1; j++) {
        //                  Console.Write ("*");
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 3; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write (j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          Console.WriteLine ();
        //          for (int i = 0; i < 3; i++) {
        //              for (int j = 0; j < 3 - i; j++) {
        //                  Console.Write ("*");
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //              Console.Write (j + "*" + i + "=" + i * j + "    \t");
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }

        //每只猴子的和加起来不是桃子总数  这种方法是错的
        //每只猴子拿的桃子数量 不等于 全部的桃子总数
        //          for (int x = 10; x < 30000; x++) {
        //              int a = (x - 1) / 5 + 1;
        //              int b = (x - a - 1) / 5 + 1;
        //              int c = (x - a - b - 1) / 5 + 1;
        //              int d = (x - a - b - c - 1) / 5 + 1;
        //
        //              int f = (x - a - b - c - d - 1) / 5 + 1;
        //              if (x == a + b + c + d + f) {
        //                  Console.WriteLine (x);
        //              }
        //          }
        //          int count = 0;
        //          int a = 0;
        //          int b = 0;
        //          int c = 0;
        //          int d = 0;
        //          while (true) {
        //              count++;
        //              a = (count - 1) / 5 * 4;
        //              b = (a - 1) / 5 * 4;
        //              c = (b - 1) / 5 * 4;
        //              d = (c - 1) / 5 * 4;
        //              if (count % 5 == 1 && a % 5 == 1 && b % 5 == 1 && c % 5 == 1 && d % 5 == 1) {
        //                  break;
        //              }
        //          }
        //          Console.WriteLine (count);


        /*
        //练习
        //前后左右姓名 
        //string 静态定义
        //第一种
        int[] arr = { 1, 1, 3, 5, -6, 4, 9, 9, 2 };
        Array.Sort (arr);
        Console.WriteLine (arr [6]);
        //第二种  错误
        int[] arr = { 1, 1, 3, 5, 6, 4, 9, 9, 2 };
        int temp;
        for (int i = 0; i < arr.Length; i++) {
            if (arr [i] > temp) {
                temp = arr [i];
            }
        }
        Console.WriteLine (temp);
        //第三种
        int[] newArray = { 1, 3, 5, 6, 4, 9, 2 };
        int max = newArray [0];
        for (int i = 0; i < newArray.Length; i++) {
            //当前保存的值不如刚查到的大
            if (max < newArray [i]) {
                max = newArray [i];
            }
        }
        Console.WriteLine (max);
        //第四种
        int[] newArray = { 1, 3, 5, 6, 4, 9, 2 };
        for (int i = 0; i < newArray.Length - 1; i++) {
            for (int j = 0; j < newArray.Length - i - 1; j++) {
                if (newArray [j] > newArray [j + 1]) {
                    int temp = newArray [j];
                    newArray [j + 1] = newArray [j];
                    newArray [j] = temp;
                }
            }
        }
        Console.WriteLine (newArray [newArray.Length - 1]);
        //第五种
        int[] newArray = { 1, 3, 5, 6, 4, 9, 2 };
        int maxIndex = 0;
        int i = 0;
        for (i = 0; i < newArray.Length; i++) {
            if (newArray [maxIndex] < newArray [i]) {
                maxIndex = i;
            }
        }
        Console.WriteLine (newArray [maxIndex]);

        ////

        //数据的默认值
        //整型 0
        //浮点型 0.0
        //布尔 false
        //string null
        */


        //九九乘法表
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //          for (int i = 1; i <= 9; i++) {
        //              for (int j = 1; j <= i; j++) {
        //                  Console.Write ("{0}*{1}={2}\t", j, i, i * j);
        //              }
        //              Console.WriteLine ();
        //          }
        //

        // 算法  二维数组
        // 数据结构
        // 数组  (顺序表)
        // 冒泡排序
        // 常见的排序算法: 快速排序  选择排序(平常我们经验都是这么选的) 插入排序 堆排序 希尔排序
        // 时间复杂度 空间复杂度 评判算法好坏的标准
        // 条件 数组
        // 升序  从小到大  降序(从大到小)
        //          int[] arr = new int[5]{ 12, 9, 3, 6, 1 };


        //第一种方法 冒泡排序
        //外层循环 轮数
        //          int[] array = { 12, 9, 3, 6, 1 };
        //          Array.Sort (array);
        //          for (int i = 1; i <= array.Length - 1; i++) {
        //              //里层循环 每一轮比较的次数
        //              for (int j = 0; j < array.Length - i; j++) {
        //                  // 判断前一个和后一个谁大,如果前一个大就交换
        //                  if (array [j] > array [j + 1]) {
        //                      // 交换
        //                      int temp = array [j];
        //                      array [j] = array [j + 1];
        //                      array [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          for (int i = 0; i < array.Length; i++) {
        //              Console.WriteLine (array [i]);
        //          }
        //思考题: 两种写法有什么不一样
        //          int[] array = { 12, 9, 3, 6, 1 };
        //          for (int i = 0; i < array.Length - 1; i++) {
        //              for (int j = 0; j < array.Length - i - 1; j++) {
        //                  if (array [j] > array [j + 1]) {
        //                      int temp = array [j];
        //                      array [j] = array [j + 1];
        //                      array [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in array)
        //              Console.Write ("\t" + x);
        //  1.
        //          int[] arr = { 12, 9, 3, 6, 1 };
        //          for (int i = 0; i < arr.Length - 1; i++) {
        //              for (int j = 0; j < arr.Length - i - 1; j++) {
        //                  if (arr [j] > arr [j + 1]) {
        //                      int temp = arr [j];
        //                      arr [j] = arr [j + 1];
        //                      arr [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in arr) {
        //              Console.Write (x + "\t");
        //          }
        //          2.
        //          int[] array = { 12, 9, 3, 6, 1 };
        //          for (int i = 0; i < array.Length - 1; i++) {
        //              for (int j = 0; j < array.Length - i - 1; j++) {
        //                  if (array [j] > array [j + 1]) {
        //                      int temp = array [j];
        //                      array [j] = array [j + 1];
        //                      array [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in array) {
        //              Console.Write (x + "\t");
        //          }
        //          3.
        //          int[] array = { 4, 9, 3, 6, 12 };
        //          for (int j = 0; j < array.Length - 1; j++) {
        //              for (int i = 0; i < array.Length - 1-i; i++) {
        //                  if (array [i] > array [i + 1]) {
        //                      int temp = array [i];
        //                      array [i] = array [i + 1];
        //                      array [i + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in array) {
        //              Console.Write (x + "\t");
        //          }
        //          第二种排序  选择排序
        //          int[] arr = new int[5]{ 12, 9, 3, 6, 1 };
        //          for (int i = 0; i < arr.Length - 1; i++) {
        //              for (int j = 1 + i; j < arr.Length; j++) {
        //                  if (arr [i] > arr [j]) {
        //                      int temp = arr [i];
        //                      arr [i] = arr [j];
        //                      arr [j] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in arr) {
        //              Console.Write (x + "\t");
        //          }


        /*
        // 二维数组
        // 行 列
        // 静态
        //          int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 } };
        //          // 动态
        ////            int[,] array = new int[5, 4];
        //          // 两层for循环
        //          // 外层循环 负责行
        //          // 里层循环 负责列
        //          for (int i = 0; i < arr.GetLength (0); i++) {
        //              for (int j = 0; j < arr.GetLength (1); j++) {
        //                  Console.Write (arr [i, j] + " ");
        //              }
        //              Console.WriteLine ();
        //          }
        //          Console.WriteLine (arr.Rank);
        // arr.Rank 指的是当前是几维数组  rank:秩,数组维数
        // arr.x 后面有些是属性 有些是方法
        // getLength() 获取每个维度上的元素的个数
        // 取值范围 0 ~ Rank-1
        //          Console.WriteLine (arr.GetLength (1));
        // 使用二维数组 保存你前后桌人的姓名
        //          string[,] array = { { "宋云龙", "我", "孙宇瑶" }, { "冯建森", "刘佳", "吴多义" }, { "张三", "李四", "王八" } };
        //          for (int i = 0; i < array.GetLength (0); i++) {
        //              for (int j = 0; j < array.GetLength (1); j++) {
        //                  Console.Write (array [i, j] + "\t");
        //              }
        //              Console.WriteLine ();
        //          }

        //电影院选座
        //          Console.WriteLine ("请输入电影院规模(整数):");
        //          int n = int.Parse (Console.ReadLine ());
        //          int[,] seatArray = new int[n, n];
        //          bool isBegin = true;
        //          while (isBegin) {
        //              Console.WriteLine ("*************************");
        //              Console.WriteLine ("*\t1.选座  \t*");
        //              Console.WriteLine ("*\t2.退座  \t*");
        //              Console.WriteLine ("*\t3.查看  \t*");
        //              Console.WriteLine ("*\t0.退出  \t*");
        //              Console.WriteLine ("*************************");
        //              Console.WriteLine ("请输入");
        //              string numStr = Console.ReadLine ();
        //              // 补充
        //              switch (numStr) {
        //              case "1":
        //                  Console.WriteLine ("执行选座功能");
        //                  Console.Write ("请选择行:");
        //                  int row = int.Parse (Console.ReadLine ());
        //                  row--;
        //                  Console.Write ("请选择列:");
        //                  int column = int.Parse (Console.ReadLine ());
        //                  column--;
        //                  // 用户输入
        //                  if (row <= n && column <= n) {
        //                      if (seatArray [row, column] == 0) {
        //                          seatArray [row, column] = 1;
        //
        //                          for (int i = 0; i < seatArray.GetLength (0); i++) {
        //                              for (int j = 0; j < seatArray.GetLength (1); j++) {
        //                                  Console.Write (seatArray [i, j]);
        //                              }
        //                              Console.WriteLine ();
        //                          }
        //                      } else {
        //                          Console.WriteLine ("该座位已经有人,请重新选择!");
        //                      }
        //                  } else {
        //                      Console.WriteLine ("输入数值过大!");
        //                  }
        //                  // 检测1.是否越界 2.查重
        //                  // 设置数组
        //                  break;
        //              case "2":
        //                  Console.WriteLine ("执行退座功能");
        //                  Console.Write ("请选择行:");
        //                  int row1 = int.Parse (Console.ReadLine ());
        //                  row1--;
        //                  Console.Write ("请选择列:");
        //                  int column1 = int.Parse (Console.ReadLine ());
        //                  column1--;
        //                  // 用户输入
        //                  if (row1 <= n && column1 <= n) {
        //                      if (seatArray [row1, column1] != 0) {
        //                          seatArray [row1, column1] = 0;
        //                          for (int i = 0; i < seatArray.GetLength (0); i++) {
        //                              for (int j = 0; j < seatArray.GetLength (1); j++) {
        //                                  Console.Write (seatArray [i, j]);
        //                              }
        //                              Console.WriteLine ();
        //                          }
        //                      } else {
        //                          Console.WriteLine ("该座位没有人,无法退票!");
        //                      }
        //                  } else {
        //                      Console.WriteLine ("输入数值过大!");
        //                  }
        //                  break;
        //              case "3":
        //                  Console.WriteLine ("当前座位图");
        //                  for (int i = 0; i < seatArray.GetLength (0); i++) {
        //                      for (int j = 0; j < seatArray.GetLength (1); j++) {
        //                          Console.Write (seatArray [i, j]);
        //                      }
        //                      Console.WriteLine ();
        //                  }
        //                  break;
        //              case "0":
        //                  Console.WriteLine ("欢迎下次光临");
        //                  isBegin = false;
        //                  break;
        //              default:
        //                  Console.WriteLine ("输入有误,请重新输入");
        //                  break;
        //              }
        //          }
        */

        /*
        //作业 十遍
        //          int[] arr = { 12, 4, 6, 7, 1, 9 };

        //          for (int i = 0; i < arr.Length - 1; i++) {
        //              for (int j = 0; j < arr.Length - i - 1; j++) {
        //                  if (arr [j] > arr [j + 1]) {
        //                      int temp = arr [j];
        //                      arr [j] = arr [j + 1];
        //                      arr [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in arr) {
        //              Console.WriteLine (x);
        //          }

        //          for (int i = 0; i < arr.Length - 1; i++) {
        //              for (int j = 0; j < arr.Length - 1 - i; j++) {
        //                  if (arr [j] > arr [j + 1]) {
        //                      int temp = arr [j];
        //                      arr [j] = arr [j + 1];
        //                      arr [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in arr) {
        //              Console.WriteLine (x);
        //          }
        //
        //          for (int i = 0; i < arr.Length - 1; i++) {
        //              for (int j = 0; j < arr.Length - i - 1; j++) {
        //                  if (arr [j] > arr [j + 1]) {
        //                      int temp = arr [j];
        //                      arr [j] = arr [j + 1];
        //                      arr [j + 1] = temp;
        //
        //                  }
        //              }
        //          }
        //          foreach (int x in arr) {
        //              Console.WriteLine (x);
        //          }
        //
        //          for (int i = 0; i < arr.Length - 1; i++) {
        //              for (int j = 0; j < arr.Length - i - 1; j++) {
        //                  if (arr [j] > arr [j + 1]) {
        //                      int temp = arr [j];
        //                      arr [j] = arr [j + 1];
        //                      arr [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in arr) {
        //              Console.WriteLine (x);
        //          }

        //          for (int i = 0; i < arr.Length - 1; i++) {
        //              for (int j = 0; j < arr.Length - i - 1; j++) {
        //                  if (arr [j] > arr [j + 1]) {
        //                      int temp = arr [j];
        //                      arr [j] = arr [j + 1];
        //                      arr [j + 1] = temp;
        //                  }
        //              }
        //          }
        //          foreach (int x in arr) {
        //              Console.WriteLine (x);
        //          }
        //          int[] array = { 12, 9, 3, 6, 1 };
        //                      for (int i = 0; i < array.Length - 1; i++) {
        //                          for (int j = 0; j < array.Length - i - 1; j++) {
        //                              if (array [j] > array [j + 1]) {
        //                                  int temp = array [j];
        //                                  array [j] = array [j + 1];
        //                                  array [j + 1] = temp;
        //                              }
        //                          }
        //                      }
        //
        */


        //
        //          float f = 12.5f;
        //          int x = (int)f;
        //          Console.WriteLine (x);
        //
    }
}

}

相关文章

  • c#Lesson04

    using System; namespace Day4{class MainClass{public stati...

网友评论

      本文标题:c#Lesson04

      本文链接:https://www.haomeiwen.com/subject/hzrgnxtx.html