VR引擎第五、六课

86 阅读1分钟
  1. 字符串数组转换成整型数组: string m = Console.ReadLine(); string[] mp=m.Split(' '); int[] intmp=new int[mp.Length]; int temp = 0; for(int i=0; i<intmp.Length; i++) { intmp[i] = Convert.ToInt32(mp[i]); } 数组输出:Console.WriteLine(string.Join(" ",数组名)); 数组排序:Array.Sort(数组名); 2.作业: using System; namespace basketball { internal class Program { static void Main(string[] args) { Console.WriteLine("输入字符串:"); /* string text = Convert.ToString(Console.ReadLine()); string[]text1= text.Split(' '); string t1 = "b a s k e t b a l l"; string[] t2 = t1.Split(' ');*/ char[] text1 = Console.ReadLine().ToCharArray(); string t1 = "basketball"; char[] t2 = t1.ToCharArray(); int[] text2 = new int[t2.Length]; int sum = 0; int res; for (int i = 0; i < t2.Length; i++) { for(int j = 0; j < text1.Length; j++) { if (t2[i] == text1[j]) { sum++; }
    } text2[i] = sum; sum = 0;

         }
         Console.WriteLine(string.Join(" ", text2));
         text2[0] = text2[0] / 2;
         text2[1] = text2[1] / 2;
         text2[6] = text2[6] / 2;
         text2[7] = text2[7] / 2;
         text2[8] = text2[8] / 2;
         text2[9] = text2[9] / 2;
         Array.Sort(text2);
         Console.WriteLine(text2[0]);
     }
    

    } }

函数:减少代码重复,对此进行封装,函数执行完可直接释放,不需要等程序全部执行完,节省空间 可以放在类里面 static 修饰符,表示静态函数 void表示返回类型,返回值为空的类型是void 当然返回类型也可以是别的eg:int bool string 函数名(形参1,形参2....)形参若要赋值要放到后面,放前面可能会出错,会报错 static int Max(int num) 4. 递归:函数中调用自己 做法:先找关系式,再找出口;
f(n)=f(n-1)+f(n-3)=f(n-2)+f(n-3)+f(n-4)+f(n-5) 给一个出口 f(4)=f(3)+f(1) f(n)=f(n+1)+f(n+2) f(10)=1 f(9)=1,故可以推出f8 eg:斐波那契数列 static int fi(int num) { if (num == 1 || num == 2) return 1; return fi(num - 1)+fi(num - 2);

        }
        int num = Convert.ToInt32(Console.ReadLine());
        for(int i = 1; i <= num; i++)
        {
            Console.Write(fi(i) + " ");
        }