也是打靶问题

abcdef0966 2009-09-14 09:57:10
一个射击运动员打靶,靶一共有10环,连开10枪打中90环的可能行有多少种?
#include <iostream>
using namespace std;


void Target(int count, int score,int turns);

static int count;

int main()
{
Target(count,90,10);
cout << count << endl;



return 0;
}

void Target(int count, int score, int turns)
{
if (score < 0 || 10 * turns < score)
return;
if (0 == turns)
{

++count;
return;

}

for (int i = 0; i < 11; i++)
{
Target(count,score-i,turns-1);
}
}


我声明的count是全局变量啊,为什么值不能改变呢
...全文
176 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
showjim 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 litaoye 的回复:]
由于只是打丢了10环,因此这个问题还有更简单的方法,就是C(19,10),如果是打丢了11环,就只能用上面的程序来了。
[/Quote]
是哦,那小于等于10的都可以优化了;
0-10与11-21也有简单的对应关系,那么<=21的都可以优化了.
绿色夹克衫 2009-09-17
  • 打赏
  • 举报
回复
由于只是打丢了10环,因此这个问题还有更简单的方法,就是C(19,10),如果是打丢了11环,就只能用上面的程序来了。
showjim 2009-09-16
  • 打赏
  • 举报
回复
        public static int shooting90()
{
int[] factorials = new int[10];
for (int f = factorials[0] = 1, i = 2; i <= 10; i++) factorials[i - 1] = (f *= i);
return shooting(0, 10, 10, 100 - 90, factorials);
}
private static int shooting(ulong currentValue, int rings, int maxValue, int sum, int[] factorials)
{
int value = 0, nextValue = maxValue;
if (sum <= maxValue)
{
int thisValue, lastValue = sum, valueCount = 0, count = factorials[9];
if (rings > 2) count /= factorials[rings - 2];
for (ulong values = currentValue; values != 0; values >>= 4)
{
if ((thisValue = (int)values & 15) == lastValue) valueCount++;
else
{
if (valueCount != 0)
{
count /= factorials[valueCount];
valueCount = 0;
}
lastValue = thisValue;
}
}
if (valueCount != 0) count /= factorials[valueCount];
value += count;
nextValue = sum - 1;
}
if (--rings != 0) while (nextValue != 0) value += shooting((currentValue << 4) | (uint)nextValue, rings, nextValue, sum - nextValue--, factorials);
return value;
}
abcdef0966 2009-09-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 litaoye 的回复:]
C#的,LZ帮我看看算对了没有?92378?

using System;

namespace ConsoleApplication5
{
    class Program
    {
        static long[,] Counter;
        static int[] upper;
        static int[] accumlation;
       
        static void Main(string[] args)
        {
            int Sum = 90;
            upper = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
            accumlation = new int[upper.Length + 1];

            for (int i = upper.Length - 1; i>=0 ; i--)
                accumlation[i] = accumlation[i + 1] + upper[i];

            Counter = new long[Sum + 1,upper.Length];
            Console.WriteLine(Calculate(Sum, 0));
        }

        static long Calculate(int sum,int index)
        {
            if (sum == 0 || sum == accumlation[index])
                return 1;

            if (index >= upper.Length || sum > accumlation[index])
                return 0;
           
            if(Counter[sum,index] > 0)
                return Counter[sum, index];

            for (int i = 0; i <= Math.Min(upper[index],sum); i++)
                Counter[sum, index] += Calculate(sum - i, index + 1);

            return Counter[sum, index];
        }
    }
}

[/Quote]

en ,92378
绿色夹克衫 2009-09-14
  • 打赏
  • 举报
回复
C#的,LZ帮我看看算对了没有?92378?

using System;

namespace ConsoleApplication5
{
class Program
{
static long[,] Counter;
static int[] upper;
static int[] accumlation;

static void Main(string[] args)
{
int Sum = 90;
upper = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
accumlation = new int[upper.Length + 1];

for (int i = upper.Length - 1; i>=0 ; i--)
accumlation[i] = accumlation[i + 1] + upper[i];

Counter = new long[Sum + 1,upper.Length];
Console.WriteLine(Calculate(Sum, 0));
}

static long Calculate(int sum,int index)
{
if (sum == 0 || sum == accumlation[index])
return 1;

if (index >= upper.Length || sum > accumlation[index])
return 0;

if(Counter[sum,index] > 0)
return Counter[sum, index];

for (int i = 0; i <= Math.Min(upper[index],sum); i++)
Counter[sum, index] += Calculate(sum - i, index + 1);

return Counter[sum, index];
}
}
}
LeonTown 2009-09-14
  • 打赏
  • 举报
回复
是啊是啊,
楼主mm大意了。。。
abcdef0966 2009-09-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 peacefulby 的回复:]
既然是全局变量的话,直接声明为Target(int score, int turns)就好了
否则函数内部会创建一个count局部变量覆盖全局变量的
[/Quote]

一语惊醒梦中人啊!!
多谢!!!
PeacefulBY 2009-09-14
  • 打赏
  • 举报
回复
既然是全局变量的话,直接声明为Target(int score, int turns)就好了
否则函数内部会创建一个count局部变量覆盖全局变量的

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧