gpt4 book ai didi

c - 替换数组中的元素

转载 作者:行者123 更新时间:2023-11-30 15:42:30 26 4
gpt4 key购买 nike

我有一个问题让我非常痛苦。这段代码的目的是用整数值填充数组,同时防止字符串等......但它不能防止重复,但我尝试用新的值替换数字例如数字输入6个整数1、2、2、3、4、5

我的代码将让我用另一个数字替换位置 1 处的 2。我想要它做的是不要再次重复相同的数字,例如请在位置 1 处替换 2。我不希望用户再次输入 2...并且我想让它仔细检查数组的工作情况,如果是否存在重复的数字,谢谢。

        system("clear");
printf("\nEntering Winning Tickets....\n");
nanosleep((struct timespec[]){{1, 0}}, NULL);

system("clear");
char userInput[256];
char c;

int duplicationArray[6] = {-1, -1, -1, -1, -1, -1};
for (i = 0; i < 6; i++)
{
printf("\nPlease enter the %d winning ticket number!(#'s must be between 1-49): ", i+1);
fgets(userInput, 256, stdin);
if ((sscanf(userInput, "%d %c", &winningNumbers[i], &c) != 1 || (winningNumbers[i] <= 0) || winningNumbers[i] >= 50))
{
printf("\nInvalid Input.\n") ;
nanosleep((struct timespec[]){{0, 350000000}}, NULL);
system("clear");
i = i - 1;
}
}

for (i = 0; i < 6 - 1; ++i)
{

min = i;
for (j = i+1; j < 6; ++j)
{
if (winningNumbers[j] < winningNumbers[min])
min = j;
}

temp = winningNumbers[i];
winningNumbers[i] = winningNumbers[min];
winningNumbers[min] = temp;

}

for (i = 0; i < 6; i++)
{
if (winningNumbers[i] == winningNumbers[i+1])
{
duplicationArray[i] = i;
duplicationCounter++;

}

else
{
duplicationCounter--;
}
}


if (duplicationCounter > -6)

{

for (i = 0; i < 6; i++)
{
int j, min, temp;
min = i;
for (j = i+1; j < 6; ++j)
{
if (duplicationArray[j] > duplicationArray[min])
min = j;
}

temp = duplicationArray[i];
duplicationArray[i] = duplicationArray[min];
duplicationArray[min] = temp;

}

for (i = 0; i < 6; i++)
{
if (duplicationArray[i] == -1)
{
zeroCounter++;
}
}
int resize = (6 - zeroCounter)+1;

for (i = 0; i <= resize; i++)
{
if (duplicationArray[i] == -1)
{
i++;
}

else if (duplicationArray[i] != -1)
{
system("clear");

printf("\nDuplicated numbers has been dected in your array. ");
printf("\nPlease replace the number %d at postion %d with another number: ", winningNumbers[duplicationArray[i]], duplicationArray[i]);
fgets(userInput, 256, stdin);

if ((sscanf(userInput, "%d %c", &winningNumbers[duplicationArray[i]], &c) != 1 || (winningNumbers[i] <= 0) || winningNumbers[i] >= 50))
{
printf("\nInvalid Input.\n") ;
nanosleep((struct timespec[]){{0, 350000000}}, NULL);
system("clear");
i = i - 1;
}
}
}

duplicationCounter = 0;

for (i = 0; i < 6; i++)
{
if (winningNumbers[i] == winningNumbers[i+1])
{
duplicationArray[i] = i;
duplicationCounter++;

}

else
{
duplicationCounter--;
}

}

printf("%d, ", duplicationCounter);

}

最佳答案

#include <stdio.h>
#include <stdint.h>

#define DATA_SIZE 6

int main(void){
char userInput[256];
int inputNum, winningNumbers[DATA_SIZE];
uint64_t table = 0;
int i=0;
while(i<DATA_SIZE){
printf("\nPlease enter the %d winning ticket number!(#'s must be between 1-49): ", i+1);
fgets(userInput, sizeof(userInput), stdin);
if(sscanf(userInput, "%d", &inputNum) != 1 || inputNum <= 0 || inputNum >= 50)
continue;
uint64_t bit = 1 << inputNum;
if(table & bit)
continue;
table |= bit;
winningNumbers[i++] = inputNum;
}
for(i=0;i<DATA_SIZE;++i)
printf("%d ", winningNumbers[i]);
printf("\n");
return 0;
}

关于c - 替换数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com