Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4.  
  5.  
  6. using namespace std;
  7. struct node{
  8.     node(int x = 0, node * y = NULL):num(x), next(y){
  9.     num = x;
  10.     next = y;
  11.     };
  12.     node(node const & copy)
  13.     : num(copy.num), next(new node(*copy.next)){};
  14.     int num;
  15.     node * next;
  16. };
  17.  
  18. int n;
  19. int main(){
  20.     cin >> n;
  21.     //здесь я просто создаю узлы, чтобы проверить
  22.     node * head = new node();
  23.     node * x = head;
  24.  
  25.     for (int i = 0; i < n - 1; ++i){
  26.         node * y = new node(i, x); 
  27.         x = y;
  28.     }
  29.  
  30.     head->next = x;
  31.     node * curr = head;
  32.     for (int i = 0; i < n + 1; ++i){ // проходимся n + 1 раз и мы в цикле
  33.         curr = curr->next;
  34.     }
  35.  
  36.     node * beg = curr; // фиксируем начало цикла
  37.     int k = 1;
  38.     curr = curr->next;
  39.     while (beg != curr){ // пока не пришли туда же бегаем по циклу
  40.         curr = curr->next;
  41.         ++k;
  42.     }
  43.     cout << k; //профит
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement