任务49:148-数组的定义和初始化
    数组

    • image.png

    任务50:149-使用for循环,while循环和foreach来遍历数组
    数组的遍历

    • foreach
      1. int[] array = { 1, 2, 4, 8 };
      2. foreach (var i in array)
      3. {
      4. Console.WriteLine(i);
      5. }
      任务51:150-字符串的处理
      字符串的处理
      1. string str = "asdfghj";
      2. for (int i = 0; i < str.Length; i++) // .length 访问长度
      3. {
      4. Console.Write(str[i]); // [i] 访问第i+1个字符
      5. }
      split方法
      1. string str = "fdasdfghjasdsaddd";
      2. string[] strArray = str.Split('a'); //把字符串拆分成数组
      任务52:151-练习1和2 练习for循环
      练习1:找出水仙花数
      见“算法”栏目(略)
      练习2:3个可乐瓶换一瓶可乐,x瓶可乐一共可以喝多少瓶
      1. int start = Convert.ToInt32(Console.ReadLine());
      2. int total = start;
      3. int empty = start;
      4. while (empty > 1)
      5. {
      6. if (empty % 3 == 0)
      7. {
      8. start = empty / 3;
      9. total = total + start;
      10. empty = start;
      11. }
      12. else if (empty % 3 == 1)
      13. {
      14. start = (empty - 1) / 3;
      15. total = total + start;
      16. empty = start + 1;
      17. }
      18. else if (empty % 3 == 2)
      19. {
      20. start = (empty + 1) / 3;
      21. total = total + start;
      22. empty = start - 1;
      23. }
      24. }
      25. Console.WriteLine(total);
      任务53:152-练习3和4 循环结构练习和字符操作
      练习3:用户猜1-50的数字
      1. Random random = new Random();
      2. int x = random.Next(1, 51);
      3. Console.WriteLine("我想好了,你猜吧(范围1-50)");
      4. int y;
      5. while (true)
      6. {
      7. y = Convert.ToInt32(Console.ReadLine());
      8. if (y > x)
      9. {
      10. Console.WriteLine("太大了,继续猜");
      11. }
      12. else if (y < x)
      13. {
      14. Console.WriteLine("太小了,继续猜");
      15. }
      16. else
      17. {
      18. break;
      19. }
      20. }
      21. Console.WriteLine("猜对了,答案就是{0}", x);
      练习4:对输入的字符串进行加密
      1. string input = Console.ReadLine();
      2. string output = "";
      3. for (int i = 0; i < input.Length; i++)
      4. {
      5. if (input[i] >= 'a' && input[i] <= 'z')
      6. {
      7. int before = input[i];
      8. before = before + 3;
      9. char after = (char)before; //显示转换
      10. if (after > 'z') //将xyz转换为abc
      11. {
      12. after = (char)(after - 26);
      13. }
      14. output = output + after;
      15. }
      16. else if (input[i] >= 'A' && input[i] <= 'Z')
      17. {
      18. int before = input[i];
      19. before = before + 3;
      20. char after = (char)before; //显示转换
      21. if (after > 'Z')
      22. {
      23. after = (char)(after - 26);
      24. }
      25. output = output + after;
      26. }
      27. else
      28. {
      29. output = output + input[i];
      30. }
      31. }
      32. Console.WriteLine(output);
      任务54:153-练习5 对数组进行排序,Array.Sort和冒泡排序算法
      练习5:对用户输入的一串数字进行排序
      1. string str = Console.ReadLine(); //要求用户输入一组整数,以空格分隔
      2. string[] strArray = str.Split(' '); //将用户输入的一串字符转变为字符组
      3. int[] numArray = new int[strArray.Length]; //将字符组转变为数组
      4. for (int i = 0; i < strArray.Length; i++)
      5. {
      6. int temp = Convert.ToInt32(strArray[i]);
      7. numArray[i] = temp;
      8. }
      9. Array.Sort(numArray); //使用CLR给我们提供的方法进行排序(这个方法其实使用了快速排序算法)
      10. for (int i = 0; i < numArray.Length; i++)
      11. {
      12. Console.Write(numArray[i] + " ");
      13. }
      任务55:154-练习6和7 循环练习
      练习6:用户输入n,输出n次循环i=(i+1)*2的结果,i初始值为1
      1. double x = 1;
      2. int n = Convert.ToInt32(Console.ReadLine());
      3. for (int i = 1; i <= n; i++)
      4. {
      5. x = (x + 1) * 2;
      6. }
      7. Console.WriteLine(x);
      练习7:略

    任务56:155-练习8 循环练习
    练习8:用户输入一个整数,将整数依序插入到从小到大排序的数组中
    比如{0,2,4,6,8,10,12,14,16,18,20}

    1. int[] OldArray = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
    2. int[] NewArray = new int[OldArray.Length + 1];
    3. Console.WriteLine("Please input integer");
    4. int x = Convert.ToInt32(Console.ReadLine());
    5. bool Trigger = true;
    6. if (x <= OldArray[0]) //检测x是否为最小值
    7. {
    8. NewArray[0] = x;
    9. Trigger = false;
    10. }
    11. else if (x >= OldArray[OldArray.Length - 1]) //检测x是否为最大值
    12. {
    13. NewArray[NewArray.Length - 1] = x;
    14. Trigger = false;
    15. }
    16. for (int i = 0; i <= OldArray.Length - 1; i++)
    17. {
    18. //检测x是否在某两个数之间,运行这段代码的前提是x不为最大值或最小值
    19. if (Trigger == true && x >= OldArray[i] && x <= OldArray[i + 1])
    20. {
    21. NewArray[i] = OldArray[i];
    22. NewArray[i+1] = x;
    23. }
    24. else if (x > OldArray[i])
    25. {
    26. NewArray[i] = OldArray[i];
    27. }
    28. else if (x < OldArray[i])
    29. {
    30. NewArray[i + 1] = OldArray[i];
    31. }
    32. }
    33. for (int i = 0; i <= NewArray.Length - 1; i++)
    34. {
    35. Console.Write(NewArray[i] + " ");
    36. }

    别人写的:

    1. int[] num = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
    2. int x = Convert.ToInt32(Console.ReadLine());
    3. int[] num2 = new int[num.Length + 1];
    4. int i = 0, j = 0;
    5. for (i = 0; i < num.Length && num[i] < x; ++i)
    6. {
    7. num2[j++] = num[i];
    8. }
    9. num2[j++] = x;
    10. for (; i < num.Length; ++i)
    11. {
    12. num2[j++] = num[i];
    13. }
    14. Console.WriteLine("原数组:");
    15. for (i = 0; i < num.Length; ++i)
    16. {
    17. Console.Write(num[i] + " ");
    18. }
    19. Console.WriteLine();
    20. Console.WriteLine("插入数: " + x);
    21. Console.WriteLine("新数组:");
    22. for (i = 0; i < num2.Length; ++i)
    23. {
    24. Console.Write(num2[i] + " ");
    25. }
    26. Console.WriteLine();

    任务57:156-练习9和10 循环练习和字符串与字符的处理
    练习9:用户输入一个整数,输出该数被256,43,1拆分的结果

    1. Console.WriteLine("Please input an integer");
    2. int x = Convert.ToInt32(Console.ReadLine());
    3. int a = x / 256;
    4. int b = (x - 256 * a) / 43;
    5. int c = (x - 256 * a - 43 * b);
    6. Console.WriteLine("{0}={1}*256+{2}*43+{3}*1", x, a, b, c);

    练习10:输入一个字符串,判断是否是C#的合法标识符
    (略)

    任务58:157-练习11和12 循环练习和字符串处理
    练习11:判断用户输入的字符串是否为回文

    1. Console.WriteLine("请输入一个回文");
    2. string input = Console.ReadLine();
    3. bool temp = true;
    4. for (int i = 0; i <= input.Length - 1; i++)
    5. {
    6. if (input[i] == input[input.Length - 1 - i])
    7. {
    8. }
    9. else
    10. {
    11. Console.WriteLine("你输入的不是回文");
    12. temp = false;
    13. break;
    14. }
    15. }
    16. if (temp == true)
    17. {
    18. Console.WriteLine("你输入的是回文");
    19. }

    练习12:校验一串密码是否符合密码安全规则(略)