Big number summation(UVA 495)
code:
#include<iostream>
#define size 10000
using namespace std;
int max_fibo=0;
string fibo_n[5001];
string big_sum(string s1,string s2){
int l1,l2,carry=0;
int sum,i=0;
char result[size],ch;
for(l1=0;s1[l1]!='\0';l1++);
for(l2=0;s2[l2]!='\0';l2++);
for(l1--,l2--;l1>=0;l1--,l2--){
if(l2>=0){
sum=(s1[l1]-'0')+(s2[l2]-'0')+carry;
}else sum=(s1[l1]-'0')+carry;
carry=sum/10;
result[i++]=(sum%10)+'0';
}if(carry) result[i++]=carry+'0';
result[i]='\0';
for(l2=0;result[l2]!='\0';l2++);
l1=0;
for(l2--;l1<l2;l2--,l1++){
ch=result[l2];
result[l2]=result[l1];
result[l1]=ch;
}
return result;
}
int main()
{
fibo_n[0]="0";
fibo_n[1]="1";
for(int i=2;i<=5000;i++){
fibo_n[i]=big_sum(fibo_n[i-1],fibo_n[i-2]);
}
int N;
while(cin>>N){
cout<<"The Fibonacci number for "<<N<<" is "<<fibo_n[N]<<endl;
}return 0;
}
NB: Here first number greater than second number.
#define size 10000
using namespace std;
int max_fibo=0;
string fibo_n[5001];
string big_sum(string s1,string s2){
int l1,l2,carry=0;
int sum,i=0;
char result[size],ch;
for(l1=0;s1[l1]!='\0';l1++);
for(l2=0;s2[l2]!='\0';l2++);
for(l1--,l2--;l1>=0;l1--,l2--){
if(l2>=0){
sum=(s1[l1]-'0')+(s2[l2]-'0')+carry;
}else sum=(s1[l1]-'0')+carry;
carry=sum/10;
result[i++]=(sum%10)+'0';
}if(carry) result[i++]=carry+'0';
result[i]='\0';
for(l2=0;result[l2]!='\0';l2++);
l1=0;
for(l2--;l1<l2;l2--,l1++){
ch=result[l2];
result[l2]=result[l1];
result[l1]=ch;
}
return result;
}
int main()
{
fibo_n[0]="0";
fibo_n[1]="1";
for(int i=2;i<=5000;i++){
fibo_n[i]=big_sum(fibo_n[i-1],fibo_n[i-2]);
}
int N;
while(cin>>N){
cout<<"The Fibonacci number for "<<N<<" is "<<fibo_n[N]<<endl;
}return 0;
}
NB: Here first number greater than second number.
problem link:Fibonacci Freeze
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন