Advertisement
icatalin

functii diverse 15.10

Oct 15th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. 1. Functie care returneaza suma cifrelor pare ale lui x. x-parametru
  2.  
  3. int sumapare(int x)
  4. {
  5.     int s=0;
  6.     while (x)
  7.     {
  8.         if (x%2==0)
  9.         s=s+s%10;
  10.         x/=10;
  11.     }
  12.     return s;
  13. }
  14.  
  15. 2. Functie care verifica daca numarul transmis ca parametru are toate cifrele impare.
  16.  
  17. int cifimpare (int x)
  18. {
  19. while (x)
  20. {
  21. if ((x%10)%2==0)
  22. return 0;
  23. x=x/10;
  24. }
  25. return 1;    
  26.  
  27. }
  28.  
  29. 3. Funcite care verifica daca nr transmis ca parametru are cel putin o cifra para.
  30.  
  31. int par(int x)
  32. {
  33.     while (x)
  34.     {
  35.         if((x%10)%2==0)
  36.         return 1;
  37.         x=x/10;
  38.     }
  39.     return 0;
  40. }
  41.  
  42. 4. Functie care verifica daca un nr transmis ca parametru are cifrele distincte doua cate doua.
  43.  
  44. int distincte(int x)
  45. {
  46.     int fv[10]={0},i,cif
  47.     while (x)
  48.     {
  49.         cif=cif%10;
  50.         fv[cif]++;
  51.         if (fv[cif]>1)
  52.         return 0;
  53.         x=x/10;
  54.     }
  55.     return 1;
  56. }
  57.  
  58. 5. O functie care primeste ca parametru un nr de max 9 cifre, intreg, si returneaza numarul obtinut prin permutarea cifrelor
  59. sale cu o pozitie.
  60.  
  61. varianta 1
  62.  
  63. int permutari_stanga(int x)
  64. {
  65.     int x,p,nr=0,xx
  66.     xx=x;
  67.     while (xx)
  68.     {
  69.         nr++;
  70.         x=x/10;
  71.     }
  72.     p=pow(10,nr-1);
  73.     x=x/p+(x%p)*10;
  74.     return x;
  75. }
  76.  
  77. varianta 2
  78.  
  79. /* functiw care permuta spre dr. cifrele unui vector cu o pozitie */
  80. void vector(int v[10],int k)
  81. {
  82.     int aux,i;
  83.     aux=v[k];
  84.     for (i=k-1;i>=1;i--)
  85.     v[i+1]=v[i];
  86.     v[1]=aux;
  87. }
  88.  
  89. /* inversez cifrele in vecotr */
  90. void inversez(int v[10],int &k, int x)
  91. {
  92.     int cif;
  93.     k=0;
  94.     while (x)
  95.     {
  96.         cif=x/10;
  97.         k++;
  98.         v[k]=cif;
  99.         x=x/10;
  100.     }
  101. }
  102.  
  103. /* formez numarul */
  104.  
  105. int rezolvare (int x)
  106. {
  107.     int v[10],k,ogl=0,i;
  108.     rezolvare (v,k,x);
  109.     vector (v,k);
  110.     for (i=k;i>=1;i--)
  111.     ogl=ogl*10+v[i];
  112.     return ogl;
  113.    
  114.    
  115. }
  116.  
  117. 6. Se citeste un nr de max 9 cifre. /* functiw care permuta spre dr. cifrele unui vector cu o pozitie */
  118. void vector(int v[10],int k)
  119. {
  120.     int aux,i;
  121.     aux=v[k];
  122.     for (i=k-1;i>=1;i--)
  123.     v[i+1]=v[i];
  124.     v[1]=aux;
  125. }
  126.  
  127. /* inversez cifrele in vecotr */
  128. void inversez(int v[10],int &k, int x)
  129. {
  130.     int cif;
  131.     k=0;
  132.     while (x)
  133.     {
  134.         cif=x/10;
  135.         k++;
  136.         v[k]=cif;
  137.         x=x/10;
  138.     }
  139. }
  140.  
  141. /* formez numarul */
  142.  
  143. int rezolvare (int x)
  144. {
  145.     int v[10],k,ogl=0,i;
  146.     rezolvare (v,k,x);
  147.     vector (v,k);
  148.     for (i=k;i>=1;i--)
  149.     ogl=ogl*10+v[i];
  150.     return ogl;
  151.    
  152.    
  153. 7. Verificat daca e super prim. Un nr se numeste prim daca atat el, cat si toate permutarile sunt nr prime. Ex: 133 313 331
  154.  
  155. #include <iostream>
  156. #include <cmath>
  157. #include <fstream>
  158.  
  159. using namespace std;
  160.  
  161. int nrcifre(int x)
  162. {
  163.     int nr=0;
  164.     while (x)
  165.     {
  166.         x=x/10;
  167.         nr++;
  168.     }
  169.     return nr;
  170. }
  171.  
  172. int nrprim (int x)
  173. {
  174.     int i;
  175.     if (x==0 || x==1 || (x%2==0 && x!=0))
  176.     return 0;
  177.     for (i=3;i<=sqrt(x);i=i+2)
  178.     if (x%i==0)
  179.     return 0;
  180.     return 1;
  181. }
  182.  
  183. int rotire (int x,int nr)
  184. {
  185.     int p;
  186.     p=pow(10,nr-1);
  187.     x=x/p+(x%p)*10;
  188.     return x;
  189. }
  190.  
  191. int extraprim(int x)
  192. {
  193.     int k=nrcifre(x),i;
  194.     for (i=1;i<=k;i++)
  195.     {
  196.         if (nrprim(x)==0)
  197.         return 0;
  198.     }
  199.     x=rotire(x,k);
  200.     return 1;
  201. }
  202.  
  203. int main()
  204. {
  205. int x;
  206. cout<<"x= ";
  207. cin>>x;
  208. if (extraprim(x)==1)
  209. cout<<"DA";
  210. else
  211. cout<<"NU";
  212. return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement