怎么还是实现不了我要实现的

avr 2005-10-02 06:56:56
高手们帮帮忙啊。
我的这段程序是想实现以下功能的,输入1个整数。在1—1600中:
1.1-1600随机生成一个数,当这个随机数与当前数count一致时输出950,即我的printA()。
2.1-400中随机生成一个数,401-800中随机生成一个数,801-1200随机生成一个数,1201-1600随机生成2个数。当这些随机数与当前数count一致时输出850,即我的printB()。
3.1-100中随机生成一个数,101-200中随机生成一个数,201-300中随机生成一个数,301-400中随机生成一个数,后面的401-1600每100都按顺序重复这些操作。当这些随机数与当前数count一致时输出750,即我的printC()。
4.1-100中随机生成2个数,101-200中随机生成3个数,后面的301-1600每100都按顺序重复这些操作。当这些随机数与当前数count一致时输出650,即我的printD()。
5.其余的都输出250,即我的printH()。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int count=1 ;

bool printA(int a)
{int k,a=1600;
k=rand()%a;
if(count==k)
{ cout<<"950"<<endl;
count++;
return true;
}
return false;
}


bool printB(int b)
{int k,m,n,b=400,step=0;
for(m=0;m<3;m++)
{k=rand()%b+step;
if(count==k){
cout<<"850"<<endl;
count++;
step+=400;
}
while(step==1200){
for(n=0;n<2;n++)
{k=rand()%b+step;
if(count==k){
cout<<"850"<<endl;
count++;}
}
return true;
}
return false;
}


bool printC(int c)
{int k,m,n,l,c=100,step=0;
for(m=0;m<12;m++)
{k=rand()%c+step;
if(count==k){
cout<<"750"<<endl;
count++;
step+=100;
}
while(step==300||step==700||step==1100)
{for(n=0;n<2;n++){
k=rand()%c+step;
if(count==k)
{cout<<"750"<<endl;
count++;}
}
}
step+=100;
while(step==1500)
{for(l=0;l<2;l++){
k=rand()%c+step;
if(count==k){
cout<<"750"<<endl;
count++;}
}
}
return true;
}
return false;
}


bool printD(int d)
{int k,m,n,l,d=100,step=0;
for(l=0;l<8;l++){
for(m=0;m<2;m++)
{k=rand()%d+step;
if(count==k){
cout<<"650"<<endl;
count++;}
}
step+=100;
for(n=0;n<3;n++)
{k=rand()%d+step;
if(count==k){
cout<<"650"<<endl;
count++;}
step+=100;
}
return true;
}
return false;
}


bool printH(int h)
{cout<<"250"<<endl;
count++;
return true;
}


int main()
{int n,x,y,w;
cin<<n<<endl;
(int) x=n/1600;
for(w=0;w<x;w++){
srand((unsigned)time(NULL));
for(y=1600;y>0;y--)
{if(printA(a)) continue;
if(printB(b)) continue;
if(printC(c)) continue;
if(printD(d)) continue;
printH(h);
}
return 0;
}
}
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaocai0001 2005-10-04
  • 打赏
  • 举报
回复
晕了~~~

楼主帖出来的代码好多错误,编译都通不过

还要问为什么实现不了~~~~~
jsjjms 2005-10-02
  • 打赏
  • 举报
回复
看糊了。不好意思。没有编译器...

单步跟踪一下应该可以解决,代码没有太复杂的地方。
jsjjms 2005-10-02
  • 打赏
  • 举报
回复
错了:-),是每次产生的随机序列都是一样的。
jsjjms 2005-10-02
  • 打赏
  • 举报
回复
bool printA(int a)
{int k,a=1600;
k=rand()%a;
if(count==k)
{ cout<<"950"<<endl;
count++;
return true;
}
return false;
}
你没有初始化种子,所以每次输出的结果都是一样的。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧