Hdu1316 (the Fibonacci number of large numbers), hdu1316 Fibonacci
Question information: calculate the number of Fibonacci numbers between two large numbers (C ++/JAVA)
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1316
Java code and c ++ code are provided here.
C ++: AC code
# Include <iostream>
# Include <string>
Using namespace std;
String add (string s1, string s2) {// simulate the addition of large numbers to a string
String s;
Int len1, len2;
Len1 = s1.size ()-1; len2 = s2.size ()-1;
Int I = 0, flag = 0;
While (len1>-1 & len2>-1 ){
Int sum = flag + (s1 [len1 --]-'0') + (s2 [len2 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
While (len1>-1 ){
Int sum = flag + (s1 [len1 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
While (len2>-1 ){
Int sum = flag + (s2 [len2 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
If (flag) s + = char ('0' + flag );
// Cout <s <endl;
For (int I = 0; I <s. size ()/2; I ++ ){
Char c = s [I];
S [I] = s [s. size ()-i-1];
S [s. size ()-i-1] = c;
}
Return s;
}
Int compareto (string s, string s1) {// simulate comparison of large numbers (strings)
Int j, I, len = s. size (), len1 = s1.size ();
For (I = 0; s [I] = '0'; I ++); // remove the leading 0
For (j = 0; s1 [j] = '0'; j ++); // remove the leading 0
If (len-I> len1-j) return 1;
Else if (len-I <len1-j) return-1;
Else for (; s [I] = s1 [j] & I <len; I ++, j ++ );
If (s [I]> s1 [j]) return 1; // s> s1
If (s [I] <s1 [j]) return-1; // s <s1
Return 0;
}
String s [501];
Int main ()
{
String a, B;
S [1] = "1"; s [2] = "2 ";
For (int I = 3; I <= 500; I ++ ){
S [I] = add (s [I-1], s [I-2]);
// If (I <10) cout <s [I] <endl;
}
While (cin> a> B ){
If (a = "0" & B = "0") break;
// Cout <compareto (a, B) <endl;
Int sum = 0;
For (int I = 1; I <= 500; I ++ ){
If (compareto (s [I], a)> = 0 & compareto (s [I], B) <= 0 ){
Sum ++;
}
}
Cout <sum <endl;
}
Return 0;
}
Java code:
Import java. math. BigInteger;
Import java. util. collections;
Public class Main {
Public static void main (String [] args ){
BigInteger a, B;
BigInteger s [] = new BigInteger [1501];
S [0] = BigInteger. ZERO;
S [1] = BigInteger. ONE;
S [2] = BigInteger. valueOf (2 );
For (int I = 3; I <= 1500; I ++ ){
S [I] = s [I-1]. add (s [I-2]);
}
// System. out. println (s [1, 1500]);
Pipeline SC = new pipeline (System. in );
While (SC. hasNext ()){
A = SC. nextBigInteger ();
B = SC. nextBigInteger ();
If (a. add (B). compareTo (BigInteger. ZERO) = 0) break;
Int cnt = 0;
// Improve program robustness
If (a. compareTo (B)> 0 ){
BigInteger tmp =;
A = B;
B = tmp;
}
For (int I = 1; I <= 1500; I ++ ){
If (s [I]. compareTo (a)> = 0 & s [I]. compareTo (B) <= 0 ){
Cnt ++;
}
}
System. out. println (cnt );
}
}
}
C ++ Fibonacci big numbers big ones computer program design questions should be able to calculate large numbers is very large, can break through the number of stacks must use the project to do
# Include <stdio. h>
# Include <string. h>
Int a [1001] [1001];
Int main ()
{
Int t, n, I, j, sum = 0;
Memset (a, 0, sizeof ());
A [1] [0] = 1;
A [2] [0] = 1;
For (I = 3; I <1001; I ++)
{
For (j = 0; j <1001; j ++)
{
Sum + = a [I-1] [j] + a [I-2] [j];
A [I] [j] = sum % 100000000;
Sum // = 100000000;
}
}
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n );
For (I = 1000; I> = 0; I --)
{
If (a [n] [I])
Break;
}
Printf ("% d", a [n] [I --]);
For (j = I; j> = 0; j --)
{
Printf ("% 08d", a [n] [j]);
}
Printf ("\ n ");
}
Return 0;
}
The issue of the Fibonacci series in C language (the main problem is how to handle data overflow. I am not familiar with big number calculation. TAT)
Returns the Fibonacci sequence of a large number.
# Include <stdio. h>
# Include <iostream>
Using namespace std;
Int a [201] [101] = {0 };
Int B [201] [101] = {0 };
Int c [201] [101] = {0 };
Int n;
Int main ()
{
Int I, j, k;
Scanf ("% d", & n );
For (k = 1; k <= n; k ++)
{
If (k <3)
{Printf ("% d \ n", k );
}
Else
{
C [k] [1] = 2;
B [k] [1] = 1;
For (I = 1; I <= K-2; I ++)
{For (j = 1; j <= 100; j ++)
{A [k] [j] = B [k] [j];
B [k] [j] = c [k] [j];
C [k] [j] = 0 ;}
For (j = 1; j <= 100; j ++)
{C [k] [j] = c [k] [j] + a [k] [j] + B [k] [j];
If (c [k] [j]> = 10)
{C [k] [j] = c [k] [j]-10;
C [k] [j + 1] = 1 ;}
}
}
I = 100;
While (c [k] [I] = 0) I --;
For (I = I; I> = 1; I --)
Printf ("% d", c [k] [I]);
Printf ("\ n ");
}
}
System ("pause ");
Return 0;
}