Advertisement
HEitor_Santos

lista recursão

Nov 24th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. 1)
  2. #include <iostream>
  3. using namespace std;
  4. int rev(int, int);
  5. int main()
  6. {
  7.     int n, a;
  8.     cin >> n;
  9.     cout << rev(a=0, n) <<endl;
  10.     return 0;
  11. }
  12. int rev(int a, int n)
  13. {
  14.     if (n==0)
  15.         return a;
  16.     else
  17.     {
  18.         return rev(a*10 +n%10, n/10);
  19.     }
  20. }
  21. 2)
  22. #include <iostream>
  23.  
  24. using namespace std;
  25. int soma(int);
  26. int main()
  27. {
  28.     int n;
  29.     cin >> n;
  30.     cout << soma(n) << endl;
  31.     return 0;
  32. }
  33. int soma(int n)
  34. {
  35.     if (n == 1)
  36.       return 1;
  37.     if (n > 1)
  38.     {
  39.       return n + soma(n-1);
  40.     }
  41. }
  42. 3)
  43. #include <iostream>
  44.  
  45. using namespace std;
  46. int pot(int, int);
  47. int main()
  48. {
  49.     int k, n;
  50.     cin >> k >> n;
  51.     cout << pot(k,n) << endl;
  52.     return 0;
  53. }
  54. int pot(int x, int y)
  55. {
  56.     if (y==0)
  57.         return 1;
  58.     if (y==1)
  59.         return x;
  60.     if (y>1)
  61.         return x * pot(x, y-1);
  62. }
  63. 4)
  64. #include <iostream>
  65.  
  66. using namespace std;
  67. int mdc(int, int);
  68. int main()
  69. {
  70.     int a, b;
  71.     cin >> a >> b;
  72.     cout << mdc(a,b) << endl;
  73.     return 0;
  74. }
  75. int mdc(int x, int y)
  76. {
  77.     if (y==0)
  78.         return x;
  79.     else
  80.         return mdc(y, x%y);
  81. }
  82. //pra calcluar mmc é (a*b)/mdc; mas n sei como faz a função pra isso;
  83. 5)
  84. #include <iostream>
  85.  
  86. using namespace std;
  87. int verif(int, int, int);
  88. int main()
  89. {
  90.     int n, j, x;
  91.     cin >> n >> j;
  92.     cout << verif(n, j, x=0) <<endl;
  93.     return 0;
  94. }
  95. int verif(int n, int j, int x)
  96. {
  97.     if (n==0)
  98.         return x;
  99.     if (n >= 1)
  100.     {
  101.         if(n%10 == j)
  102.             ++x;
  103.         verif(n/10, j, x);
  104.     }
  105. }
  106. 6)
  107. #include <iostream>
  108.  
  109. using namespace std;
  110. int multip_rec(int, int);
  111. int main()
  112. {
  113.     int x, y;
  114.     cin >> x >> y;
  115.     cout << multip_rec(x,y) << endl;
  116.     return 0;
  117. }
  118. int multip_rec(int n1, int n2)
  119. {
  120.     if (n2 == 0)
  121.         return 0;
  122.     if (n2 == 1)
  123.         return n1;
  124.     if (n2>1)
  125.         return n1 + multip_rec(n1, n2-1);
  126. }
  127. 7)
  128. #include <iostream>
  129.  
  130. using namespace std;
  131.  
  132. void cres(int, int);
  133. int main()
  134. {
  135.     int x, y;
  136.     cin >> x;
  137.     cres(x,y=0);
  138.     return 0;
  139. }
  140. void cres(int n, int h)
  141. {
  142.     if (n==0)
  143.         cout << 0 <<endl;
  144.     if (h <= n and n != 0)
  145.     {
  146.         cout << h  <<endl;
  147.         cres(n, h+1);
  148.     }
  149. }
  150. 7.1)
  151. #include <iostream>
  152.  
  153. using namespace std;
  154.  
  155. void cres(int);
  156. int main()
  157. {
  158.     int x;
  159.     cin >> x;
  160.     cres(x);
  161.     return 0;
  162. }
  163. void cres(int n)
  164. {
  165.     static int k = -1;
  166.     if (n==0)
  167.     {
  168.         cout << ++k <<endl;
  169.     }
  170.     else
  171.     {
  172.         cout << ++k  <<endl;
  173.         cres(n-1);
  174.     }
  175. }
  176. 8)
  177. #include <iostream>
  178.  
  179. using namespace std;
  180.  
  181. void cres(int, int);
  182. int main()
  183. {
  184.     int _aux;
  185.     cin >> _aux;
  186.     cres(_aux,0);
  187.     return 0;
  188. }
  189. void cres(int n, int _aux =0)
  190. {
  191.     if (n==0)
  192.         cout << n <<endl;
  193.     if (_aux <= n and n != 0)
  194.     {
  195.         if (_aux%2==0)
  196.            cout << _aux <<endl;
  197.         cres(n, _aux +1);
  198.     }
  199. }
  200. 9)
  201. #include <iostream>
  202.  
  203. using namespace std;
  204. void cres(int);
  205. int main()
  206. {
  207.     int x;
  208.     cin >> x;
  209.     cres(x);
  210.     return 0;
  211. }
  212. void cres(int n)
  213. {
  214.     if (n==0)
  215.         cout << n <<endl;
  216.     if (n>=1)
  217.     {
  218.         if (n%2==0)
  219.         cout << n <<endl;
  220.         cres(n-1);
  221.    }
  222. }
  223. 12)
  224. #include <iostream>
  225. #include <cmath>
  226. using namespace std;
  227. int fatex(int);
  228. int main()
  229. {
  230.     int n;
  231.     cin >> n;
  232.     cout << fatex(n) << endl;
  233.     return 0;
  234. }
  235. int fatex(int n)
  236. {
  237.     if (n >= 0)
  238.         return pow(n, fatex(n-1));
  239.     else
  240.         return n;
  241. }
  242.  
  243. 13)
  244. #include <iostream>
  245.  
  246. using namespace std;
  247. int trib(int);
  248. int main()
  249. {
  250.     int n;
  251.     cin>> n;
  252.     cout << trib(n) << endl;
  253.     return 0;
  254. }
  255. int trib(int n)
  256. {
  257.     if (n<=2)
  258.     {
  259.         if (n<=1)
  260.             return 0;
  261.         else
  262.             return 1;
  263.     }
  264.     else
  265.         return trib(n-1) + trib(n-2) + trib(n-3);
  266. }
  267. 14)
  268. #include <iostream>
  269.  
  270. using namespace std;
  271. int bin(int);
  272. int main()
  273. {
  274.     int ndec;
  275.     cin >> ndec;
  276.     cout << bin(ndec) << endl;
  277.     return 0;
  278. }
  279. int bin(int nbin)
  280. {
  281.     if ( nbin < 2 )
  282.         return nbin;
  283.     else
  284.         return ( 10 * bin( nbin / 2 ) ) + nbin % 2;
  285. }
  286. 15)
  287. #include <iostream>
  288.  
  289. using namespace std;
  290. int h(int,int);
  291. int main()
  292. {
  293.     int a, b;
  294.     cin >> a >> b;
  295.     cout << h(a,b) << endl;
  296.     return 0;
  297. }
  298. int h(int m,int n)
  299. {
  300.     if (n==1 or m==1)
  301.     {
  302.         if (n==1)
  303.             return m+1;
  304.         if (m==1)
  305.             return n+1;
  306.     }
  307.     else
  308.         return h(m,n-1) + h(m-1,n);
  309. }
  310. 16)
  311. #include <iostream>
  312.  
  313. using namespace std;
  314. int somaSerie(int,int,int);
  315. int main()
  316. {
  317.     int a, b, c;
  318.     cin>> a >> b >> c;
  319.     cout << somaSerie(a, b, c) << endl;
  320.     return 0;
  321. }
  322. int somaSerie(int i,int j,int k)
  323. {
  324.     if (i+k <= j)
  325.         return i + somaSerie(i+k, j, k);
  326.     else
  327.         return i;
  328. }
  329. 17)
  330. #include <iostream>
  331.  
  332. using namespace std;
  333. int somDig(int, int);
  334. int main()
  335. {
  336.     int a, n;
  337.     cin >> n;
  338.     cout << somDig(a=0, n) << endl;
  339.     return 0;
  340. }
  341. int somDig(int a, int n)
  342. {
  343.     if (n==0)
  344.         return a;
  345.     else
  346.         return somDig(a + n%10,n/10);
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement