编写一个方法用于验证指定的字符串是否为反转字符,返回true和false。请用递归算法实现。(反转字符串样式为"abcdedcba")
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace RecursionAlgorithms
- {
- class Program
- {
- private static bool fn1(ref string str, ref int from, ref int to)
- {
- if (from>=to)
- {
- return true;
- }
- if (str[from++]!=str[to--])
- {
- return false;
- }
- return fn1(ref str,ref from, ref to);
- }
- private static int fn2(int i)
- {
- return i <= 2 ? 1 : fn2(i - 2) + fn2(i - 1);
- }
- private static long fn3(long x, ref long n)
- {
- return (x <= 1) ? x : fn3(x - 1, ref n) + (x % 10) * (n *= 10);
- }
- private static long fn4(long x, ref long n)
- {
- return (x < 10) ? x : fn4(x / 10, ref n) + (x % 10) * (n *= 10);
- }
- private static long fn5(int n, int sum)
- {
- if ((n==1&∑<=10)||(sum==n*10))
- {
- return 1;
- }
- if (sum>n*10||sum<0)
- {
- return 0;
- }
- long ok = 0;
- for (int i = 0; i <= 10; i++)
- {
- ok += fn5(n - 1, sum - i);
- }
- return ok;
- }
- static void Main(string[] args)
- {
- string[] strs = { "", "a", "aa", "aba", "abba", "abcba", "ab", "abc", "abca" };
- for (int i = 0; i < strs.Length; i++)
- {
- string str = strs[i];
- int from = 0, to = str.Length - 1;
- Console.WriteLine("{0} is {1}", str, fn1(ref str, ref from, ref to));
- }
- for (int i = 1; i <=30; i++)
- {
- Console.Write("{0}:{1} /t", i, fn2(i));
- }
- long n = 1, m = 1, t = 0;
- for (int i = 0; i <=9; i++,n=m=1)
- {
- Console.Write("/n {0}==>{1}", t = fn3(i, ref n), fn4(t, ref m));
- }
- Console.WriteLine("/n{0}种可能性", fn5(10, 90));
- }
- }
- }