Please explain the number of c language cases.

Asked 2 years ago, Updated 2 years ago, 30 views

#include <stdio.h>


int money(int m)

{

     int a = 1, b = 1, c = 1;

     for (a = 1; 500 * a + 700 * b + 400 * c <= m; a++)
     {
        for (b = 1; 500 * a + 700 * b + 400 * c <= m; b++)
        {
           for (c = 1; 500 * a + 700 * b + 400 * c <= m; c++)
           {
              if (500 * a + 700 * b + 400 * c == m)
                 printf("%d cream bread, %d shrimp crackers, %d coke \n", a, b, c);
           }
           c = 1;
         }
         b = 1;
         c = 1;
    }
}

int main(void)

{

    int m;
    printf ("The amount you currently own: ");
    scanf_s("%d", &m);
    money(m);
    printf ("How would you like to buy it?"\n");
    system("pause");
    return 0;
}

I am not sure why c=1 and b=1, c=1 were given among the main(intm) functions in the source code above. For your information, I didn't plan the source code above.

c

2022-09-22 20:17

1 Answers

Although the c language is concise, the for syntax is not so neat.

In the grammatical structure of for (initial value; value up to repetition; value up to initial value)

Giving the values of a, b, and c by 1 is setting the initial value.

If for is in for, it is a two-dimensional data representation.

You can think of Gugudan as a representative.

for(int x = 2; x < 10; x++){
    for(int y  = 1; y < 10; y++){
        printf("%d * %d = %d\n",  x, y, x * y);
    }
}

위의 코드는

2 * 1 = 2

2 * 2 = 4

...

9 * 1 = 9

9 * 2 = 18

...

9 * 9 = 81

It's going to be it.

Please compile and run the code above and learn how x and y values change with one-line debugging.


2022-09-22 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.