#includeusing namespace std; int main() { int t; int i = 0, n = 4; int a[5]; for (i=0; i <= 4; i++) cin >> a[i]; while(n>=1) { for (i=0; i <= n - 1; i++) if (a[i] > a[i + 1]) t = a[i],a[i] = a[i + 1],a[i + 1] = t; n--; } for (i=0; i <= 4; i++) cout << a[i]< 为何以下swap函数不能交换两变量的数值???
#includeusing namespace std; void swap(int a, int b) { int t; t = a; a = b; b = t; } int main() { int t; int i = 0, n = 4; int a[5]; for (i=0; i <= 4; i++) cin >> a[i]; while(n>=1) { for (i = 0; i <= n - 1; i++) { if (a[i] < a[i + 1]) t=a[i],a[i]=a[i+1],a[i+1]=t; } n--; for (i = 0; i <= 4; i++) cout << a[i] << " "; cout << endl; } for (i=0; i <= 4; i++) cout << a[i]<<" "; int m, n1; cin >> m >> n1; swap(m, n1); cout << m <<" "<< n1; //m和n1的值并未得到交换 return 0; } 选择排序法,输入一个数组并从小到大输出。
#includeusing namespace std; int main() { int i,j; int a[5]; int t; for (i = 0; i<= 4; i++)cin >> a[i]; for (i = 0; i < 5; i++) { int k = i; for (j = i; j < 5; j++) if (a[j] < a[k])k = j; t = a[i], a[i] = a[k], a[k] = t; k++; for (j = 0; j < 5; j++) cout << a[j] << " "; cout << endl; } cout << "最终结果" << endl; for (j = 0; j < 5; j++) cout << a[j]<< " "; return 0; } 欢迎分享,转载请注明来源:内存溢出
冒泡排序法和选择排序法
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
【蓝桥杯C++练习】每日一练14-编程题
上一篇
2022-10-21
设计模式-结构型-适配器模式-c++
下一篇
2022-10-21
评论列表(0条)