生成一定范围内的随机数(c++例题)

1.题目详情:生成一定范围内的随机数

  • 借助随机生成函数3-9之间任意整数450个,将结果显示到屏幕上,每6个一行
  • 统计3-9分别出现了多少次
  • 去除450个数中的重复值,并打印输出
  • 冒泡排序算法,求450个数的升序排序

2.解题小知识

srand((unsigned int)time(0));
int a = rand() % 51 + 13; 产生13~63的随机数 分析:取模即取余, rand()%51+13我们可以看成两部分: rand()%51是产生 0~50 的随机数,后面+13保证 a 最小只能是13,最大就是 50+13=63。

3.产生3-9之间的随机数

//产生3-9之间的随机数
void randomNumber(int arr[], int b[], int n) {
	int j = 1, a = 0, count = 0;
	//int arr[450] = {0};
	//srand((int)time(0));
	srand((unsigned int)time(0));
	cout << "产生450个3-9之间的随机数" << endl;
	while (j <= n) {
		//产生3~9的随机数
		a = rand() % 7 + 3;
		cout << a << " ";
		//如果是每行是6个,则换成下一行
		if (j % 6 == 0){
			cout << endl;
			count++;
		}

		//arr[]和b[]存储产生的随机数
		b[j - 1] = arr[j - 1] = a;
		j++;
	}
	cout << "共计" << count << "行"<<endl;
}

4.统计3-9分别出现了多少次

//统计3-9分别出现了多少次
int Count_x(int arr[], int n) {
	int count[1001];
	for (int i = 0; i < n; i++) {
		switch (arr[i]) {
			case 3:
				count[3]++;
				break;
			case 4:
				count[4]++;
				break;
			case 5:
				count[5]++;
				break;
			case 6:
				count[6]++;
				break;
			case 7:
				count[7]++;
				break;
			case 8:
				count[8]++;
				break;
			case 9:
				count[9]++;
				break;
		}
	}
	cout << "统计3-9分别出现了多少次" << endl;
	for (int i = 3; i < 10; i++) {

		cout << i << "出现" << count[i] << "次" << endl;
	}
	return 0;

}

5.去除450个数中的重复值,并打印输出

//去除450个数中的重复值,并打印输出
int removeSame(int arr[], int n) {
	int temp;
	for (int i = 0; i < n; i++) {	          //循环判断数组中每一个数
		for (int j = i + 1; j < n; j++) {	  //判断arr[i]后边的数是否和arr[i]相等
			if (arr[i] == arr[j]) {
				for (temp = j; temp < n; temp++) {
					//将arr[j]后面的元素全往前移一个位置
					arr[temp] = arr[temp + 1];
				}
				j--;	//arr[j+1]取代a[j]位置,为使下次从arr[j+1]开始查找,j减一(为使j保持不变)
				n--;	//数组长度减一
			}
		}
	}
	cout << "还剩" << n << "个数" << endl;
	cout << "去除450个数中的重复值,并打印输出结果为" << endl;
	for (int i = 0; i < n; i++) {

		cout << arr[i] << " ";
	}
	return 0;

}

6.冒泡排序算法,求450个数的升序排序

//冒泡排序算法,求450个数的升序排序
int bubblesort(int b[], int n) {
	//int temp;
	for (int i = 0; i < n - 1; i++) {	     	//确定排序趟数
		for (int j = 0; j < n - i - 1; j++) {   //确定比较次数
			if (b[j] > b[j + 1]) {          //若为逆序
				//交换
//				temp = arr[j];
//				arr[j] = arr[j + 1];
//				arr[j + 1] = temp;
				swap(b[j], b[j + 1]);       //交换
			}
		}
	}
	cout << "冒泡排序算法,求450个数的升序排序" << endl;
	for (int i = 0; i < n; i++) {
		cout << b[i] << " ";
	}
	return 0;
}

7.完整代码C++

#include<bits/stdc++.h>
using namespace std;
/*
	生成一定范围内的随机数
	1.借助随机生成函数3-9之间任意整数450个,将结果显示到屏幕上,每6个一行
	2.统计3-9分别出现了多少次
	3.去除450个数中的重复值,并打印输出
	4.冒泡排序算法,求450个数的升序排序

*/
/*
    srand((unsigned int)time(0));
    int a = rand() % 51 + 13;
	产生13~63的随机数
	分析:取模即取余,
	rand()%51+13我们可以看成两部分:
	rand()%51是产生 0~50 的随机数,后面+13保证 a 最小只能是 13,最大就是 50+13=63。
*/

//产生3-9之间的随机数
void randomNumber(int arr[], int b[], int n) {
	int  a = 0, count = 0;
	//int arr[450] = {0};
	//srand((int)time(0));
	srand((unsigned int)time(0));
	cout << "产生"<<n<<"个3-9之间的随机数" << endl;
	for (int j = 1; j <= n; j++) {
		//产生3~9的随机数
		a = rand() % 7 + 3;
		cout << a << " ";
		//如果是每行是6个,则换成下一行
		if (j % 6 == 0) {
			cout << endl;
			count++;
		}

		//arr[]和b[]存储产生的随机数
		b[j - 1] =	arr[j - 1] = a;

	}
	cout << "共计" << count << "行" << endl;
}
//统计3-9分别出现了多少次
int Count_x(int arr[], int n) {
	int count[1001];
	for (int i = 0; i < n; i++) {
		switch (arr[i]) {
			case 3:
				count[3]++;
				break;
			case 4:
				count[4]++;
				break;
			case 5:
				count[5]++;
				break;
			case 6:
				count[6]++;
				break;
			case 7:
				count[7]++;
				break;
			case 8:
				count[8]++;
				break;
			case 9:
				count[9]++;
				break;
		}
	}
	cout << "统计3-9分别出现了多少次" << endl;
	for (int i = 3; i < 10; i++) {

		cout << i << "出现" << count[i] << "次" << endl;
	}
	return 0;

}

//去除450个数中的重复值,并打印输出
int removeSame(int arr[], int n) {
	cout << "去除"<<n<<"个数中的重复值,并打印输出结果为" << endl;
	int temp;
	for (int i = 0; i < n; i++) {	          //循环判断数组中每一个数
		for (int j = i + 1; j < n; j++) {	  //判断arr[i]后边的数是否和arr[i]相等
			if (arr[i] == arr[j]) {
				for (temp = j; temp < n; temp++) {
					//将arr[j]后面的元素全往前移一个位置
					arr[temp] = arr[temp + 1];
				}
				j--;	//arr[j+1]取代a[j]位置,为使下次从arr[j+1]开始查找,j减一(为使j保持不变)
				n--;	//数组长度减一
			}
		}
	}
	
	for (int i = 0; i < n; i++) {
		cout << arr[i] << " ";
	}
	cout<<endl;
	cout << "还剩" << n << "个数" << endl;
	return 0;
}

//冒泡排序算法,求450个数的升序排序
int bubblesort(int b[], int n) {
	//int temp;
	for (int i = 0; i < n - 1; i++) {	     	//确定排序趟数
		for (int j = 0; j < n - i - 1; j++) {   //确定比较次数
			if (b[j] > b[j + 1]) {          //若为逆序
				//交换
//				temp = arr[j];
//				arr[j] = arr[j + 1];
//				arr[j + 1] = temp;
				swap(b[j], b[j + 1]);       //交换
			}
		}
	}
	cout << "冒泡排序算法,求"<<n<<"个数的升序排序" << endl;
	for (int i = 0; i < n; i++) {
		cout << b[i] << " ";
	}
	return 0;
}

int main() {
	int n = 450;
	int arr[n];
	int b[n];
	randomNumber(arr, b, n);
	Count_x(arr, n);
	removeSame(arr, n);
	cout << endl;
	bubblesort(b, n);
	//sort(arr, arr + n);
	return 0;
}

8.结果

在这里插入图片描述

本文可以参考另外一篇讲解rand()和srand()产生随机数的
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值