Advertisement
Alweys

fakin algoritmi

Nov 4th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. bool jeLiProst(int broj)
  7. {
  8.     if (broj == 0 || broj == 1)
  9.         return false;
  10.  
  11.     for (int i = 2; i <= broj / 2; ++i)
  12.             if (broj % i == 0)
  13.                 return false;
  14.     return true;
  15. }
  16.  
  17. int brojZnamenaka(int broj)
  18. {
  19.     int brojac = 0;
  20.     while(broj!=0)
  21.     {
  22.         brojac++;
  23.         broj/=10;
  24.     }
  25.     return brojac;
  26. }
  27.  
  28. bool zbrojZnamenaka(int broj, int n)
  29. {
  30.     int zbroj1 = 0, zbroj2 = 0;
  31.     int znam = brojZnamenaka(broj);
  32.     if(znam < n)
  33.         return false;
  34.  
  35.     for(int i=0;i<n;i++)
  36.     {
  37.         zbroj1+=broj%10;
  38.         broj/=10;
  39.     }
  40.     for(int i=0;i<znam;i++)
  41.     {
  42.         zbroj2+=broj%10;
  43.         broj/=10;
  44.     }
  45.  
  46.     if(zbroj1==zbroj2)
  47.         return true;
  48.  
  49.     return false;
  50. }
  51.  
  52. void zbrojZnamenakaUpetlji()
  53. {
  54.     int unos, N;
  55.     cout<<"Unos: "; cin>>unos;
  56.     cout<<"N: "; cin>>N;
  57.     while(true)
  58.     {
  59.         if(zbrojZnamenaka(unos,N) == 1)
  60.             break;
  61.         else
  62.         {
  63.             cout<<"Unos: "; cin>>unos;
  64.             cout<<"N: "; cin>>N;
  65.         }
  66.     }
  67. }
  68.  
  69. bool usporediZnamenke(int broj1, int broj2)
  70. {
  71.     bool prva[10] ={0}, druga[10]={0};
  72.     while(broj1)
  73.     {
  74.         prva[broj1%10]=1;
  75.         broj1/=10;
  76.     }
  77.     while(broj2)
  78.     {
  79.         druga[broj2%10]=1;
  80.         broj2/=10;
  81.     }
  82.     for (int i=0; i<10; i++){
  83.         cout<<prva[i]<<" ";
  84.     }
  85.     cout<<endl;
  86.     for (int i=0; i<10; i++)
  87.         cout<<druga[i]<<" ";
  88.     cout<<endl;
  89.     for (int i=0; i<10; i++){
  90.         if (prva[i]!=druga[i])
  91.             return false;
  92.     }
  93.  
  94.     return false;
  95. }
  96.  
  97. int obrnuti(int broj)
  98. {
  99.     int obrnuti=0;
  100.     int znam = brojZnamenaka(broj);
  101.     for(int i=0;i<znam;i++)
  102.     {
  103.         obrnuti*=10;
  104.         obrnuti+=broj%10;
  105.         broj/=10;
  106.     }
  107.     return obrnuti;
  108. }
  109.  
  110. int ukupnoVrijeme(int h, int m, int s)
  111. {
  112.     return h*3600 + m*60 + s;
  113. }
  114.  
  115. float sekundeuSate(int s)
  116. {
  117.     return s/3600.0;
  118. }
  119.  
  120. int sekundeuMinute(int s)
  121. {
  122.     if(s/60 > 60)
  123.         return s/60 - 60;
  124.  
  125.     return s/60;
  126. }
  127.  
  128. void ispisiNputa(int n)
  129. {
  130.     static int broj = 0;
  131.     cout<<broj<<endl;
  132.     broj++;
  133.     if(broj == n+1)
  134.         return;
  135.     ispisiNputa(n);
  136. }
  137.  
  138. int main()
  139. {
  140.     //cout << jeLiProst(4) << endl;
  141.     //cout << brojZnamenaka(4323) << endl;
  142.     //cout << zbrojZnamenaka(1214,1) << endl;
  143.     //zbrojZnamenakaUpetlji();
  144.     //cout<<usporediZnamenke(1221, 12119)<<endl;
  145.     //cout<<obrnuti(4003)<<endl;
  146.     //cout<<ukupnoVrijeme(2,35,21)<<endl;
  147.     //cout<<sekundeuSate(12336)<<endl;
  148.     //cout<<sekundeuMinute(1072)<<endl;
  149.     ispisiNputa(1000);
  150.  
  151.     return 0;
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement