编写一个方法用于验证指定的字符串是否为反转字符,返回true和false。请用递归算法实现

编写一个方法用于验证指定的字符串是否为反转字符,返回true和false。请用递归算法实现。(反转字符串样式为"abcdedcba")

Code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace RecursionAlgorithms  
  7. {  
  8.     class Program  
  9.     {  
  10.         private static bool fn1(ref string str, ref int from, ref int to)  
  11.         {  
  12.             if (from>=to)  
  13.             {  
  14.                 return true;  
  15.             }  
  16.             if (str[from++]!=str[to--])  
  17.             {  
  18.                 return false;  
  19.             }  
  20.             return fn1(ref str,ref from, ref to);  
  21.         }  
  22.         private static int fn2(int i)  
  23.         {  
  24.             return i <= 2 ? 1 : fn2(i - 2) + fn2(i - 1);  
  25.         }  
  26.         private static long fn3(long x, ref long n)  
  27.         {  
  28.             return (x <= 1) ? x : fn3(x - 1, ref n) + (x % 10) * (n *= 10);  
  29.         }  
  30.         private static long fn4(long x, ref long n)  
  31.         {  
  32.             return (x < 10) ? x : fn4(x / 10, ref n) + (x % 10) * (n *= 10);  
  33.         }  
  34.         private static long fn5(int n, int sum)  
  35.         {  
  36.             if ((n==1&∑<=10)||(sum==n*10))  
  37.             {  
  38.                 return 1;  
  39.             }  
  40.             if (sum>n*10||sum<0)  
  41.             {  
  42.                 return 0;  
  43.             }  
  44.             long ok = 0;  
  45.             for (int i = 0; i <= 10; i++)  
  46.             {  
  47.                 ok += fn5(n - 1, sum - i);  
  48.             }  
  49.             return ok;  
  50.         }  
  51.   
  52.         static void Main(string[] args)  
  53.         {  
  54.             string[] strs = { """a""aa""aba""abba""abcba""ab""abc""abca" };  
  55.             for (int i = 0; i < strs.Length; i++)  
  56.             {  
  57.                 string str = strs[i];  
  58.                 int from = 0, to = str.Length - 1;  
  59.                 Console.WriteLine("{0} is {1}", str, fn1(ref str, ref from, ref to));  
  60.             }  
  61.             for (int i = 1; i <=30; i++)  
  62.             {  
  63.                 Console.Write("{0}:{1} /t", i, fn2(i));  
  64.             }  
  65.             long n = 1, m = 1, t = 0;  
  66.             for (int i = 0; i <=9; i++,n=m=1)  
  67.             {  
  68.                 Console.Write("/n {0}==>{1}", t = fn3(i, ref n), fn4(t, ref m));  
  69.             }  
  70.             Console.WriteLine("/n{0}种可能性", fn5(10, 90));  
  71.         }  
  72.     }  
  73. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值