Home > OS >  how to Find the three input values that satisfy the constraints imposed by the functions
how to Find the three input values that satisfy the constraints imposed by the functions

Time:05-27

i should provide the correct input: (f,k,z) to have the program print the sentence: Exactly! Good Job. The input should satisfy the 3 methods Here we have 3 methods, each one either succeds or fails (if failed it prints: nope) I tried to execute metallica and aerosmith method by hand and solve equations but am stuck there...

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
static int64_t ac_dc[] = {10143, 54893, 47109, 24350, 17669, 82062};
const static int N = sizeof(ac_dc) / sizeof(*ac_dc);
static void fail() {
    puts("Nope!");
    exit(EXIT_FAILURE);
}
static void linkin_park(int64_t t, int64_t v, int64_t s) {
    if(t - s / 7   3 * v / 11) fail();
}
static void metallica(int o, int64_t j) {
    int64_t g = j;
    for(; o < N;   o) {
        if((o % 2) == 0) continue;
        g  = ac_dc[o];
    }
    if(g != 94857) fail();
}
static void aerosmith(int d, int64_t n) {
    if(d < N) {
        if(d % 2)
            aerosmith(  d, n);
        else
            aerosmith(d   1, n * ac_dc[d]);
    } else if(n != 540151794)
        fail();
}
int main() {
    int64_t f, k, z;
    printf("Please enter the right three numbers: ");
    fflush(stdout);
    if(scanf("%" SCNd64 " %" SCNd64 " %" SCNd64, &f, &k, &z) != 3) fail();
    ac_dc[0] = f;
    ac_dc[5] = k;
    ac_dc[4] = z;
    metallica(0, 14041);
    aerosmith(1, 9);
    linkin_park(f, k, z);
    puts("Exactly! Good job.");
}

CodePudding user response:

Think of it as system of 3 equations with 3 unknowns: f, k, z.

The program assigns these values to a global array

static int64_t ac_dc[] = {f, 54893, 47109, 24350, z, k}; // conceptually

The linkin_park function is basically an equation of 3 variables:

f - z/7   3*k/11 = 0

We'll use it at the end to find the last unknown, given the other two.

metallica is "easier":

g   ac_dc[1]   ac_dc[3]   ac_dc[5] = 94857
14041   54893   24350   k = 94857
k = 1573

aerosmith is more challenging, but if we trace the parameters of the recursive calls:

d n n passed
1 9 9
2 9 9 * ac_dc[2] = 9 * 47,109 = 423,981
3 423,981 423,981
4 423,981 423,981 * z
z = 540151794 / 423981

That's enough to find all the unknowns.

  • Related