当前位置: 代码迷 >> C语言 >> [求助]一个递推题,总错第九个数据
  详细解决方案

[求助]一个递推题,总错第九个数据

热度:350   发布时间:2007-08-14 08:35:31.0
以下是引用mp3aaa在2007-8-13 18:31:14的发言:

这个是我的 算法好像一样吧
#include <stdio.h>
#include<math.h>
int main()
{
int k ,n,i,j,m,t,L,y;
int a[1000]={0};
a[0]=1;
while(scanf("%d%d",&k,&n)!=EOF){
j=1,t=0,L=1,y=2;
for(m=0;m<n;m++)
{
if(j+1==y){
y=y*2;
t=pow(k,L++);
a[j++]=t;
i=0;
continue;
}
a[j++]=t+a[i++];
}
printf("%d",a[n-1]);

}
}

把数组开大一点,小心越界


----------------解决方案--------------------------------------------------------

//回复11楼:

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
short x=0,s[2002];
short i,j,n,k;
cin >> k >> n;
i=1;//原来的0改为1就成功啦
while(i<=n)
{
s[i]=(short)pow(k,x);
x++;
for(j=1;j<i;j++)
{
if(i+j>=2002)return -1;
s[i+j]=s[i]+s[j];
}
i=i+j;
}
cout<<s[n]<<endl;
return 0;
}


----------------解决方案--------------------------------------------------------
以下是引用yu_hua在2007-8-14 10:52:13的发言:

//回复11楼:

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
short x=0,s[2002];
short i,j,n,k;
cin >> k >> n;
i=1;//原来的0改为1就成功啦
while(i<=n)
{
s[i]=(short)pow(k,x);
x++;
for(j=1;j<i;j++)
{
if(i+j>=2002)return -1;
s[i+j]=s[i]+s[j];
}
i=i+j;
}
cout<<s[n]<<endl;
return 0;
}

明显通过不了,short的范围在int之内了,太小了,估计也就过2-3个测试点就不错了


----------------解决方案--------------------------------------------------------
以下是引用卧龙孔明在2007-8-14 10:55:46的发言:

明显通过不了,short的范围在int之内了,太小了,估计也就过2-3个测试点就不错了

你太骄傲了。为什么不实地试一试呢?

[此贴子已经被作者于2007-8-14 11:00:14编辑过]


----------------解决方案--------------------------------------------------------
以下是引用yu_hua在2007-8-14 10:58:35的发言:

你太骄傲了。为什么不实地试一试呢?


我错了,对不起,我没尝试过
不过你的也错了
http://www.vijos.cn/Record_Show.asp?id=433109
R433109 Unaccepted 0 From 卧龙孔明-
  P1319 CPP Vivid Puppy 2007-8-14 11:05:00


数列 全国青少年信息学奥林匹克分区联赛 (NOIp) 竞赛原题


编译失败...|错误号:1


MyProger\Prog31780.cpp: In function `int main()':
MyProger\Prog31780.cpp:13: error: call of overloaded `pow(short int&, short int&)' is ambiguous
C:/Vijos/Compiler/GCC/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
C:/Vijos/Compiler/GCC/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note:         long double std::pow(long double, int)
C:/Vijos/Compiler/GCC/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note:         float std::pow(float, int)
C:/Vijos/Compiler/GCC/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note:         double std::pow(double, int)
C:/Vijos/Compiler/GCC/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note:         long double std::pow(long double, long double)
C:/Vijos/Compiler/GCC/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note:         float std::pow(float, float)


-------------------------
Unaccepted 有效得分:0 有效耗时:0ms



----------------解决方案--------------------------------------------------------
  相关解决方案