fork(7) download
  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. int used = 0;
  7. char str[32];
  8. void eval()
  9. {
  10. char *p, *q;
  11. int a,b,c;
  12. a = strtol(str, 0, 10);
  13. q = strchr(str, '='); c = strtol(q + 1, 0, 10);
  14. p = strchr(str, '+');
  15. if(p) {
  16. b = strtol(p + 1, 0, 10);
  17. if(p[1] == '0' || q[1] == '0') return;
  18. if((a + b) == c){
  19. printf(" %s\n", str);
  20. }
  21. }
  22.  
  23. p = strchr(str, '-');
  24. if(p) {
  25. b = strtol(p + 1, 0, 10);
  26. if(p[1] == '0' || q[1] == '0') return;
  27. if((a - b) == c){
  28. printf(" %s\n", str);
  29. }
  30. }
  31. }
  32.  
  33. void recur(int offs)
  34. {
  35. int c,n,i,old_used=used;
  36. char old_str[32];
  37. if((c = str[offs]) == 0) {
  38. eval();
  39. return;
  40. }
  41. if(! isalpha(c)) {
  42. recur(offs + 1);
  43. return;
  44. }
  45.  
  46. strcpy(old_str, str);
  47. for(n=((offs == 0)?1:0); n<10; n++) {
  48. if(!(used & (1 << n))) {
  49. used |= (1 << n);
  50. for(i=0; str[i]; i++) {
  51. if(str[i] == c) str[i] = '0' + n;
  52. }
  53. recur(offs + 1);
  54. used = old_used;
  55. strcpy(str, old_str);
  56. }
  57. }
  58. }
  59.  
  60. main()
  61. {
  62. strcpy(str, "SEND+MORE=MONEY");
  63. printf("\n%s\n", str);
  64. recur(0);
  65.  
  66. strcpy(str, "WWWDOT-GOOGLE=DOTCOM");
  67. printf("\n%s\n", str);
  68. recur(0);
  69. return 0;
  70. }
  71.  
  72.  
Success #stdin #stdout 1.98s 2052KB
stdin
Standard input is empty
stdout
SEND+MORE=MONEY
  9567+1085=10652

WWWDOT-GOOGLE=DOTCOM
  777589-188103=589486
  777589-188106=589483