或
鳄鱼,兔子,骆驼,斑马,大象 等等提前谢谢你:D
修改:
#include <stdio.h>
#include <string.h>
#define SIZE 10
int main ()
{
char arr2[SIZE][20] = { "tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat" };
int x = 0;
srand(time(NULL));
while (x < SIZE - 5)
{
arr2 [x][20] = rand ();
printf ("%s\n", arr2[x]);
x++;
}
system ("pause");
return 0;
}
答案 0 :(得分:3)
将单词放入数组中。在正确的范围内生成5个(或其他)伪随机数(0..array_size-1)。使用这些数字从数组中选择单词。
答案 1 :(得分:1)
您可以执行以下操作:
k
,您可以像arr2[k]
一样访问数组元素。rand
函数来完成,您可能以错误的方式调用了该函数check[SIZE] = {0,}
,然后在打印arr2[k]
之前,检查是否check[k]==0
然后打印该值。打印后设置arr2[k]=1
。完成此操作后,请粘贴代码。希望你能理解这个问题的逻辑。
答案 2 :(得分:0)
为了便于说明,这是C#,但我确信你可以很容易地转换为C语言:
static void Main(string[] args)
{
string[] words =
{ "tiger", "lion", "elephant", "zebra", "horse",
"camel", "deer", "crocodile", "rabbit", "cat" };
string randomWords = RandomWords.GenerateRandomWordString(5, words);
}
public static class RandomWords
{
private static readonly Random _random = new Random((int)DateTime.Now.Ticks);
public static string GenerateRandomWordString(int numWords, string[] words)
{
int maxlen = words.Length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numWords; i++)
{
// Note: in .NET, Random.Next(0, max) returns
// a value in range zero to max - 1
sb.Append(words[_random.Next(0, maxlen)]);
sb.Append(" ");
}
return sb.ToString().Trim();
}
}