|
@@ -1,83 +1,83 @@
|
|
|
-/*HEAD homework 1 HOMEWORK */
|
|
|
-/*==============================================================================
|
|
|
-2024年度随机数排序程序设计作业
|
|
|
-================================================================================
|
|
|
-File description:
|
|
|
-这是第一次作业演示的样板程序。
|
|
|
-提交者:徐子昂
|
|
|
-邮 箱: dutxza@163.com
|
|
|
-
|
|
|
-================================================================================
|
|
|
-Date Name Description of Change
|
|
|
-05/20/2024 Xu Ziang Created.
|
|
|
-$HISTORY$
|
|
|
-================================================================================
|
|
|
-*/
|
|
|
-
|
|
|
-#include <iostream>
|
|
|
-#include <ctime> // 闅忔満鏁扮�瀛愬ご鏂囦欢
|
|
|
-using namespace std;
|
|
|
-
|
|
|
-void generate_rand(int Size/*I*/, int num[]/*I*/)
|
|
|
-{
|
|
|
- for (int i = 0; i < Size; i++)
|
|
|
- {
|
|
|
- num[i] = rand() % 100 + 1;
|
|
|
- }
|
|
|
-
|
|
|
- cout << "鐢熸垚鐨勯殢鏈烘暟缁勶細" << endl;
|
|
|
- for (int i = 0; i < Size; i++)
|
|
|
- {
|
|
|
- cout << num[i] << " ";
|
|
|
- }
|
|
|
- cout << endl;
|
|
|
-}
|
|
|
-
|
|
|
-void sort_num(int Size/*I*/, int num[]/*I*/)
|
|
|
-{
|
|
|
-
|
|
|
- for (int i = 0; i < Size - 1; i++) // 对比总轮数
|
|
|
- {
|
|
|
- for (int j = 0; j < Size - i - 1; j++) // 姣忚疆瀵规瘮娆℃暟
|
|
|
- {
|
|
|
- if (num[j] > num[j + 1])
|
|
|
- {
|
|
|
- // 浜ゆ崲鍏冪礌
|
|
|
- int temp = num[j];
|
|
|
- num[j] = num[j + 1];
|
|
|
- num[j + 1] = temp;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void result(int Size/*I*/, int num[]/*I*/)
|
|
|
-{
|
|
|
- cout << "排序后的数组:" << endl;
|
|
|
- for (int i = 0; i < Size; i++)
|
|
|
- {
|
|
|
- cout << num[i] << " ";
|
|
|
- }
|
|
|
- cout << " " << endl;
|
|
|
-}
|
|
|
-
|
|
|
-int main()
|
|
|
-{
|
|
|
- // 设置随机数种子
|
|
|
- srand(time(NULL)); //防止每次生成随机数一样
|
|
|
-
|
|
|
- const int Size = 10;
|
|
|
- int num[Size];
|
|
|
-
|
|
|
- // 1.鐢熸垚闅忔満鏁板苟鏌ョ湅
|
|
|
- generate_rand(Size, num);
|
|
|
-
|
|
|
- // 2.浣跨敤鍐掓场鎺掑簭瀵归殢鏈烘暟杩涜�鎺掑簭
|
|
|
- sort_num(Size, num);
|
|
|
-
|
|
|
- // 3.杈撳嚭鎺掑簭缁撴灉
|
|
|
- result(Size, num);
|
|
|
-
|
|
|
- system("pause");
|
|
|
- return 0;
|
|
|
+/*HEAD homework 1 HOMEWORK */
|
|
|
+/*==============================================================================
|
|
|
+2024年度随机数排序程序设计作业
|
|
|
+================================================================================
|
|
|
+File description:
|
|
|
+这是第一次作业演示的样板程序。
|
|
|
+提交者:徐子昂
|
|
|
+邮 箱: dutxza@163.com
|
|
|
+
|
|
|
+================================================================================
|
|
|
+Date Name Description of Change
|
|
|
+05/20/2024 Xu Ziang Created.
|
|
|
+$HISTORY$
|
|
|
+================================================================================
|
|
|
+*/
|
|
|
+
|
|
|
+#include <iostream>
|
|
|
+#include <ctime> // 随机数种子头文件
|
|
|
+using namespace std;
|
|
|
+
|
|
|
+void generate_rand(int Size/*I*/, int num[]/*I*/)
|
|
|
+{
|
|
|
+ for (int i = 0; i < Size; i++)
|
|
|
+ {
|
|
|
+ num[i] = rand() % 100 + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ cout << "生成的随机数组:" << endl;
|
|
|
+ for (int i = 0; i < Size; i++)
|
|
|
+ {
|
|
|
+ cout << num[i] << " ";
|
|
|
+ }
|
|
|
+ cout << endl;
|
|
|
+}
|
|
|
+
|
|
|
+void sort_num(int Size/*I*/, int num[]/*I*/)
|
|
|
+{
|
|
|
+
|
|
|
+ for (int i = 0; i < Size - 1; i++) // 对比总轮数
|
|
|
+ {
|
|
|
+ for (int j = 0; j < Size - i - 1; j++) // 每轮对比次数
|
|
|
+ {
|
|
|
+ if (num[j] > num[j + 1])
|
|
|
+ {
|
|
|
+ // 交换元素
|
|
|
+ int temp = num[j];
|
|
|
+ num[j] = num[j + 1];
|
|
|
+ num[j + 1] = temp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void result(int Size/*I*/, int num[]/*I*/)
|
|
|
+{
|
|
|
+ cout << "排序后的数组:" << endl;
|
|
|
+ for (int i = 0; i < Size; i++)
|
|
|
+ {
|
|
|
+ cout << num[i] << " ";
|
|
|
+ }
|
|
|
+ cout << " " << endl;
|
|
|
+}
|
|
|
+
|
|
|
+int main()
|
|
|
+{
|
|
|
+ // 设置随机数种子
|
|
|
+ srand(time(NULL)); //防止每次生成随机数一样
|
|
|
+
|
|
|
+ const int Size = 10;
|
|
|
+ int num[Size];
|
|
|
+
|
|
|
+ // 1.生成随机数并查看
|
|
|
+ generate_rand(Size, num);
|
|
|
+
|
|
|
+ // 2.使用冒泡排序对随机数进行排序
|
|
|
+ sort_num(Size, num);
|
|
|
+
|
|
|
+ // 3.输出排序结果
|
|
|
+ result(Size, num);
|
|
|
+
|
|
|
+ system("pause");
|
|
|
+ return 0;
|
|
|
}
|