元素按奇偶排序
来源:互联网 发布:mac 10.13 卡 编辑:程序博客网 时间:2024/05/21 06:29
将数组中偶数从大到小排序,奇数从小到大排序,奇数和偶数交叉着放且输出数组第一位放奇数 若奇数和偶数不等长,则把剩下的直接放到数组中。
输入数组a, 将偶数放入b数组,奇数放入c数组,分别对b,c排序,然后输出结果到result中,用flag标记。
#include<iostream> #include<string> using namespace std; void main(){int a[100];cout << "请输入数组:" << endl;cin >> a[0];int la = 1;while (getchar() != '\n'){cin >> a[la++];}int *b = new int[la];int *c = new int[la];int lb = 0, lc = 0, temp;for (int i = 0; i < la; i++){if (a[i] % 2 == 0)b[lb++] = a[i];elsec[lc++] = a[i];}for (int i = 0; i < lb - 1; i++){for (int r = 0; r < lb - i - 1; r++){if (b[r] < b[r + 1]){temp = b[r];b[r] = b[r + 1];b[r + 1] = temp;}}}for (int i = 0; i < lc - 1; i++){for (int r = 0; r < lc - i - 1; r++){if (c[r]>c[r + 1]){temp = c[r];c[r] = c[r + 1];c[r + 1] = temp;}}}int *result = new int [la];int flag = 0,k=0;int i = 0, j = 0;while (i < lc&&j < lb){if(!flag){result[k++] = c[i++];flag = 1;}else{result[k++] = b[j++];flag = 0;}}while (i < lc){result[k++] = c[i++];}while (j < lb){result[k++] = b[j++];}for (int i = 0; i < k; i++)cout << result[i] << " ";cout << endl;system("pause");}
0 0
- 元素按奇偶排序
- 华为机试:元素按奇偶排序
- 华为机试-元素按奇偶排序
- 华为机试—元素按奇偶排序 (金字塔)
- 华为机试—元素按奇偶排序
- 华为机试—元素按奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 奇偶排序
- 使用SharedPerference保存数据
- linux命令行播放声音
- Android多线程中UI线程和其他线程进行交互的几种方法小结
- 数据源架构模式之表入口模式
- public static void main(String arg[]) 详细解释
- 元素按奇偶排序
- 数塔
- 命令行选项 CCF
- iOS静态库与Workspace在项目中的使用
- STM32波特率设置
- TopCoder SRM 664 Div2 Level Three
- 程序员技术练级攻略
- Easter Holidays(二次spfa求最短路、最长路+打印路径)
- 重载重写_覆盖重定义隐藏.cpp