C++中经典的“给小学生出加法考试题”
#include<iostream>#include<stdlib.h>intAddTest(int,int);intAddTest(inta,intb){returna...
#include <iostream>
#include <stdlib.h>
int AddTest(int,int);
int AddTest(int a,int b)
{return a+b;}
using namespace std;
int main()
{
inti,a,b,c,d=0,e=0,f;
for(i=1;i<=10;i++)
{
a=rand()%10+1;
b=rand()%10+1;
cout<<a<<"+"<<b<<"="<<endl;
cout<<"Pleaseinput the corrcet result:"<<endl;
cin>>c;
f=AddTest(a,b);
if(c!=f)
{
cout<<"Notcorrect!"<<endl;
e=e+1;
}
else
{
cout<<"Right!"<<endl;
d=d+10;
}
}
cout<<"Thescore is:"<<d<<endl;
cout<<"Thewrong resluts are:"<<e<<endl;
return0;
}
以上是我自己写的,但是为什么每次系统出的题都是一样的呢? 展开
#include <stdlib.h>
int AddTest(int,int);
int AddTest(int a,int b)
{return a+b;}
using namespace std;
int main()
{
inti,a,b,c,d=0,e=0,f;
for(i=1;i<=10;i++)
{
a=rand()%10+1;
b=rand()%10+1;
cout<<a<<"+"<<b<<"="<<endl;
cout<<"Pleaseinput the corrcet result:"<<endl;
cin>>c;
f=AddTest(a,b);
if(c!=f)
{
cout<<"Notcorrect!"<<endl;
e=e+1;
}
else
{
cout<<"Right!"<<endl;
d=d+10;
}
}
cout<<"Thescore is:"<<d<<endl;
cout<<"Thewrong resluts are:"<<e<<endl;
return0;
}
以上是我自己写的,但是为什么每次系统出的题都是一样的呢? 展开
3个回答
展开全部
在a=rand()%10+1;之前加上一句,产生随机数种子就可以了:
srand(unsigned(time(0)));
加个头文件:
#include <ctime>
http://zhidao.baidu.com/link?url=LkmrYvmEJlh77ZBf_mkURjtsjErhO2FGVQ6X1lmq51L8Je1bAM0vgalxNbb1sS1dU89lefngyoQTw9c5eRdVOa
srand(unsigned(time(0)));
加个头文件:
#include <ctime>
http://zhidao.baidu.com/link?url=LkmrYvmEJlh77ZBf_mkURjtsjErhO2FGVQ6X1lmq51L8Je1bAM0vgalxNbb1sS1dU89lefngyoQTw9c5eRdVOa
展开全部
#include <iostream>
#include <stdlib.h>
#include <time.h>
int AddTest(int,int);
int AddTest(int a,int b)
{return a+b;}
using namespace std;
int main()
{
int i,a,b,c,d=0,e=0,f;
for(i=1;i<=10;i++)
{
srand(time(NULL));
a=rand()%10+1;
b=rand()%10+1;
cout<<a<<"+"<<b<<"="<<endl;
cout<<"Pleaseinput the corrcet result:"<<endl;
cin>>c;
f=AddTest(a,b);
if(c!=f)
{
cout<<"Notcorrect!"<<endl;
e=e+1;
}
else
{
cout<<"Right!"<<endl;
d=d+10;
}
}
cout<<"Thescore is:"<<d<<endl;
cout<<"Thewrong resluts are:"<<e<<endl;
return 0;
}
#include <stdlib.h>
#include <time.h>
int AddTest(int,int);
int AddTest(int a,int b)
{return a+b;}
using namespace std;
int main()
{
int i,a,b,c,d=0,e=0,f;
for(i=1;i<=10;i++)
{
srand(time(NULL));
a=rand()%10+1;
b=rand()%10+1;
cout<<a<<"+"<<b<<"="<<endl;
cout<<"Pleaseinput the corrcet result:"<<endl;
cin>>c;
f=AddTest(a,b);
if(c!=f)
{
cout<<"Notcorrect!"<<endl;
e=e+1;
}
else
{
cout<<"Right!"<<endl;
d=d+10;
}
}
cout<<"Thescore is:"<<d<<endl;
cout<<"Thewrong resluts are:"<<e<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-02
展开全部
字母表示(a+b)c=a+(b+c)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询