|
发表于 2021-4-27 20:01:25
|
显示全部楼层
- #include<iostream>
- using std::cout;
- using std::cin;
- using std::endl;
- int main()
- {
- int a[10];
- cout<<"input 10 numbers :\n";
- int n = 0, i = 0;
- //输入完后以Ctrl+Z结尾,跳出循环
- while (cin >> n && i < 10) {
- a[i] = n;
- ++i;
- }
- //使用while(cin>>n)后重置cin,后面可继续用
- cin.clear();
- cin.ignore();
- cout << endl;
- int flag = 1;//flag为1时有交换产生,为0时则表明排序完毕
- while (flag == 1) {
- flag = 0;
- for (i = 0; i < 9; ++i) {
- //a[i]<a[i+1]时,交换位置
- if (a[i] < a[i + 1]) {
- int temp = a[i];
- a[i] = a[i + 1];
- a[i + 1] = temp;
- flag = 1;
- }
- }
- }
- cout<<"the sorted numbers :\n";
- for (i = 0; i < 10; i++)
- {
- cout << a[i]<<' ';
- }
- cout << endl;
- return 0;
- }
复制代码
|
|