如何在for循环中每次获取不同的数组?

发布于 2025-01-09 04:59:58 字数 2306 浏览 2 评论 0原文

#include<stdio.h>
#include<time.h>


int main(void)
{
srand(time(NULL));
int answer;
int treatment = rand() % 4;
printf("###발모제 찾기###\n\n");

int cntShowBottle = 0;
int prevCntShowBottle = 0;

int ptr[4] = { 0,0,0,0 };
int bottle[4] = { 0, 0, 0, 0 };
int isincluded = 0;

for (int i = 1; i <= 3; i++)
{
    do {
        cntShowBottle = rand() % 2 + 2;
    } while (cntShowBottle == prevCntShowBottle);
    prevCntShowBottle = cntShowBottle;

    printf(" %d 번째 시도 : ", i);

    for (int j = 0; j < cntShowBottle; j++)
    {
        int randBottle = rand() % 4;

        if (bottle[randBottle] == 0)
        {
            bottle[randBottle] = 1;
            if (randBottle == treatment)
            {
                isincluded = 1;
            }
        }
        else
        {
            j--;
        }
    }
}

    if (bottle[0] == ptr[0] && bottle[1] == ptr[1] && bottle[2] == ptr[2] && bottle[3] == ptr[3])
    {
        int bottle[4] = { 0,0,0,0 };
        for (int j = 0; j < cntShowBottle; j++)
        {
            int randBottle = rand() % 4;

            if (bottle[randBottle] == 0)
            {
                bottle[randBottle] = 1;
                if (randBottle == treatment)
                {
                    isincluded = 1;
                }
            }
            else
            {
                j--;
            }
        }
    }
    else
    {
        return 0;
    }



    for (int i = 0; i < 4; i++)
    {
        ptr[i] = bottle[i];
    }



    for (int k = 0; k < 4; k++)
    {
        if (bottle[k] == 1)
            printf("%d ", k + 1);
    }
        printf("번 물약을 머리에 바릅니다.\n\n");
        
        if (isincluded == 1)
        {
            printf("성공!\n");
        }
        else
        {
            printf("실패...\n");
        }
        
        printf("\n ...계속 하려면 아무키나 누르세요...");
        getchar(0);



printf("\n\n발모제는 몇 번? : ");
scanf_s("%d", &answer);

if (answer == treatment+1)
{
    printf("\n 정답! \n");
}
else
{
    printf("\n 실패! \n 정답은 %d 였습니다.\n", treatment+1);
}


return 0;
}

在此循环中, for (int j = 0; j < cntShowBottle; j++), 'bottle' 数组将为 [0,1,1,1] 或 [1,1,0,0] 等。在此循环中,如何获得不同的数组而不重叠(例如 [0,1,1,0] 和 [0,1,1,0])?我尝试比较每个元素,如果它需要重叠数组,则再次生成“瓶子”数组。但它运行不正常。请帮忙..

#include<stdio.h>
#include<time.h>


int main(void)
{
srand(time(NULL));
int answer;
int treatment = rand() % 4;
printf("###발모제 찾기###\n\n");

int cntShowBottle = 0;
int prevCntShowBottle = 0;

int ptr[4] = { 0,0,0,0 };
int bottle[4] = { 0, 0, 0, 0 };
int isincluded = 0;

for (int i = 1; i <= 3; i++)
{
    do {
        cntShowBottle = rand() % 2 + 2;
    } while (cntShowBottle == prevCntShowBottle);
    prevCntShowBottle = cntShowBottle;

    printf(" %d 번째 시도 : ", i);

    for (int j = 0; j < cntShowBottle; j++)
    {
        int randBottle = rand() % 4;

        if (bottle[randBottle] == 0)
        {
            bottle[randBottle] = 1;
            if (randBottle == treatment)
            {
                isincluded = 1;
            }
        }
        else
        {
            j--;
        }
    }
}

    if (bottle[0] == ptr[0] && bottle[1] == ptr[1] && bottle[2] == ptr[2] && bottle[3] == ptr[3])
    {
        int bottle[4] = { 0,0,0,0 };
        for (int j = 0; j < cntShowBottle; j++)
        {
            int randBottle = rand() % 4;

            if (bottle[randBottle] == 0)
            {
                bottle[randBottle] = 1;
                if (randBottle == treatment)
                {
                    isincluded = 1;
                }
            }
            else
            {
                j--;
            }
        }
    }
    else
    {
        return 0;
    }



    for (int i = 0; i < 4; i++)
    {
        ptr[i] = bottle[i];
    }



    for (int k = 0; k < 4; k++)
    {
        if (bottle[k] == 1)
            printf("%d ", k + 1);
    }
        printf("번 물약을 머리에 바릅니다.\n\n");
        
        if (isincluded == 1)
        {
            printf("성공!\n");
        }
        else
        {
            printf("실패...\n");
        }
        
        printf("\n ...계속 하려면 아무키나 누르세요...");
        getchar(0);



printf("\n\n발모제는 몇 번? : ");
scanf_s("%d", &answer);

if (answer == treatment+1)
{
    printf("\n 정답! \n");
}
else
{
    printf("\n 실패! \n 정답은 %d 였습니다.\n", treatment+1);
}


return 0;
}

in this loop, for (int j = 0; j < cntShowBottle; j++), 'bottle' array will be [0,1,1,1] or [1,1,0,0] etc. In this loop, how to get different array without overlapping(like [0,1,1,0] and again [0,1,1,0])?? I tried comparing each elements, if it takes overlapping array, makes 'bottle' array again. but it didn't run properly. please help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文