《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 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...
随机推荐
- 二维码生成的WEB api方法
/// <summary> /// 获取二维码 /// </summary> /// <param name="size">编码测量度,值越大生 ...
- ORA-01262,oracle启动报错,及Oracle启动原理
错误状态: SQL> startup ORA-01261: Parameter db_recovery_file_dest destination string cannot be transl ...
- 1.3配置存储单元(nbu重删池)
1.3配置存储单元 在备份或归档操作时,NetBackup将数据通过存储单元写到物理介质上,NetBackup支持三种类型的存储单元.即: 介质管理器.磁盘和NDMP. 1.3.1介质管理存储单元配置 ...
- grep的使用
http://www.eguidedog.net/linux-tutorial/05-grep.php grep apple fruitlist.txt:在fruitlist.txt中查找apple字 ...
- glocktop
glocktop: Display or print active GFS2 locks. Index of glocktop man page Read glocktop man page on L ...
- N76E003---输入捕获
输入捕获 根据芯片手册,定时器2可以作为输入捕获使用,设置非常简单,官方也提供了宏给我们使用 void Time2_cap_init(void) { /******* 输入捕获CF设置 ******* ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
- git常用命令(三)
====================================================================== 本地仓库操作 ====================== ...
- 第50章 读写内部FLASH—零死角玩转STM32-F429系列
第50章 读写内部FLASH 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...
- window下绝对路径
项目中配置文件(properties或yml)和项目是分离的,常见的配置方法如下: <profiles> <profile> <id>mas</id> ...