一道acm题 Children’s Queue 急!!

xiaoxiong5227 2008-10-18 09:43:12
递推公式是:
f(n)=f(n-1)+f(n-2)+f(n-4)
要求算出n<=1000的结果
程序:#include<stdio.h>
int main()
{
int a[1000][500],i,n,j,t=0,s=0;
for(i=0;i<1000;i++)
for(j=0;j<500;j++)
a[i][j]=0;
a[0][0]=1;
a[1][0]=2;
a[2][0]=4;
a[3][0]=7;
for(i=4;i<1000;i++)
for(j=0;j<500;j++)
{
t=a[i-1][j]+a[i-2][j]+a[i-4][j]+s;
s=t/10;
a[i][j]=t%10;
}
while(scanf("%d",&n)!=EOF)
for(j=499;j>=0;j--)
{
if(a[n-1][j]!=0)
{
for(i=j;i>=0;i--)
printf("%d",a[n-1][i]);
printf("\n");
break;
}
}
}
提交结果是时间超过限制,
哪位大虾有好一点的办法啊
...全文
601 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaibinxi 2010-11-07
  • 打赏
  • 举报
回复
#include<stdio.h>
#define c 31
#define d 100000000
int main()
{
    int n,i,j;
    int f[1000][c]={0};
    f[0][0]=1;
    f[1][0]=2;
    f[2][0]=4;
    f[3][0]=7;
    for(i=4;i<1000;i++)
    {
      for(j=0;j<c;j++)
        f[i][j]=f[i-1][j]+f[i-2][j]+f[i-4][j];
      for(j=0;j<c-1;j++)
        if(f[i][j]>d)
        {
          f[i][j+1]+=f[i][j]/d;
          f[i][j]%=d;
        }
    }
    while(scanf("%d",&n)!=EOF)
    {
      for(i=c-1;i>0;i--)if(f[n-1][i]!=0)break;
      printf("%d",f[n-1][i]);
      for(j=i-1;j>=0;j--)printf("%08d",f[n-1][j]);
      printf("\n");
    }
    return 0;


要用到大整数 递推算法出来了就简单了
dage_01 2009-03-22
  • 打赏
  • 举报
回复
xiexie
xiaoxiong5227 2008-11-05
  • 打赏
  • 举报
回复
谢谢
fjtxwd 2008-11-05
  • 打赏
  • 举报
回复
#include "stdio.h"
main()
{
int i,j,n;
int num[1000][35]={0};
num[0][0]=1;
num[1][0]=2;
num[2][0]=4;
num[3][0]=7;
for(i=4;i <1000;i++)
{ for(j=0;j <35;j++)
num[i][j]=num[i-1][j]+num[i-2][j]+num[i-4][j];
for(j=0;j <34;j++)
if(num[i][j]>100000000)
{
num[i][j+1]+=num[i][j]/100000000;
num[i][j]%=100000000;
}
}
while(scanf("%d",&n)!=EOF)
{
for(i=34;i>0;i--)
if(num[n-1][i]!=0)
break;
printf("%d",num[n-1][i]);
for(j=i-1;j>=0;j--)
printf("%08d",num[n-1][j]);
printf("\n");
}
}

这个就是c的啊
xiaoxiong5227 2008-11-04
  • 打赏
  • 举报
回复
有没有c的啊?
12楼这位兄弟的在其他地方看到过啊
fjtxwd 2008-10-26
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
int num[1000][35]={0};
num[0][0]=1;
num[1][0]=2;
num[2][0]=4;
num[3][0]=7;
for(i=4;i<1000;i++)
{ for(j=0;j<35;j++)
num[i][j]=num[i-1][j]+num[i-2][j]+num[i-4][j];
for(j=0;j<34;j++)
if(num[i][j]>100000000)
{
num[i][j+1]+=num[i][j]/100000000;
num[i][j]%=100000000;
}
}
while(scanf("%d",&n)!=EOF)
{
for(i=34;i>0;i--)
if(num[n-1][i]!=0)
break;
printf("%d",num[n-1][i]);
for(j=i-1;j>=0;j--)
printf("%08d",num[n-1][j]);
printf("\n");
}
return 0;
}
jammy_hom 2008-10-26
  • 打赏
  • 举报
回复
FFMFF 想明白了, 自问自答下 呵呵
jammy_hom 2008-10-26
  • 打赏
  • 举报
回复
我根据公式
f(n)=f(n-1)+f(n-2)+f(n-4) 求得当n为5时,有12种情况,可我只能列出一下11中情况FFFFF, FFFFM, MFFFF, FFFMM, MFFFM, MMFFF, FFMMM, MFFMM, MMFFM, MMMFF, MMMMM
请问下还少了哪一种情况?
hchen2008 2008-10-19
  • 打赏
  • 举报
回复
递归
xiaoxiong5227 2008-10-19
  • 打赏
  • 举报
回复
结果是大数,远远超过int的范围啊
zhxyu19861224 2008-10-18
  • 打赏
  • 举报
回复
为何不用函数递归做呢?
int f(n)
{f(n)= f(n-1)+f(n-2)+f(n-4);
return f(n);

}
但是初始条件是什么呢?如果是F(0)=0就很简单了
xiaoxiong5227 2008-10-18
  • 打赏
  • 举报
回复
怎么没人了??自己顶一下。要求如题
xiaoxiong5227 2008-10-18
  • 打赏
  • 举报
回复
Children’s Queue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1327 Accepted Submission(s): 397


Problem Description
There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children?


Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000)


Output
For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.


Sample Input
1
2
3


Sample Output
1
2
4

  • 打赏
  • 举报
回复
acm?不懂,友情up
zez 2008-10-18
  • 打赏
  • 举报
回复
表达不清楚..

具体是什么问题..你给个递推公式,那结束条件呢?
zhyinty 2008-10-18
  • 打赏
  • 举报
回复
你直接把题贴出来算了
aaajj 2008-10-18
  • 打赏
  • 举报
回复
用个数组保存结果,不要重复计算

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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