Advertisement
Guest User

eq.c

a guest
Mar 5th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <math.h>
  4.  
  5. #define INPUT "input.txt"
  6. #define OUTPUT "output.txt"
  7.  
  8. int sumOfDigits(int x) {
  9.     int res = 0;
  10.    
  11.     do {
  12.         res += (x % 10);
  13.         x /= 10;
  14.     } while(x);
  15.  
  16.     return res;
  17. }
  18.  
  19. int findSol(unsigned long long n) {
  20.     int x, min = -1;
  21.  
  22.     for(x = 0; x < 180; x++) {
  23.         if((x*x + sumOfDigits(x)*x - n) == 0) {
  24.             min = x;
  25.             break;
  26.         }
  27.     }
  28.  
  29.     return min;
  30. }
  31.  
  32. int main(int argc, char **argv) {
  33.     FILE *file_in, *file_out;
  34.     unsigned long long n;
  35.  
  36.     file_in = fopen(INPUT, "r");
  37.     file_out = fopen(OUTPUT, "w");
  38.  
  39.     assert(fscanf(file_in, "%llu", &n) == 1);
  40.     fprintf(file_out, "%d", findSol(n));
  41.            
  42.     fclose(file_in);
  43.     fclose(file_out);
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement