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