自己弄的现丑了
#include<stdio.h>
#include<stdlib.h>
int congFu();
int main()
{
//产生双色球
//产生红色球6个
int redRolls[6] = {0};//定义红球数组
int len = sizeof(redRolls) / sizeof(redRolls[0]);//数组长度
for (int i = 0; i < len; )//遍历蓝色球6个
{
int num = arc4random_uniform(33)+1;//产生1~33之间的随机数
int res = congFu(redRolls,len,num);
if (res)
{
redRolls[i] = num;
i++;
}
}
// 红球排序
/*
//外循环
for (int i = 0; i < len - 1; i++)
{
//遍历i之后的数值
for (int j = i+1; j < len; j++)
{
if (redRolls[i] > redRolls[j])
{
redRolls[i] = redRolls[i] + redRolls[j];
redRolls[j] = redRolls[i] - redRolls[j];
redRolls[i] = redRolls[i] - redRolls[j];
}
}
}
*/
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j<len - i - 1; j++) {
if (redRolls[j] > redRolls[j+1]) {
redRolls[j] = redRolls[j] + redRolls[j+1];
redRolls[j+1] = redRolls[j] - redRolls[j+1];
redRolls[j] = redRolls[j] - redRolls[j+1];
}
}
}
//产生1个蓝球
int blueRolls = arc4random_uniform(16)+1;//产生1~16之间的随机数
printf("产生红球为:\n");//打印产生红球
for (int i = 0; i < 6; i++)
{
printf("%d ",redRolls[i]);
}
printf("\n");
printf("产生蓝球球为:\n%d\n",blueRolls);
return 0;
}
/**
* 计算有没有重复数据产生
*
* @param redRolls 红球的数组
* @param len 数字长度
* @param num 产生的随机数
*
* @return 0 为重复 1 为不重复
*/
int congFu(int redRolls[],int len,int num)
{
for (int i = 0;i < 6;i++)
{
if (num == redRolls[i])
{
return 0;
}
}
return 1;
}
|
|