| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求大神帮忙!!一直tle但是自己又不觉得慢,我估计是哪里掉循环里面了,所有测试数据我都过了啊,大家看看//首先参数传进来肯定是字符串,肯定要做逆置
//,把下标为0的字符变成数字的个位,然后模拟进位,算两个数的加法,存到下一个字符串里面,直到超过10^100,然后二分查找来找到数的临界值,
//从那个值开始进行字符串比较,直到比上限都大
#include <stdio.h>
#include <string.h>
char he[150]={0};
void plus(char beijiashu[],char jiashu[])
{
char beijiashufan[150]={0};
char jiashufan[150]={0};
int i,k;
int temp;//计算用临时变量
int carry=0;//进位变量
for(i=strlen(beijiashu)-1,k=0;i>=0;i--,k++)
{
beijiashufan[k]=beijiashu[i];
}
for(i=strlen(jiashu)-1,k=0;i>=0;i--,k++)
{
jiashufan[k]=jiashu[i];
}
int len;
len=strlen(beijiashufan);
for(i=0;i<=len;i++)
{
if(jiashufan[i]==0)
jiashufan[i]=jiashufan[i]+'0';
if(beijiashufan[i]==0)
beijiashufan[i]=beijiashufan[i]+'0';
temp=(beijiashufan[i]-'0')+(jiashufan[i]-'0')+carry;
carry=temp/10;
beijiashufan[i]=temp%10+'0';
}
len=strlen(beijiashufan);
if(beijiashufan[len-1]=='0')
beijiashufan[len-1]=0;
len=strlen(beijiashufan);
for(i=len-1,k=0;i>=0;i--,k++)
{
he[k]=beijiashufan[i];
}
}
int cmp(char a[],char b[])
{
int r;
if(strlen(a)>strlen(b))
r=1;
else if(strlen(a)<strlen(b))
r=-1;
else
r=strcmp(a,b);
return r;
}
int main()
{
char fibs[500][120]={0};
//首先把fib数组构建起来
strcpy(fibs[0],"1");
strcpy(fibs[1],"2");
int i;
for(i=2;strlen(fibs[i-1])<102;i++)
{
plus(fibs[i-1],fibs[i-2]);
strcpy(fibs[i],he);
memset(he,0,sizeof(char)*150);
}
char low[120]={0},high[120]={0};
while(1)
{
scanf("%s %s",low,high);
if(!(strcmp(low,"0")||strcmp(high,"0")))
break;
int min=0,max=500,mid;
mid=(min+max)/2;
while(!((cmp(fibs[mid],low)<0)&&(cmp(fibs[mid+1],low)>0)))
{
if(cmp(fibs[mid],low)>0)
max=mid;
else if(cmp(fibs[mid],low)<0)
min=mid;
else
{
break;
}
mid=(min+max)/2;
if(min>=max)
break;
}
int count=0;
while(cmp(fibs[mid],high)<=0)
{
if((cmp(fibs[mid],low)>=0)&&(cmp(fibs[mid],high)<=0))
count++;
mid++;
}
printf("%d\n",count);
}
return 0;
}
Poj菜鸟求解啊!!!
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator