给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数

news/2024/7/8 3:38:58

给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数

分类: c/c++ 数据结构+算法   594人阅读  评论(0)  收藏  举报
[cpp]  view plain copy print ?
  1. /* 
  2. 给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。 
  3.     要求:空间复杂度O(1),时间复杂度为O(n)。 
  4. //*/  
  5.   
  6. #include <iostream>  
  7. #include <iomanip>  
  8. #include <limits>  
  9.   
  10. using namespace std;  
  11.   
  12. void swap_int(int& a, int& b)  
  13. {  
  14.     int t = a;  
  15.     a = b;  
  16.     b = t;  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     int numel[] = {1, 23, 2, 34, 21, 45, 26, 22, 41, 66, 74, 91, 17, 64};  
  22.     int sz = sizeof(numel)/sizeof(numel[0]);  
  23.   
  24.     for(int i =0; i<sz; ++i){  
  25.         cout << numel[i] << " ";  
  26.     }  
  27.     cout << endl;  
  28.   
  29.     int begin = 0;  
  30.     int end = sz -1;  
  31.     while(begin < end){  
  32.         while(numel[begin]%2 == 1 && end > begin){  
  33.             ++begin;  
  34.         }  
  35.         while(numel[end]%2 == 0 && end > begin){  
  36.             --end;  
  37.         }  
  38.         swap_int(numel[begin], numel[end]);  
  39.     }  
  40.   
  41.     for(int i =0; i<sz; ++i){  
  42.         cout << numel[i] << " ";  
  43.     }  
  44.     cout << endl;  
  45.   
  46.     return 0;  
  47. }  

http://www.niftyadmin.cn/n/2525424.html

相关文章

开源大数据周刊-第8期

阿里云E-Mapreduce动态 E-Mapreduce团队 1.3.2版本&#xff08;已经发布&#xff09;&#xff1a; Master HA功能1.4版本&#xff08;正在研发&#xff09;: 用户执行计划及集群运行状态自定义报警集群整体运行情况的仪表盘集群的一些专家建议&#xff0c;例如&#xff1a;扩容…

Unity - RenderWithShader, SetReplacementShader, ResetReplacementShader 测试

文章目录API 简介DemoShaderCSharp ScriptsScene - 场景内容运行效果详细解析SetReplacementShader与ResetReplacementShaderRenderWithShader坑点ProjectUnity - RenderWithShader, SetReplacementShader, ResetReplacementShader 测试 最不喜欢介绍API&#xff0c;但可以参考…

强制类型转换运算符

强制类型转换运算符 C有四种强制类型转换符&#xff0c;分别是dynamic_cast&#xff0c;const_cast&#xff0c;static_cast&#xff0c;reinterpret_cast。其中dynamic_cast与运行时类型转换密切相关&#xff0c;在这里我们介绍dynamic_cast。dynamic_cast强制转换运算符 该转…

Ex2010-17 Linked Mailbox in Exchange Server

Series 1http://www.msexchange.org/articles-tutorials/exchange-server-2007/planning-architecture/deploying-exchange-resource-forest-part1.htmlSeries 2http://msexchangeteam.in/linked-mailbox-in-exchange-server-2013-part-1-2/转载于:https://blog.51cto.com/1050…

Unity Shader PostProcessing - 3 - 深度+法线来边缘检测

最近事情太多&#xff0c;学习时间断断续续&#xff0c;终于挤出时间&#xff0c;将深度法线边缘检测的基础学习完 前一篇&#xff1a;Unity Shader PostProcessing - 2 - 边缘检测&#xff0c;是基于图像像素来边缘检测的。 在平面像素边缘检测会遇到很多像素颜色问题导致检…

boost bind 用法

boost::bind()用来将一个函数或函数对象绑定某些参数&#xff0c;返回值是一个函数对象。 它提供一个任意的函数对象(仿函数)、函数、函数指针、成员函数指针。 上代码&#xff1a; #include <boost/bind.hpp> #include <iostream> #include <vector> #…

判断访问端是手机还是电脑

2019独角兽企业重金招聘Python工程师标准>>> <SCRIPT LANGUAGE"JavaScript"> //客户端判断 mobile_device_detect(); function mobile_device_detect() { var appurl "http://www.baidu.com/"; /* 移动…

boost asio Example

下面的代码来自官网 http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp03/echo/async_tcp_echo_server.cpp #include <cstdlib> #include <iostream> #include <boost/bind.hpp> #include <boost/asio.hpp>using boost::asio::…