Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | None | 0 0
  1. //10720116鄭宇傑 10720138陳尚宏
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <list>
  5. #include <vector>
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9. int mission1(int);
  10. unsigned long long mission2(int);
  11. void initial(void);
  12. void printDivisors(unsigned long long);
  13. int mission1_new(int);
  14. unsigned long long fib(int, unsigned long long, unsigned long long);
  15.  
  16. int fibcount = 0 ;
  17.  
  18.  
  19. int rec=0;
  20. struct factorlist
  21. {
  22.     unsigned long long value1,value2,dif;
  23.     struct factorlist *rlink;
  24.     struct factorlist *llink;
  25. };
  26. struct factorlist *head,*tail,*prev,*ptr,*cur;
  27.  
  28.  
  29. int main()
  30. {
  31.     int num1;
  32.  
  33.  
  34.  
  35.     while(1)
  36.     {
  37.         int num;
  38.         cout<<"\n"<<"**Fibonacci Series Generator**"<<endl;
  39.         cout<<"* 0.Quit                      *"<<endl;
  40.         cout<<"* 1.Iterative generation      *"<<endl;
  41.         cout<<"* 2.Recursive generation      *"<<endl;
  42.         cout<<"*******************************"<<endl;
  43.         cout <<"Input a command(0,1,2): ";
  44.         cin>>num;
  45.         switch(num)
  46.         {
  47.  
  48.         case 0:
  49.             exit(1);
  50.             break;
  51.         case 1:
  52.             cout<<"Please input a number:";
  53.             cin>>num1;
  54.             mission1(num1);
  55.             break;
  56.         case 2:
  57.             {
  58.  
  59.  
  60.             cout<<"Please input a number:";
  61.             cin>>num1;
  62.             //mission2(num1);
  63.             int outloop = 0;
  64.             for(int i=1; i<=num1; i++)
  65.             {
  66.                 outloop++;
  67.  
  68.                 //unsigned long long fnum = mission2(i);
  69.                 unsigned long long fnum = fib(i+1,1,0);
  70.  
  71.                 cout<<"["<<i<<"]";
  72.                 cout<<fnum;
  73.  
  74.                 unsigned long long firstnum,secnum ;
  75.  
  76.                 int inloop = 0 ;
  77.  
  78.                 for (unsigned long long i1=1; i1<=sqrt(fnum); i1++)
  79.                 {
  80.                     if (fnum%i1 == 0)
  81.                     {
  82.                         firstnum = i1;
  83.                         secnum = fnum/i1;
  84.                     }
  85.                     inloop++ ;
  86.                 }
  87.  
  88.                 printf(" = ");
  89.                 cout<<firstnum;
  90.                 printf(" * ");
  91.                 cout<<secnum;
  92.                 //printf("\n");
  93.  
  94.  
  95.                 cout<<" (Inner recursion: "<<inloop<<" times)\n";
  96.  
  97.             }
  98.             cout<<"<Outer recursion: "<<outloop<<" times>\n";
  99.             break;
  100.             }
  101.         default:
  102.             cout<<" Command does not exist."<<endl;
  103.             break;
  104.  
  105.         }
  106.     }
  107.     return 0;
  108. }
  109.  
  110.  
  111.  
  112.  
  113. unsigned long long mission2(int num1)
  114. {
  115.  
  116.  
  117.     if(num1==0){
  118.  
  119.         return 1;
  120.     }
  121.     else if(num1==1){
  122.  
  123.         return 1;
  124.     }
  125.     else
  126.         return mission2(num1-1)+mission2(num1-2);
  127. }
  128.  
  129. unsigned long long fib(int term, unsigned long long val = 1, unsigned long long prev = 0)
  130. {
  131.  if(term == 0) return prev;
  132.  return fib(term - 1, val+prev, val);
  133. }
  134.  
  135.  
  136.  
  137. int mission1(int num1)
  138. {
  139.     int t=0,h=0;
  140.     unsigned long long Fib1=1,Fib0=0;
  141.     unsigned long long f[num1];
  142.     for(int i=1; i<=num1; i++)
  143.     {
  144.         unsigned long long Fib;
  145.         Fib=Fib0+Fib1;
  146.         f[t]=Fib;
  147.         Fib0=Fib1;
  148.         Fib1=Fib;
  149.         t++;
  150.     }
  151.     for(int u=0; u<=t-1; u++)
  152.     {
  153.         int n=1,count=0;
  154.         h++;
  155.         unsigned long long b;
  156.         cout<<"["<<u+1<<"]    "<<"["<<f[u]<<"]"<<"\t";
  157.         {
  158.             ptr=(struct factorlist*)malloc(sizeof(struct factorlist));
  159.             ptr->llink=ptr;
  160.             ptr->rlink=ptr;
  161.             head=ptr;
  162.             tail=ptr;
  163.             b=f[u];
  164.             while(n<=f[u]/n)
  165.             {
  166.                 if(b%n!=0)
  167.                 {
  168.                     n++;
  169.                     continue;
  170.                 }
  171.                 else
  172.                 {
  173.                     count++;
  174.                     ptr=(struct factorlist*)malloc(sizeof(struct factorlist));
  175.                     if(head->rlink==head)
  176.                     {
  177.                         head->rlink=ptr;
  178.                         head->llink=ptr;
  179.                         ptr->rlink=head;
  180.                         ptr->llink=head;
  181.                         ptr->value1=n;
  182.                         ptr->value2=b/n;
  183.                         ptr->dif=((b/n)-n);
  184.                         tail=ptr;
  185.                     }
  186.                     else
  187.                     {
  188.                         ptr=(struct factorlist*)malloc(sizeof(struct factorlist));
  189.                         ptr->rlink=head;
  190.                         ptr->llink=tail;
  191.                         tail->rlink=ptr;
  192.                         head->llink=ptr;
  193.                         ptr->value1=n;
  194.                         ptr->value2=b/n;
  195.                         ptr->dif=((b/n)-n);
  196.                         tail=ptr;
  197.                     }
  198.                     n++;
  199.                 }
  200.             }
  201.             cur=head->rlink;
  202.             int a=0,c;
  203.             while(cur!=head)
  204.             {
  205.                 a++;
  206.                 cur=cur->rlink;
  207.             }
  208.             cur=head->rlink;
  209.             for(int k=1; k<=a-1; k++)
  210.             {
  211.                 for(int l=1; l<=a-k; l++)
  212.                 {
  213.                     if((cur->dif)<(cur->rlink->dif))
  214.                     {
  215.                         c=cur->dif;
  216.                         cur->dif=cur->rlink->dif;
  217.                         cur->rlink->dif=c;
  218.                     }
  219.                 }
  220.             }
  221.             cout<<"= "<<head->llink->value1<<"*"<<head->llink->value2<<"\t"<<"[inner loop= "<<count<<" time]"<<endl;
  222.         }
  223.     }
  224.     cout<<"outer loop= "<<h<<endl;
  225.     return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement