Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

c - Condition in 2D array is it true

Write on C

You are located in front of a stone wall with steps (two dimesional array) and each step has its own value- random integer between 1 and 500. You need to reach the top of the wall. To reach the top of the wall you need to do certain condition the top row should have a step with a value the difference between it and the step we are currently on should be less than or equal to a preset value K. Show on the screen "You won" if you can reach the top of the wall and which steps you climb. You start from a row with value of 0 for each step.

That's what I do, but sometimes doesn't work

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j, x = 1, y = 0, max = 0, n, k, br, flag = 0;
    int arr[3][3] = { { 0,   385, 16 },
                      { 370, 170, 117 },
                      { 469, 246, 288 } 
                    };
    int st[3][3];
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("
");
    }
    printf("
");

    printf("Insert K: ");
    scanf("%d", &k);
    printf("
");

    while (x < 3) {
        flag = 0;
        for (y = 0; y < 3; y++) {

            if ((arr[x][y] - max) <= k) {
                max = arr[x][y];
                printf("Step %d
", arr[x][y]);
                flag = 2;
                break;
            }
        }
        if (flag == 0) {
            printf("You lose");
            return 0;
        }
        x++;
    }
    printf("You won ");

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...