dooooooduo

algo-adding-comb_benchmark.c

Jun 21st, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. /**
  2.  * Problem:
  3.  * Find all possibilities which satisfy the equation 'abc+def=ghi',
  4.  * each digit is selected from 1-9, and they are all unique.
  5.  * @author    Δοδύο
  6.  * @version   0.1
  7.  * @changelog
  8.  *      - V0.1: Basic implementation
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14.  
  15. void comb (char remain[9], int index, int carry, char a[3], char b[3], char c[3], int *count) {
  16.     if (index > 2) {
  17.         int aa = a[2] * 100 + a[1] * 10 + a[0];
  18.         int bb = b[2] * 100 + b[1] * 10 + b[0];
  19.         int cc = c[2] * 100 + c[1] * 10 + c[0];
  20.         (*count)++;
  21.         printf("%d + %d = %d\n", aa, bb, cc);
  22.         return;
  23.     }
  24.     int i, j;
  25.     for (i = 0; i < 9; i++) {
  26.         if (remain[i] == -1) continue;
  27.         for (j = 0; j < 9; j++) {
  28.             // exclude symmetries (symmetries cause 0 sum)
  29.             if (remain[j] == -1 || i == j || j == 8 - i) continue;
  30.             int cr = 0;
  31.             char cp[9], na[3], nb[3], nc[3];
  32.             strcpy(cp, remain);
  33.             strcpy(na, a);
  34.             strcpy(nb, b);
  35.             strcpy(nc, c);
  36.  
  37.             na[index]  = remain[i];
  38.             nb[index]  = remain[j];
  39.             nc[index]  = remain[i] + remain[j] + carry;
  40.             cr         = nc[index] >= 10;
  41.             nc[index] %= 10;
  42.  
  43.             if (na[index] == nc[index] ||
  44.                 nb[index] == nc[index] ||
  45.                 nc[index] ==  0 ||        // exclude 0
  46.                 index == 2 && cr == 1)    // four digits number is impossible
  47.                 continue;
  48.                
  49.             int found = 0, k;
  50.             for (k = 0; k < 9; k++) {
  51.                 if (nc[index] == cp[k]) {
  52.                     found = 1;
  53.                     break;
  54.                 }
  55.             }
  56.  
  57.             if (!found) // the number does not exist in the remaining list
  58.                 continue;
  59.  
  60.             cp[i] = cp[j] = cp[k] = -1;
  61.             comb(cp, index + 1, cr, na, nb, nc, count);
  62.         }
  63.     }
  64. }
  65.  
  66. // from http://www.cnblogs.com/OctoptusLian/p/6675885.html
  67. int aaa[10], book[10], total = 0;
  68. void dfs(int step){
  69.     int i;
  70.     if(step==10) {
  71.         if (aaa[1] * 100 + aaa[2] * 10 + aaa[3] +
  72.             aaa[4] * 100 + aaa[5] * 10 + aaa[6] ==
  73.             aaa[7] * 100 + aaa[8] * 10 + aaa[9]) {
  74.             total++;
  75.         }
  76.         return;
  77.     }
  78.     for (i = 1; i <= 9; i++) {
  79.         if(book[i] == 0) {
  80.             aaa[step] = i;
  81.             book[i]   = 1;
  82.             dfs(step + 1);
  83.             book[i]   = 0;
  84.         }
  85.     }
  86.     return;
  87. }
  88.  
  89. int main () {
  90.     struct timespec stime, etime;
  91.     clock_gettime(CLOCK_MONOTONIC, &stime);
  92.    
  93.     printf("My algo:\n");
  94.     char set[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  95.     char a[3]   = {-1, -1, -1};
  96.     char b[3]   = {-1, -1, -1};
  97.     char c[3]   = {-1, -1, -1};
  98.     int  count = 0;
  99.     comb(set, 0, 0, a, b, c, &count);
  100.     printf("There are %d unique combinations.\n", count / 2);
  101.     clock_gettime(CLOCK_MONOTONIC, &etime);
  102.     printf("Time spent: %ld nsec\n\n", etime.tv_nsec - stime.tv_nsec);
  103.    
  104.     clock_gettime(CLOCK_MONOTONIC, &stime);
  105.     printf("Depth-first-search:\n");
  106.     dfs(1);
  107.     printf("There are %d unique combinations.\n", total / 2);
  108.     clock_gettime(CLOCK_MONOTONIC, &etime);
  109.     printf("Time spent: %ld nsec\n", etime.tv_nsec - stime.tv_nsec);
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment