- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- int n[10];
- for(int j=0; j<10; j++)
- {
- n[j]=rand()%21;
- for(int k=0; k<j; k++)
- {
- if(n[j]==n[k])
- {
- j--;
- break;
- }
- }
- }
- cout << "排序前: ";
- for (int g = 0; g < 10; g++) {
- cout << n[g] << " ";
- }
- cout << endl;
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 9 - i; j++) {
- if (n[j] > n[j + 1]) {
- int temp = n[j];
- n[j] = n[j + 1];
- n[j + 1] = temp;
- }
- }
- }
- cout << "排序後: ";
- for (int g = 0; g < 10; g++) {
- cout << n[g] << " ";
- }
- cout << endl;
- return 0;
- }
複製代碼 |