C#递归

    技术2022-05-13  10

    using System;using System.Collections.Generic;using System.Text;

    namespace E6{    class F    {        public int Fac(int i)        {            int f = 0;            if (i == 1)            {                return 1;            }            else            {                f = i * Fac(i - 1);            }            return f;        }        public string reverse(string test)        {            if (test.Length==1)            {                return test;            }            else            {                string result = test.Substring(test.Length - 1,1);                result += reverse(test.Substring(0, test.Length - 1));                return result;            }        }    }    class Program    {               static void Main(string[] args)        {            Console.WriteLine("输入一个数");            int a = int.Parse(Console.ReadLine());            F myf = new F();            int b = myf.Fac(a);            Console.WriteLine("这个数的阶乘是:{0}", b);            Console.WriteLine();            Console.WriteLine("输入字符串");            string c = string.Empty;            c = Console.ReadLine();            Console.WriteLine("逆转为:{0}",myf.reverse(c));            Console.ReadLine();

            }    }}


    最新回复(0)