免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1826 | 回复: 3
打印 上一主题 下一主题

在给定范围内随机产生规定个数的不同数字、看看谁写的最好。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-14 23:04 |只看该作者 |倒序浏览
在给定范围内随机产生规定个数的不同数字、看看谁写的最好。
假如我要求随机产生5个不大于200的数字。该怎么写啊。 我写了一个;看看有没有错误的地方:
#include"math.h"

int rangerand(int a , int b)
{int c[20];  //如果能申请到大小为b的数组更好 ,能吗?
     c[0]=rand(void)%a;
int j;
for(int i=1;i<b;i++){
     j=rand(void)%a;
  
for(int k=0;k<i;k++){
      if(a[k]!=j&&k=i-1) //查看与已经产生的数是否相同
      {c=j;
         cout<<c<<" "<<endl; //如果要求返回5个值,用return c吗 ?
       break;};          //要求跳出到外围循环
       continue;      //继续本循环  
       };
       };
       }
      然后调用rangerand(200, 5);

论坛徽章:
0
2 [报告]
发表于 2005-04-14 23:39 |只看该作者

在给定范围内随机产生规定个数的不同数字、看看谁写的最好。

khtrhtrhtr

论坛徽章:
0
3 [报告]
发表于 2005-04-14 23:53 |只看该作者

在给定范围内随机产生规定个数的不同数字、看看谁写的最好。

59 64 192 88 143

论坛徽章:
0
4 [报告]
发表于 2005-04-15 01:29 |只看该作者

在给定范围内随机产生规定个数的不同数字、看看谁写的最好。

应该给我满分吧!
#include <iostream>;
using namespace std;

void generate(int lower, int upper, int count)
{
int *x, i, total;
    total=upper-lower+1;
    x = new int[total];
    for (i=0;i<total;i++) x=lower+i;
    for (i=0;i<count;i++) {
        swap(x,x[rand()%(total-i)+i]);
        cout<<x<<" ";
    }
    cout<<endl;
    delete [] x;   
}

int main()
{
int l,u,c;   
    cout<<"Lower bound : ";
    cin>;>;l;
    cout<<"Upper bound : ";
    cin>;>;u;
    cout<<"How many : ";
    cin>;>;c;
    srand(time(NULL));
    generate(l,u,c);
    system("AUSE";
    exit(0);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP