《Cracking the Coding Interview》——第7章:数学和概率论——题目4
2014-03-20 02:16
题目:只用加法和赋值,实现减法、乘法、除法。
解法:我只实现了整数范围内的。减法就是加上相反数。乘法就是连着加上很多个。除法就是减到不能减为止,数数总共减了多少个。
代码:
// 7.4 Implement the -*/ function with only the + operator.
// You cannot use bit operation, although you might want it for efficiency.
#include <cstdio>
using namespace std; int negate(int n)
{
int one = (n >= ? - : );
int res = ; while (n != ) {
n += one;
res += one;
} return res;
} int add(int x, int y)
{
return x + y;
} int subtract(int x, int y)
{
return x + negate(y);
} int multiply(int x, int y)
{
int res = ;
int value = (x >= ? y : negate(y));
int one = (x >= ? - : ); while (x != ) {
res += value;
x += one;
} return res;
} int divide(int x, int y)
{
if (y == ) {
return ;
}
if (x == ) {
return ;
} int res = ;
int one = ;
int negone = -;
int negy = negate(y); if (x > ) {
if (y > ) {
while (x >= y) {
x += negy;
res += one;
}
} else {
while (x >= negy) {
x += y;
res += negone;
}
}
} else {
if (y > ) {
while (x <= negy) {
x += y;
res += negone;
}
} else {
while (x <= y) {
x += negy;
res += one;
}
}
} return res;
} int main()
{
char s[];
int a, b;
int res; while (scanf("%d%s%d", &a, s, &b) == ) {
switch (s[]) {
case '+':
res = add(a, b);
break;
case '-':
res = subtract(a, b);
break;
case '*':
res = multiply(a, b);
break;
case '/':
res = divide(a, b);
break;
}
printf("%d\n", res);
} return ;
}
《Cracking the Coding Interview》——第7章:数学和概率论——题目4的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目7
2014-03-20 02:29 题目:将质因数只有3, 5, 7的正整数从小到大排列,找出其中第K个. 解法:用三个iterator指向3, 5, 7,每次将对应位置的数分别乘以3, 5, 7,取三 ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目6
2014-03-20 02:24 题目:给定二位平面上一堆点,找到一条直线,使其穿过的点数量最多. 解法:我的解法只能适用整点,对于实数坐标就得换效率更低的办法了.请参见LeetCode - Max ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目5
2014-03-20 02:20 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...
随机推荐
- SPOJ - LIS2 Another Longest Increasing Subsequence Problem
cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...
- Hash模板
;//一般为靠近总数的素数 struct Hashtable { int x;//hash存的值 Hashtable * next; Hashtable() { next = ; } }; Hasht ...
- uva题库爬取
每次进uva都慢的要死,而且一步一步找到自己的那个题目简直要命. 于是,我想到做一个爬取uva题库,记录一下其中遇到的问题. 1.uva题目的链接是一个外部的,想要获取https资源,会报出SNIMi ...
- Java 序列化对象工具类
SerializationUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.Byt ...
- maven没有servlet(创建servlet后报错)
maven不能创建servlet 解决方案 方案一 在项目的iml进行指定根目录 <sourceRoots> <root url="file://$MODULE_DIR$/ ...
- 第33章 TIM—电容按键检测—零死角玩转STM32-F429系列
第33章 TIM—电容按键检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
- chisel(安装)
github地址 先安装homeBrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...
- JS底层挖掘
//Promise版本的Ajaxconst getJSON = function(url) { const promise =new Promise(function(resolve, reject) ...
- harbor 配置 Keepalived 实现HA
环境说明: $ cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) vip : 192 168 93 142 MASTER-har ...
- mysql数据库设置外键,更新与删除选项
CASCADE:父表delete.update的时候,子表会delete.update掉关联记录:SET NULL:父表delete.update的时候,子表会将关联记录的外键字段所在列设为null, ...