Advertisement
oaktree

ones-simple.c

Nov 22nd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. static void prime_factor(int n);
  5. static void make_into_ones(int x);
  6.  
  7. int main(void) {
  8.  
  9.     int n; scanf("%d", &n);
  10.     prime_factor(n);
  11.     printf("\n");
  12.     return 0;
  13. }
  14.  
  15. static void prime_factor(int n) {
  16.     if (n < 0) n *= -1;
  17.  
  18.     int x = 2;
  19.     while (n > 1) {
  20.         if (n % x == 0) {
  21.             n /= x;
  22.  
  23.             make_into_ones(x);
  24.             if (n != 1) printf("*");
  25.  
  26.             x = 2;
  27.         } else {
  28.             x++;
  29.         }
  30.     }
  31. }
  32.  
  33. static void make_into_ones(int x) {
  34.     if (x < 1) return;
  35.     printf("(");
  36.     for (int i = 1; i < x; i++) {
  37.         printf("1+");
  38.     }
  39.     printf("1)");
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement