C# 编程题,请问这个算法用递归怎么写?

2025-04-03 22:21:55
推荐回答(1个)
回答1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;namespace ConsoleApplication1
{
class Program
{
public static int count =1; static void Main(string[] args)
{
string str = Console.ReadLine();
if (Regex.IsMatch(str, @"^\d+$"))
{
Build(2 * int.Parse(str));
Console.WriteLine(count);
}
Console.ReadKey();
} private static void Build(int _year)
{
for (int i = 0; i < (_year < 12 ? _year : 12); i++)
{
if (i == 0 || i == 1)
{
}
else if (i == 11)
{
count--;
}
else if (i == 10)
{ }
else
{
count++;
Build(_year - i);
}
}
}
}
}