- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int main()
- {
- start:
- srand(time(NULL));
- int a[4] = {};
- cout << "四個介於0~9的不重複隨機亂數為: ";
- for (int i = 0; i < 4; i++)
- {
- a[i] = rand() % 10;
- for (int j = 0; j < i; j++)
- if (a[j] == a[i])
- {
- i--;
- break;
- }
- }
-
- for (int i = 0; i < 4; i++)
- {
- cout << a[i] << "\t";
- }
- cout << endl;
- system("pause");
- goto start;
- return 0;
- }
複製代碼 |