[求助]如何随机选择一种算法?
问题很简单:int a=rand()%100+1;
int b=rand()%100+1;
int num=a+b,num1=a-b;
cin>>num3;
if(num3==num)
cout<<"正确";
问:如何才能让num=a+b,num1=a-b;也随机出现呢?
也就是 随机出现加减法。
randomly geneerate 0 and 1 with 50-to-50 chance. if it is 0, do +; if it is 1, do -.
算法随机,你可以吧算法作为数组。
typedef int (*Comp)(int,int);
int Sum(int a,int b)
{
return a+b;
}
int Sub(int a,int b)
{
return a-b;
}
void main()
{
srand(time(0));
int a=rand()%100+1;
int b=rand()%100+1;
Comp c[]={Sum,Sub};
int num=rand()%(sizeof(c)/sizeof(int));
cin>>num3;
if(num3==num)
cout<<"正确";
}
[此贴子已经被作者于2007-6-26 10:54:15编辑过]