欢迎访问我的博客 https://javascript.shop

递归 打靶子问题

原文发布时间为:2009-03-01 —— 来源于本人的百度文章 [由搬家工具导入]

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

namespace digui1
{
public class Class2
    {
      public static int sum=0;
      public static int[] store=new int[10];

      public static void Main()
      {
          fun(90, 9);
          Console.WriteLine("总数为{0}", sum);
          Console.ReadLine();
      }

      public static void fun(int score, int num)
      {
          if (score < 0 || score > (num + 1) * 10)
              return;
          if (num == 0)
          {
              store[num] = score;
              Output();
              return;
          }
          for (int i = 0; i <= 10; i++)
          {
              store[num] = i;
              fun(score - i, num - 1);
          }
      }
      public static void Output()
      {
          for (int i = 9; i >= 0; i--)
          {
              Console.Write("{0} ", store[i]);
          }
          Console.WriteLine();
          ++sum;
      }
    }
}

posted @ 2017-07-11 00:39  孑孓子  阅读(172)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop