SHARE
TWEET

Untitled

a guest Sep 17th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string findSum(string str1, string str2);
  5.  
  6. string StringFib(string number, int num){
  7.  
  8.     string bignumba = "";
  9.     string first = "0";
  10.     string secon = "1";
  11.  
  12.     for(int i = 0; i < num-1; i++){
  13.         bignumba = findSum(first,secon);
  14.         first = secon;
  15.         secon = bignumba;
  16.     }
  17.  
  18.     return bignumba;
  19. }
  20.  
  21. int main(){
  22.  
  23.     int reps;
  24.     cin >> reps;
  25.     while(reps--){
  26.     int a;
  27.     cin >> a;
  28.     string n = to_string(a);
  29.  
  30.     cout << StringFib(n,a+1) << "\n";
  31.     }
  32.     return 0;
  33.  
  34. }
  35.  
  36. string findSum(string str1, string str2) {
  37.  
  38.     if (str1.length() > str2.length())
  39.         swap(str1, str2);
  40.  
  41.     string str = "";
  42.  
  43.  
  44.     int n1 = str1.length(), n2 = str2.length();
  45.  
  46.     reverse(str1.begin(), str1.end());
  47.     reverse(str2.begin(), str2.end());
  48.  
  49.     int carry = 0;
  50.     for (int i=0; i<n1; i++)
  51.     {
  52.         int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
  53.         str.push_back(sum%10 + '0');
  54.  
  55.         carry = sum/10;
  56.     }
  57.  
  58.     for (int i=n1; i<n2; i++)
  59.     {
  60.         int sum = ((str2[i]-'0')+carry);
  61.         str.push_back(sum%10 + '0');
  62.         carry = sum/10;
  63.     }
  64.  
  65.     if (carry)
  66.         str.push_back(carry+'0');
  67.  
  68.     reverse(str.begin(), str.end());
  69.  
  70.     return str;
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top