#include<time.h>
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
int x[30];
int i,b;
int r,p;
char name[11][20] = {"Umbrella","Pillow blanket","Neck pillow","Towels","Temperature glass","Cloth bag","Multipurpose bag","Slippers","Portable fan","Rice box"};
for(i = 0;i < 30;++i)
{
printf("Enter your student ID (%d/30): ", i+1);
scanf("%d", &x
);
if(x
== 0 && i < 15)
{
if ( x
== 0 )
{
printf("Please enter your ID again\n");
printf("Enter your student ID (%d/30): ", i+1);
scanf("%d", &x
);
}
}
else if(x
== 0 && i >= 15)
{
break;
}
}
srand(time(NULL));
for (b = 0 ; b < 10;++b)
{
r = rand()%i;
p = rand()+1;
printf("Winner is Number %d student ID %d prize is %d %s\n",r+1,x[r],p,name[p-1]);
}
return 0;
}
ช่วยดูโค้ดให้หน่อยครับภาษา C ผมต้องการให้การสุ่ม ผลลัพธ์จะต้องออกมาไม่ซำ้กันอ่ะครับ
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
int x[30];
int i,b;
int r,p;
char name[11][20] = {"Umbrella","Pillow blanket","Neck pillow","Towels","Temperature glass","Cloth bag","Multipurpose bag","Slippers","Portable fan","Rice box"};
for(i = 0;i < 30;++i)
{
printf("Enter your student ID (%d/30): ", i+1);
scanf("%d", &x);
if(x == 0 && i < 15)
{
if ( x == 0 )
{
printf("Please enter your ID again\n");
printf("Enter your student ID (%d/30): ", i+1);
scanf("%d", &x);
}
}
else if(x == 0 && i >= 15)
{
break;
}
}
srand(time(NULL));
for (b = 0 ; b < 10;++b)
{
r = rand()%i;
p = rand()+1;
printf("Winner is Number %d student ID %d prize is %d %s\n",r+1,x[r],p,name[p-1]);
}
return 0;
}