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中即可:

解法一:

  1. void swap(int a, int b) {
  2. a = a - b;
  3. b = b + a;
  4. a = b - a;
  5. cout << a << " " << b << endl;
  6. }

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

解法二:

  1. void swap(int a, int b) {
  2. a = a ^ b;
  3. b = a ^ b;
  4. a = a ^ b;
  5. cout << a << " " << b << endl;
  6. }

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. eclipse中快捷键

    转为大写  ctrl+shift+x 转为小写  ctrl+shift+y 根据类名快速找到类文件 ctrl+shift+r 返回上一级 ALT  +  <- 速定位到某一行 ctrl+L

  2. jbox使用总结

    jbox是一个不错的插件 当使用get打开新页面的时候,可以使用h.对像ID来获得对像ID的值 Js代码 js代码: /** * @description: test * @author: BrinP ...

  3. 使用MulticastSocket实现多点广播

    原文链接:http://hbiao68.iteye.com/blog/1943354 使用MulticastSocket实现多点广播 DatagramSocket只允许数据报发送给指定的目标地址,而M ...

  4. 【项目总结】之——JS分割字符串

    背景: 在我们做那个招标项目的时候,由于是刚刚接触到这个BS东西,我基本上是什么也不会.可是当时组长浩哥给过我一个任务,就是叫我将数据里面以字符串形式存在的信息切割开,然后显示到前台上去.当时对于浩哥 ...

  5. 把textarea右下角的灰点去掉

    这个是浏览器自带功能,那个区域属于滚动条的占位区 使textarea可以调整size,加 style="resize:none"可解决,添加多行文字时还是会出现滚动条的.

  6. 利用scp传输文件小结

    从本地复制到远程 scp mysql-5.5.29-linux2.6-x86_64.tar.gz 192.168.1.11:/opt 指定端口: scp -P 60022 /opt/ray/nginx ...

  7. Angular.js 以及个人学习网站

    Angular.js  教程 http://www.360doc.com/content/14/0414/15/14416931_368816305.shtml web前端学习: 慕课网:http:/ ...

  8. HDU 5769 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 [2016多校contest-4] 题意:给定一个字符,还有一个字符串,问这个字符串存在多少个不 ...

  9. Codeforces Round #354 (Div. 2)-C

    C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...

  10. node.js整理 04网络操作

    简介 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content- ...