Advertisement
J00ker

CAN 1

Sep 13th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. ifstream fin("f.in");
  11. ofstream fout("f.out");
  12.  
  13. unsigned int SumCif(int n) {
  14.     //cout << n << "\t" << (n % 10) << "\t" << abs(n % 10) << "\n";
  15.     //printf("%d\t%d\t%d\n", n, (n % 10), (int)abs(n % 10));
  16.     if(!n) return 0;
  17.     else return abs(n % 10) + SumCif(n / 10);
  18. }
  19.  
  20. unsigned int SumCif2(int n) {
  21.     int s = 0;
  22.     while(n) {
  23.         //cout << n << "\t" << (n % 10) << "\t" << abs(n % 10) << "\n";
  24.         //printf("%d\t%d\t%d\n", n, (n % 10), (int)abs(n % 10));
  25.         s += abs(n % 10);
  26.         n /= 10;
  27.     }
  28.     return s;
  29. }
  30.  
  31. unsigned int NrMax(int n) {
  32.     int nr = 0;
  33.     int cif[10] = {0};
  34.  
  35.     while(n) {
  36.         cif[ (int)abs(n % 10) ]++;
  37.         n /= 10;
  38.     }
  39.  
  40.     for(int i = 9; i >= 0; i--)
  41.         for(int j = cif[i]; j > 0; j--)
  42.             nr = nr * 10 + i;
  43.  
  44.     return nr;
  45. }
  46.  
  47. bool Palindrom(int n) {
  48.     int cif = 0;
  49.     int n2 = n = (int)abs(n);
  50.  
  51.     while(n2) {
  52.         cif++;
  53.         n2 /= 10;
  54.     }
  55.  
  56.     int c1, c2;
  57.     while(n) {
  58.         c1 = n % 10;
  59.         c2 = n / pow(10, (cif - 1));
  60.         cout << n << "\t" << c1 << "\t" << c2 << "\t" << cif << "\n";
  61.         if(c1 != c2) return false;
  62.  
  63.         n %= (int)pow(10, (cif - 1));
  64.         n /= 10;
  65.         cif -= 2;
  66.     }
  67.     return true;
  68. }
  69.  
  70. bool Par(int n) {
  71.     return !(n % 2);
  72. }
  73.  
  74. bool Prim(int n) {
  75.     if(n <= 1) return false;
  76.  
  77.     for(int i = 2; i <= n / 2; i++)
  78.         if(!(n % i)) return false;
  79.  
  80.     return true;
  81. }
  82.  
  83. bool PatrPerf(int n) {
  84.     return (sqrt(n) * sqrt(n) == n);
  85. }
  86.  
  87. bool Sir(int *s, int n) {
  88.     for(int i = 1; i < n; i++)
  89.         fout << s[i] << "\t" << ((Par(s[i])) ? "PAR" : "IMPAR") << "\t" << (Prim(s[i]) ? "PRIM" : "NEPRIM") << "\t" << (PatrPerf(s[i]) ? "PATRAT PERFECT" : "NOPE") << "\n";
  90. }
  91.  
  92. int main() {
  93.     int x;
  94.  
  95.     //while(fin >> x) cout << SumCif(x) << "\n\n";
  96.     //while(fin >> x) printf("%d\n\n", SumCif2(x));
  97.  
  98.     //for(int i = 1; i <= 64; i++) printf("%d\t%o\t%x\t%p\n", i, i, i, i);
  99.  
  100.     //cout << (unsigned int)-1;
  101.  
  102.     //while(fin >> x) fout << NrMax(x) << "\n";
  103.  
  104.     //while(fin >> x) fout << Palindrom(x) << "\n";
  105.  
  106.     int sir_n = 0;
  107.     int *sir, *sir_aux;
  108.     sir = sir_aux = NULL;
  109.     while(fin >> x) {
  110.         sir_n++;
  111.         if(!(sir = (int*) realloc (sir, sir_n * sizeof(int)))) return -1;
  112.         //sir = (int*) realloc (sir, sir_n * sizeof(int));
  113.         sir[sir_n - 1] = x;
  114.     }
  115.     Sir(sir, sir_n);
  116.  
  117.     free(sir);
  118.     free(sir_aux);
  119.  
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement