S1E31:局部变量和全局变量 洗牌程序的小疑问
原题:问题在第16行代码中。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle(int array[], int length);
void deal(int array[], int player);
void show(char names, int player);
void shuffle(int array[], int length)
{
int index, temp, i;
srand(time(NULL));
for (i = 0; i < length; i++)
{
index = rand() % (length - i) + i; //这里为什么要(length - i) + i,不能直接length吗?
if (index != i)
{
temp = array;
array = array;
array = temp;
}
}
}
void deal(int array[], int player)
{
int i, j, k = 0;
for (i = 0; i < 14; i++)
{
for (j = 0; j < 3; j++)
{
player = array;
}
}
}
void show(char names, int player)
{
int i, j, poker;
printf("\n方=方角,梅=梅花,红=红桃,黑=黑桃\n\n");
for (i = 0; i < 3; i++)
{
printf("%s手上的牌是:", names);
for (j = 0; j < 14; j++)
{
poker = player;
if (poker < 11 && 0 < poker)
{
printf("方%d ", poker);
}
else if (poker < 21 && 10 < poker)
{
printf("梅%d ", poker-10);
}
else if (poker < 31 && 20 < poker)
{
printf("红%d ", poker-21);
}
else if (poker < 41 && 30 < poker)
{
printf("黑%d ", poker-30);
}
else
{
switch (poker)
{
case 41: printf("方J "); break;
case 42: printf("方Q "); break;
case 43: printf("方K "); break;
case 44: printf("梅J "); break;
case 45: printf("梅Q "); break;
case 46: printf("梅K "); break;
case 47: printf("红J "); break;
case 48: printf("红Q "); break;
case 49: printf("红K "); break;
case 50: printf("黑J "); break;
case 51: printf("黑Q "); break;
case 52: printf("黑K "); break;
case 53: printf("小王 "); break;
case 54: printf("大王 "); break;
}
}
}
printf("\n\n");
}
}
int main(void)
{
int array;
int player;
int i, ch;
char names;
for (i = 0; i < 54; i++)
{
array = i+1;
}
for (i = 0; i < 3; i++)
{
printf("\n请输入%d号玩家的名字:", i+1);
scanf("%s", names);
}
do
{
shuffle(array, 40);
deal(array, player);
show(names, player);
printf("重新洗牌(Y/N)?");
do
{
ch = getchar(); //过滤输入缓冲区的其他字符
} while (ch != 'Y' && ch != 'N');
} while (ch == 'Y');
return 0;
} 兄弟,你认真的吗?5x4+1,能写成5x5吗? 倒戈卸甲 发表于 2020-4-12 17:02
兄弟,你认真的吗?5x4+1,能写成5x5吗?
不好意思...我运算符的优先级搞混了{:10_262:}
我以为等于 rand() % ((length - i) + i)
谢谢了 不过也不排除你不知道c++随机数用法,rand()%10表示0到10之间的随机数。但你其实你只要感觉,这个加法运算的优先级也不可能高。 倒戈卸甲 发表于 2020-4-12 17:05
不过也不排除你不知道c++随机数用法,rand()%10表示0到10之间的随机数。但你其实你只要感觉,这个加法运算 ...
我确实不懂(捂脸)
我也是刚刚在学c语言,谢谢了
页:
[1]