17.1 Write a function to swap a number in place (that is, without temporary variables).

这道题让我们交换两个数,但是不能用额外空间,那么我们可以先做差值,存入a中,然后再加上b,存入b中,那么此时的b即为原来的a,因为整个相当于做了一个a - b + b的过程,那么现在b是原来的a,而a中现在保存的是差值,,那么原来的b值就可以通过b-a来得到,保存到a中即可:

解法一:

void swap(int a, int b) {
a = a - b;
b = b + a;
a = b - a;
cout << a << " " << b << endl;
}

下面这种做法跟上面的方法思路相同,但是用到了异或操作符,a异或b求的实际上就是a与b的差值,然后结果再异或b得到a,这就相当于a^b^b,即a^(b^b),由于b^b为0,任何数异或0都为其本身,所以b就成了a,然后a再异或差值就得到了b,完成了交换:

解法二:

void swap(int a, int b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout << a << " " << b << endl;
}

CareerCup All in One 题目汇总

[CareerCup] 17.1 Swap Number In Place 互换位置的更多相关文章

  1. java android 将 List中元素互换位置

    很多时候我要对List中的元素调换位置,这时候可以用如下代码,意思是将data中的index1与index2元素互换位置 //data 为List Collections.swap(data,inde ...

  2. 实现数组元素互换位置(乘机理解java参数传递)

    Java中函数参数是按值传递的,在实现数组元素互换位置之前,我想先说一下Java函数参数传递过程.一般情况下我们会把参数分为基本数据类型和引用数据类型,然后分别来讲参数传递,因为他们的外在表现似乎是不 ...

  3. [CareerCup] 18.4 Count Number of Two 统计数字2的个数

    18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如 ...

  4. [CareerCup] 17.14 Unconcatenate Words 断词

    17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace m ...

  5. [CareerCup] 17.11 Rand7 and Rand5 随机生成数字

    17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...

  6. [CareerCup] 17.5 Game of Master Mind 猜字游戏

    17.5 The Came of Master Mind is played as follows: The computer has four slots, and each slot will c ...

  7. CareerCup: 17.14 minimize unrecognized characters

    Oh, no! You have just completed a lengthy document when you have an unfortu- nate Find/Replace misha ...

  8. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  9. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

随机推荐

  1. Zigzag convert

    public static String Convert(String s,int row) { char[] c=s.toCharArray(); int len=s.length(); Strin ...

  2. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  3. 在CSDN中添加友情连接

    <a bref='http://www......'>友情连接</a><br/> <a bref='http://www......'>友情连接2< ...

  4. DSP using MATLAB 示例Example2.3

    n = [-10:1:10]; alpha = -0.1+0.3j; % x = exp(alpha*n); % subplot(2,1,1); set(gcf,'Color',[1,1,1]) % ...

  5. Matlab 查看变量信息who whos命令

  6. 农资产品送货车上使用 PDA手持机 现场销售开单 然后开单后能直接通过移动网络传回电脑(云服务器)

    客户挑战与需求 目前很多中小型企业或零售商店都会使用各种进销存管理软件以提高管理效率,但是由于这类软件都是安装在PC端,更多的是帮助客户通过进销存的管理分析企业营运账款,并不能实时地反映当前销售.库存 ...

  7. WPF点补间、拟合回归直线

    1,path画刷,绘制正弦 点,线: 生成正弦点 profilePoint.Value = * ( - Math.Sin(i * Math.PI / )); profilePoint.Type = ; ...

  8. css3 -- 2D变换

    1.transform 1 E{ 2 -moz-transform:function(value): 3 -ms-transform:function(value): 4 -o-transform:f ...

  9. jQuery-插件,优化

    jQuery应用: 1.表单验证: A:jQuery Validation插件:有时需要将验证的属性写在class中,有时需要将验证信息写在属性中,例如: <input id="cem ...

  10. Codeforce 493c

    H - Vasya and Basketball Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...