【问题标题】:How to save a matrix indexes [closed]如何保存矩阵索引
【发布时间】:2016-05-14 10:27:32
【问题描述】:

我正在练习 c,但这段代码遇到了一些问题: 我想做一个电影院票务程序。 这些程序每次都会获得一个特定的数字,它会寻找第一个免费座位,如果没有它应该打印一个按摩。 座位必须相互连接并在同一排。 需要打印座位的索引以及标记为已占用。 我将 1 标记为已使用,将 0 标记为未使用。 我每次都需要你的帮助来保存索引。

#include <stdio.h>
#include <string.h>


main()
{
    int i, j;
    int Freeseats = 0;
    int arr[10][20]= {{0}};

    for(i=0; i<10; i++)
    {
        for(j=0; j<20; j++)
        {
            printf("%d", arr[i][j]);
        }
        printf("\n");
    }
    while(1)
    {

        int num, n=0;
        scanf("%d",&num);
        for(i=0; i<10; i++)
        {
            for(j=0; j<20; j++)
            {
                if(arr[i][j] == 0 && Freeseats < num)
                {
                    Freeseats++;

                }
                else{
                    Freeseats = 0;
                }
                if(Freeseats == num)
                {

                }
            }

        }
    }   
}

【问题讨论】:

  • 不清楚你在问什么。要在程序中“保存”某些内容,只需将其分配给一个或多个变量即可。
  • 每次 Freeseats 变量增加时,我都想保存那个准确的索引,而每次 Freeseats 变 0 时,我想删除这些索引。像一个临时变量,但我不知道在这种特殊情况下如何做

标签: c arrays matrix


【解决方案1】:

我认为你的方法是错误的。您不需要保存任何索引。

你的任务是找到一排有 N 个连续的空闲座位。所以你需要一个额外的循环来计算连续空闲座位的数量。

类似:

#include <stdio.h>
#include <string.h>

#define SEATS_PER_ROW 20
#define ROWS 10

main()
{
    int i, j, k, max;
    int arr[ROWS][SEATS_PER_ROW]= {{0}};

    for(i=0; i<10; i++)
    {
        for(j=0; j<20; j++)
        {
            printf("%d", arr[i][j]);
        }
        printf("\n");
    }
    while(1)
    {
        printf("Please enter the number of seats needed\n");

        int num, n=0;
        if (1 != scanf("%d",&num))
        {
            printf("Illegal input - stop program\n");
            break;
        }
        printf("Searching for %d seats\n", num);
        if (num > SEATS_PER_ROW)
        {
            printf("We don't have that many seats in a single row\n");
            continue;
        }

        int found = 0;
        for (i=0; i<10 && found != 1; i++)
        {
            found = 0;
            for (j=0; j<(20-num+1) && found != 1; j++)
            {
                found = 0;
                // Check for num consecutive free seats starting at i, j
                for (k=0; k<num; k++)
                {
                    if(arr[i][j+k] != 0)
                    {
                        // Can't sit here
                        found = -1;
                        break;
                    }
                }
                if (found == 0)
                {
                    // We found a place - starting at i, j
                    // Mark them as used
                    found = 1;
                    printf("Seats assinged in row %d : seat %d to %d\n", i, j, j+num-1);
                    for(k=0; k<num; k++)
                    {
                        arr[i][j+k] = 1;
                    }

                    // Debug print
                    //for(i=0; i<10; i++)
                    //{
                    //    for(j=0; j<20; j++)
                    //    {
                    //        printf("%d", arr[i][j]);
                    //    }
                    //    printf("\n");
                    //}

                }
            }

        }
        if (found != 1)
        {
            printf("So many seats are not available\n");
        }
    }   
    return 0;
}

有输入:

10
15
2
6
20
3
21
9

输出是:

Please enter the number of seats needed
Searching for 10 seats
Seats assinged in row 0 : seat 0 to 9
Please enter the number of seats needed
Searching for 15 seats
Seats assinged in row 1 : seat 0 to 14
Please enter the number of seats needed
Searching for 2 seats
Seats assinged in row 0 : seat 10 to 11
Please enter the number of seats needed
Searching for 6 seats
Seats assinged in row 0 : seat 12 to 17
Please enter the number of seats needed
Searching for 20 seats
Seats assinged in row 2 : seat 0 to 19
Please enter the number of seats needed
Searching for 3 seats
Seats assinged in row 1 : seat 15 to 17
Please enter the number of seats needed
Searching for 21 seats
We don't have that many seats in a single row
Please enter the number of seats needed
Searching for 9 seats
Seats assinged in row 3 : seat 0 to 8
Please enter the number of seats needed
Searching for 15 seats
Seats assinged in row 4 : seat 0 to 14

查看https://ideone.com/36zjuC

【讨论】:

  • 谢谢!我刚刚编译了你的代码,它不起作用。只有我输入的第一个号码获得了他的座位,但其余的无论是什么号码都没有得到它。再加上程序不会让我知道有什么座位。
  • @Natezone - ups,有一个错误 - 答案已更新
猜你喜欢
  • 2021-12-01
  • 1970-01-01
  • 2018-09-09
  • 2019-04-16
  • 2018-05-12
  • 1970-01-01
  • 2011-12-08
  • 2022-07-10
  • 2019-10-20
相关资源
最近更新 更多