# 希望大佬们可以指导下怎么把Fibonacci数判定的代码改成高精度啊？

...全文
3445 18 打赏 收藏 举报

18 条回复

64位无符号数只能计算到fib(93)，“1000位以内的整数”需要大数运算，不过肯定超时，此路不通。实际上（十进制）1000位以内的fibonacci数只有4800多个，所以实现方法和我之前的代码是一样的，只是需要自己做字符串模拟加法
• 打赏
• 举报

luj_1768 2021-05-12

• 打赏
• 举报

``````#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
scanf("%d",&n);
for (j=0;j<n;j++) {
scanf("%s%s",a1,a2);

L=strlen(a1);
for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';

L=strlen(a2);
for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';

for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];

for (i=0;i<MAXLEN;i++) {
if (v3[i]>=10) {
v3[i+1]+=v3[i]/10;
v3[i]=v3[i]%10;
}
}

printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);

z=0;
for (i=MAXLEN-1;i>=0;i--) {
if (z==0) {
if (v3[i]!=0) {
printf("%d",v3[i]);
z=1;
}
} else {
printf("%d",v3[i]);
}
}
if (z==0) printf("0");

printf("\n");
}
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110
``````
• 打赏
• 举报

qzjhjxj 2021-05-11

``````#include<stdio.h>

int Fab(unsigned __int64 n)
{
unsigned __int64 a=0,b=1,k=0;
while((k=a+b) < n){
a=b;   b=k;
}
if(k==n)return 1;
else    return 0;
}

int main()
{
unsigned __int64 n;
while(scanf("%I64u",&n)!=EOF)
{
if(Fab(n))
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````
• 打赏
• 举报

``````
#include <stdio.h>

int main()
{
unsigned int f[47] = {1, 1};

for (int i = 2; i < 47; i++) f[i] = f[i - 1] + f[i - 2];
for (int i, n; ~scanf("%d", &n);)
{
for (i = 0; i < 47; i++) if (n == f[i]) break;
printf("%s\n", i < 47 ? "yes" : "no");
}

return 0;
}
``````

• 打赏
• 举报

``````
#include <stdio.h>

int main()
{
unsigned int f[47] = {1, 1};

for (int i = 2; i < 47; i++) f[i] = f[i - 1] + f[i - 2];
for (int i, n; ~scanf("%d", &n);)
{
for (i = 0; i < 47; i++) if (n == f[i]) break;
printf("%s\n", i < 47 ? "yes" :
}
return 0;
}
``````

• 打赏
• 举报

``````int Fab(int n)
{
int a=0,b=1,k=0;
while((k=a+b) < n){
a=b;
b=k;
}
if(k==n)return 1;
else    return 0;
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(Fab(n))
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````

• 打赏
• 举报

``````
#include <stdio.h>

int main()
{
unsigned int f[47] = {1, 1};

for (int i = 2; i < 47; i++) f[i] = f[i - 1] + f[i - 2];
for (int i, n; ~scanf("%d", &n);)
{
for (i = 0; i < 47; i++) if (n == f[i]) break;
printf("%s\n", i < 47 ? "yes" : "no");
}

return 0;
}
``````

• 打赏
• 举报

qzjhjxj 2021-05-11

``````int Fab(int n)
{
int a=0,b=1,k=0;
while((k=a+b) < n){
a=b;
b=k;
}
if(k==n)return 1;
else    return 0;
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(Fab(n))
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````
• 打赏
• 举报

qzjhjxj 2021-05-11

``````int Fab(int n)
{
int a=0,b=1,k=0;
while((k=a+b) < n){
a=b;
b=k;
}
if(k==n)return 1;
else    return 0;
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==1||n==2||Fab(n))
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````
• 打赏
• 举报

``````#include<stdio.h>
int fab(int n)
{
if(n==1||n==2)      return 1;
else return fab(n-1)+fab(n-2);
}

int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=3;fab(i)<n;i++);
if(n==1||n==2||fab(i)==n)
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````

• 打赏
• 举报

• 打赏
• 举报

qzjhjxj 2021-05-10

``````#include<stdio.h>
int fab(int n)
{
if(n==1||n==2)      return 1;
else return fab(n-1)+fab(n-2);
}

int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=3;fab(i)<n;i++);
if(n==1||n==2||fab(i)==n)
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````
• 打赏
• 举报

``````#include<stdio.h>
int fab(int n)
{
if(n==1)
return 1;
if(n==2)
return 1;
if(n>2)
return fab(n-1)+fab(n-2);
}
int isfab(int m)
{
int result=0;
for(int i=0; fab(i)<m; i++)
{
if(fab(i+1)==m)
{
result=1;
break;
}

}
return result;
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(isfab(n)==1)
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````

• 打赏
• 举报

qzjhjxj 2021-05-10

``````#include<stdio.h>
int fab(int n)
{
if(n==1)
return 1;
if(n==2)
return 1;
if(n>2)
return fab(n-1)+fab(n-2);
}
int isfab(int m)
{
int result=0;
for(int i=0; fab(i)<m; i++)
{
if(fab(i+1)==m)
{
result=1;
break;
}

}
return result;
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(isfab(n)==1)
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````
• 打赏
• 举报

``````#include<stdio.h>
int fab(int n)
{
if(n==1)
return 1;
if(n==2)
return 1;
if(n>2)
return fab(n-1)+fab(n-2);
}
int isfab(int m)
{
int result=0;
for(int i=0; fab(i)<m; i++)
{
if(fab(i+1)==m)
{
result=1;
break;
}
else
result=0;

}
return result;
}

int main()
{
int n;
while(1)
{
if(scanf("%d",&n)==EOF) break;
if(isfab(n)==1)
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````

• 打赏
• 举报

qzjhjxj 2021-05-10

``````#include<stdio.h>
int fab(int n)
{
if(n==1)
return 1;
if(n==2)
return 1;
if(n>2)
return fab(n-1)+fab(n-2);
}
int isfab(int m)
{
int result=0;
for(int i=0; fab(i)<m; i++)
{
if(fab(i+1)==m)
{
result=1;
break;
}
else
result=0;

}
return result;
}

int main()
{
int n;
while(1)
{
if(scanf("%d",&n)==EOF) break;
if(isfab(n)==1)
printf("yes\n");
else
printf("no\n");
}

return 0;
}``````
• 打赏
• 举报

• 打赏
• 举报

3859

C/C++ 其它技术问题

2021-05-09 12:23