Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- nine-five-four_zero.c
- Task from Graham Holmes:
- https://www.facebook.com/groups/242739573272753/permalink/874176896795681/
- NINE - FIVE - FOUR = ZERO
- Replace each letter with a digit from 0 to 9 to make this sum add up.
- No two letters can have the same digit,
- and no two digits can share the same letter.
- Leading zeroes aren't permitted.
- What is the value of ZERO?
- Result:
- Value of ZERO is 1564.
- There are 4 solutions:
- NINE - FIVE - FOUR = ZERO
- 8085 - 3025 - 3496 = 1564
- NINE - FIVE - FOUR = ZERO
- 8085 - 3095 - 3426 = 1564
- NINE - FIVE - FOUR = ZERO
- 8785 - 3725 - 3496 = 1564
- NINE - FIVE - FOUR = ZERO
- 8785 - 3795 - 3426 = 1564
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // Returns 1 if all digits are different from each other, otherwise returns 0.
- int allDifferent(int n, int i, int e, int f, int v, int o, int u, int r, int z)
- {
- int arr[9]={n,i,e,f,v,o,u,r,z};
- int len = sizeof(arr) / sizeof(arr[0]);
- int x, y;
- for(x=0; x<len; x++)
- for(y=x+1; y<len; y++)
- if( arr[x] == arr[y] )
- return 0;
- return 1;
- }
- int main(void)
- {
- int n, i, e, f, v, o, u, r, z, nine, five, four, zero, solved=0, number_of_solutions=0;
- for(n=1; n<10; n++)
- for(i=0; i<10; i++)
- for(e=0; e<10; e++)
- for(f=1; f<10; f++)
- for(v=0; v<10; v++)
- for(o=0; o<10; o++)
- for(u=0; u<10; u++)
- for(r=0; r<10; r++)
- for(z=1; z<10; z++)
- {
- nine = n*1000 + i*100 + n*10 + e;
- five = f*1000 + i*100 + v*10 + e;
- four = f*1000 + o*100 + u*10 + r;
- zero = z*1000 + e*100 + r*10 + o;
- if( nine - five - four == zero &&
- allDifferent(n,i,e,f,v,o,u,r,z) )
- {
- solved = 1;
- number_of_solutions++;
- printf("\n NINE - FIVE - FOUR = ZERO \n");
- printf("\n %d%d%d%d - %d%d%d%d - %d%d%d%d = %d%d%d%d \n\n",
- n,i,n,e, f,i,v,e, f,o,u,r, z,e,r,o );
- }
- }
- if( !solved )
- printf("\n There is no solution. \n");
- else
- printf("\n There are %d solutions. \n", number_of_solutions);
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment