My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
/*---------------------------------------------------------------------------
File name: NKU1021-[NKPC2]?S???.c
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 8/12/2007 21:29:53
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
I think I discussed this problem with Leeco before. Bottom of line is:
write out the binary representation of n.
Following is my code submitted to Nankai online judge.
*/
long long b[] = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147, 531441, 1594323, 4782969, 14348907, 43046721, 129140163, 387420489, 1162261467, 3486784401, 10460353203, 31381059609, 94143178827, 282429536481, 847288609443, 2541865828329, 7625597484987, 22876792454961, 68630377364883, 205891132094649, 617673396283947};
int main()
{
int k;
unsigned n;
while( scanf("%u", &n) != -1 )
{
--n;
k=0;
while(n)
{
if(n&1)
printf("%lld\n", b[k]);
++k;
n >>= 1;
}
}
return 0;
}
与这道题不完全一样,输入样例后过不了,这道题的规模在int内,不需要long long
又看到这个结果了,我用int,unsigned和高精度都是这个结果,也不知道为什么,你换成double可以看到结果是90分
题目明确说明:保证输出数据在2^31以内,所以表示用int完全可以,不需要用极端数据15 1000测试,我用过unsigned,同样过6个数据
这个是我的 算法好像一样吧
#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;
}
明显通过不了,short的范围在int之内了,太小了,估计也就过2-3个测试点就不错了