Handsome Kun

仰天长笑,笑天下可笑之人; 大肚能盛,盛天下可盛之事!

导航

博客里面什么都没有,把师傅给我做的两个题目贴一下吧^_^

第一个:打靶问题
一人打靶,一次最多中10环(最少可以为0),共打了10次靶,中了90环,用递规列出所有可能的情况

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

/*
Author:Handsome Kun
*/
namespace ShootTarget
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Begin(90, 0);
            View.AppendText("打靶结束,总共有" + AllPossibleCount.ToString() + "种情况");
        }
        static int[] APossible = new int[10];
        private static int AllPossibleCount = 0;
        private void Begin(int Leave, int Count)
        {
            if (Leave > (10 - Count) * 10)//后面的每次中10环都不能中满90环
            {
                return;
            }

            if (Leave == 0)//没有环数了
            {
                for (int i = Count; i < 10; i++)
                {
                    APossible[i] = 0;
                }
                Print();
                return;
            }

            if (Count == 9)
            {
                APossible[Count] = Leave;
                Print();
                return;
            }

            int temp = Leave > 10 ? 10 : Leave;
            int NextCount = Count + 1;
           
            for (int i = 0; i <= temp; i++)
            {
                APossible[Count] = i;
                Begin(Leave - i, NextCount);
            }
        }

        private void Print()
        {
            string OnePossible = "";
            for (int i = 0; i < 10; i++)
            {
                OnePossible += APossible[i].ToString() + "  ";
            }
            AllPossibleCount++;
            View.AppendText("第" + AllPossibleCount + "种情况:" + OnePossible);
        }
    }
}

posted on 2008-05-17 19:10  Handsome Kun  阅读(222)  评论(0编辑  收藏  举报