只实现了部分,还请各位耐心帮忙实现全部

avr 2005-10-19 09:21:47
本程序是想实现以下功能,在1-1600中:
1.1-1600中随机取1个数,在这个数位上输出“950”。即printA()函数。
2.1-400中随机取1个数,401-800中随机取1个数,801-1200中随机取1个数,1201-1600中随机取2个不重复的数。在这些数位上输出“850”。即printB()函数。
3.1-100中随机取1个数,101-200中随机取1个数,201-300中随机取1个数,301-400中随机取2个不重复的数。后面的401-1600中每100单位内都按上面的规律随机取数。在这些数位上输出“750”。即printC()函数。
4.1-100中随机取2个不重复的数,101-200中随机取3个不重复的数。后面的201-1600中每100单位内都按上面的规律随机取数。在这些数位上输出“650”。即printD()函数。
5.1-1600中每100单位内随机取5个不重复的数。在这些数位上输出“550”。即printE()函数。
6.1-1600中每100单位内随机取10个不重复的数。在这些数位上输出“450”。即printF()函数。
7.1-100中随机取27个不重复的数,101-200中随机取28个不重复的数,201-300中随机取28个不重复的数,301-400中随机取28个不重复的数。后面的401-1600中每100单位内都按上面的规律随机取数。在这些数位上输出“350”。即printG()函数。
8.在其余的数位上都输出“250”。

程序中全局变量A,B,C,D,E,F,G就是用来察看每100单位内随机取的数是否和规定的一致。现在每100单位内随机取的数是按上面的规律。但是在有些数位上输出了2个或3个数,如:1508.650 550 450。我想让程序在每个数位上只输出1个数,且还要遵守上面的取数规则。应该如何解决。代码如下,比较长,还请各位耐心帮忙。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int A=0,B=0,C=0,D=0,E=0,F=0,G=0;

bool printA(int n)
{
static int current;
static int randdata[1];
static int randnum;
if(n %1600==1)
{
int i=0, k, temp, j;
randnum = 1;
while(i<randnum)
{
temp = rand()%1600+1;
k = 0;
while(k < i && temp > randdata[k])
++k;
if(temp == randdata[k])
continue;
j = i;
while(j > k)
{
randdata[j] = randdata[j-1];
--j;
}
randdata[j] = temp;
++i;
}
current = 0;
}
if(n == randdata[current])
{
cout<<"950"<<" ";
++A;
++current;
return true;
}
return false;
}


bool printB(int n)
{
static int current;
static int randdata[2];
static int randnum;
if(n % 400 == 1)
{
int base, i=0, k, temp, j;
base = n - n % 100;
if(n % 1600== 1201)
randnum = 2;
else
randnum = 1;
while(i<randnum)
{
temp = rand()% 100 + base + 1;
k = 0;
while(k < i && temp > randdata[k])
++k;
if(temp == randdata[k])
continue;
j = i;
while(j > k)
{
randdata[j] = randdata[j-1];
--j;
}
randdata[j] = temp;
++i;
}
current = 0;
}
if(n == randdata[current])
{
cout<<"850"<<" ";
++B;
++current;
return true;
}
return false;
}


bool printC(int n)
{
static int current;
static int randdata[2];
static int randnum;
if(n % 100 == 1)
{
int base, i=0, k, temp, j;
base = n - n % 100;
if(n % 400 == 301)
randnum = 2;
else
randnum = 1;
while(i<randnum)
{
temp = rand()% 100 + base + 1;
k = 0;
while(k < i && temp > randdata[k])
++k;
if(temp == randdata[k])
continue;
j = i;
while(j > k)
{
randdata[j] = randdata[j-1];
--j;
}
randdata[j] = temp;
++i;
}
current = 0;
}
if(n == randdata[current])
{
cout<<"750"<<" ";
++C;
++current;
return true;
}
return false;
}


bool printD(int n)
{
static int current;
static int randdata[3];
static int randnum;
if(n % 100 == 1)
{
int base, i=0, k, temp, j;
base = n - n % 100;
if(n % 200 == 101)
randnum = 3;
else
randnum = 2;
while(i<randnum)
{
temp = rand()% 100 + base + 1;
k = 0;
while(k < i && temp > randdata[k])
++k;
if(temp == randdata[k])
continue;
j = i;
while(j > k)
{
randdata[j] = randdata[j-1];
--j;
}
randdata[j] = temp;
++i;
}
current = 0;
}
if(n == randdata[current])
{
cout<<"650"<<" ";
++D;
++current;
return true;
}
return false;
}


bool printE(int n)
{
static int current;
static int randdata[5];
static int randnum;
if(n % 100 == 1)
{
int base, i=0, k, temp, j;
base = n - n % 100;
randnum = 5;
while(i<randnum)
{
temp = rand()% 100 + base + 1;
k = 0;
while(k < i && temp > randdata[k])
++k;
if(temp == randdata[k])
continue;
j = i;
while(j > k)
{
randdata[j] = randdata[j-1];
--j;
}
randdata[j] = temp;
++i;
}
current = 0;
}
if(n == randdata[current])
{
cout<<"550"<<" ";
++E;
++current;
return true;
}
return false;
}


bool printF(int n)
{
static int current;
static int randdata[10];
static int randnum;
if(n % 100 == 1)
{
int base, i=0, k, temp, j;
base = n - n % 100;
randnum = 10;
while(i<randnum)
{
temp = rand()% 100 + base + 1;
k = 0;
while(k < i && temp > randdata[k])
++k;
if(temp == randdata[k])
continue;
j = i;
while(j > k)
{
randdata[j] = randdata[j-1];
--j;
}
randdata[j] = temp;
++i;
}
current = 0;
}
if(n == randdata[current])
{
cout<<"450"<<" ";
++F;
++current;
return true;
}
return false;
}


bool printG(int n)
{
static int current;
static int randdata[28];
static int randnum;
if(n % 100 == 1)
{
int base, i=0, k, temp, j;
base = n - n % 100;
if(n % 400 == 1)
randnum = 27;
else
randnum = 28;
while(i<randnum)
{
temp = rand()% 100 + base + 1;
k = 0;
while(k < i && temp > randdata[k])
++k;
if(temp == randdata[k])
continue;
j = i;
while(j > k)
{
randdata[j] = randdata[j-1];
--j;
}
randdata[j] = temp;
++i;
}
current = 0;
}
if(n == randdata[current])
{
cout<<"350"<<" ";
++G;
++current;
return true;
}
return false;
}

bool printH()
{
cout<<"250"<<" ";
return true;
}

int main()
{
int y;
bool result;
srand((unsigned)time(NULL));
for(y=1; y<=1600; ++y)
{
cout<<y<<".";
result=printA(y);
result=printB(y)||result;
result=printC(y)||result;
result=printD(y)||result;
result=printE(y)||result;
result=printF(y)||result;
result=printG(y)||result;
if(!result)
printH();
if(y%100==0){
cout<<endl;
cout<<"A="<<A<<endl;
cout<<"B="<<B<<endl;
cout<<"C="<<C<<endl;
cout<<"D="<<D<<endl;
cout<<"E="<<E<<endl;
cout<<"F="<<F<<endl;
cout<<"G="<<G<<endl;}
}
return 0;
}

...全文
92 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
K 2005-10-20
  • 打赏
  • 举报
回复
连注释都没有,我晕了
northwolves 2005-10-20
  • 打赏
  • 举报
回复
看得头晕.

33,311

社区成员

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

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