我刚刚测验过 19500那个数据 他应该用的 5 120 来测试的 在我这 5 120 输出的就是 19500 根本不是 19497
羊肉串 葡萄干 哈密瓜!!
这个是我的 算法好像一样吧
#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]);
}
}
/*---------------------------------------------------------------------------
File name: vijos1319.c
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 8/13/2007 03:46:19
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
I think Leeco and my algorithm work. Mine has to be modified a little bit.
Here is a AC submission:
R431946 Accepted 100 From hjin- P1319 GCC Vivid Puppy 2007-8-13 18:41:25
==================(submitted code)=============================
main(){int k,N,s,p;while(scanf("%d%d",&k,&N)!=-1){s=0;p=1;while(N){if(N&1)s+=p;p*=k;N>>=1;}printf("%d\n",s);}}
*/
main()
{
int k, N, s, p;
while(scanf("%d%d", &k, &N)!=-1)
{
s=0;
p=1;
while(N)
{
if(N&1)
s+=p;
p*=k;
N>>=1;
}
printf("%d\n", s);
}
}
题目明确说明:保证输出数据在2^31以内,所以表示用int完全可以,不需要用极端数据15 1000测试,我用过unsigned,同样过6个数据