ZOJ 1828 Fibonacci Numbers (大数)

大数加法,好久没有做过大数的题了,练手。

 

#include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { int i,n; int j,k,t,jinwei; int jian2[1001],jian1[1001],result[1001]; while( scanf("%d",&n) != EOF ) { if( n==1 || n == 2 ) printf("1/n"); memset(jian1,0,sizeof(jian1)); memset(jian2,0,sizeof(jian2)); memset(result,0,sizeof(result)); jian1[0] = jian2[0] = 1; for( i = 3; i <= n; i++ ) { for( j = jinwei = 0; j <= 1000; j++) { t = jian1[j] + jian2[j] + jinwei; if( t > 9 ) jinwei = 1,t %= 10; else jinwei = 0; result[j] = t; } memcpy(jian2,jian1,sizeof(result)); memcpy(jian1,result,sizeof(result)); } j = 1000; while( result[j] == 0 && j >= 0 ) j--; if( j < 0 ) printf("0/n"); else { while( j >= 0 ) printf("%d",result[j--]); printf("/n"); } } system("pause"); return 0; }  

你可能感兴趣的:(ZOJ 1828 Fibonacci Numbers (大数))