Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
static int64_t metallica[] = { 32538, 37741, 89540, 14627, 34272, 58765 };
const static int N = sizeof(metallica)/sizeof(*metallica);
static void fail(){
 puts("Nope!");
 exit(EXIT_FAILURE);
}
static void the_beatles(int64_t h, int64_t s, int64_t i){
 if ( h - i / 7 + 3 * s / 11 )
 fail();
}
static void anthrax(int z, int64_t j){
 int64_t p = j;
 for(; z<N; ++z) {
 if ((z % 2) == 0)
 continue;
 p += metallica[z];
 }
 if (p != 69857)
 fail();
}
static void eagles(int k, int64_t d){
 if (k<N) {
 if (k % 2)
 eagles(++k, d);
 else eagles(k + 1, d * metallica[k]);
 } else if (d != 1269229500)
 fail();
}
int main(){
 int64_t a, e, y;
 printf("Please enter the right three numbers: ");
 fflush(stdout);
 if (scanf("%" SCNd64 " %" SCNd64 " %" SCNd64, &a, &e, &y) != 3)
 fail();
 metallica[0] = a;
 metallica[5] = e;
 metallica[4] = y;
 anthrax(0, 15916);
 eagles(1, 9);
 the_beatles(a, e, y);
 puts("Exactly! Good job.");
}


What I have tried:

Tried different inputs! All were integer. Executed those function separately to understand those function! but seems like I far away from the solution!
Posted
Updated 6-Mar-22 8:09am
v3
Comments
Richard MacCutchan 6-Mar-22 11:13am    
You need to know which numbers are the correct ones.

In practice, you have two options:
1) Go and ask the person who wrote the code. This is the quick option ...
Or
2) Go with the slow option. You know what the end result must be: it's the value which doesn't make the code call fail. So start with the end - the the_beatles function and work out parameters which cause that to pass.
Then work back a step from that and work out what range of values will pass eagles
 
Share this answer
 
Comments
CPallini 6-Mar-22 13:03pm    
5.
As Eagles fan I give you some hints for a solution: rewrite the code to get it testable.
Change all function to give you some bool return code,
C++
static bool eagles(int k, int64_t d){
 if (k<N) {
 if (k % 2)
   return eagles(++k, d);
 else eagles(k + 1, d * metallica[k]);
 } else if (d != 1269229500)  { // some braces were missing here
 return false;
 }
 return true;
}
And rewrite the main with 3 for loops for a, e, y and let the computer do the job to run, til all return true. Make an output at end.

C++
if( anthrax(0, 15916) && eagles(1, 9) && the_beatles(a, e, y) ) {
// that is the case you are searching: make output to see the result
}
BTW: the eagles and anthrax input are constants. So they arent making much sense. Typo?
 
Share this answer
 
As Griff already suggested, you need to follow the code flow in order to discover what values are the good ones.
However, opposite to Griff, I suggest you to analyze the functions in the call order, i.e. anthrax, eagles, the_beatles.
Note: your task looks more tricky than it actual it is. For example let's consider the function anthrax
C
static void anthrax(int z, int64_t j){
 int64_t p = j;
 for(; z<N; ++z) {
 if ((z % 2) == 0)
 continue;
 p += metallica[z];
 }
 if (p != 69857)
 fail();
}
And its call in the main function
C
anthrax(0, 15916);

The function adds to 15916 all the odd items of the metallica array, namely
15916+37741+14627+e
and then checks if the sum is equal to 69857

Hence
e = 69857 - (15916+37741+14627) = 1573
is the only good value.

You can yourself find out the other values just making similar reasonings on eagles and the_beatles.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900