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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第7章:数学和概率论——题目7

    2014-03-20 02:29 题目:将质因数只有3, 5, 7的正整数从小到大排列,找出其中第K个. 解法:用三个iterator指向3, 5, 7,每次将对应位置的数分别乘以3, 5, 7,取三 ...

  8. 《Cracking the Coding Interview》——第7章:数学和概率论——题目6

    2014-03-20 02:24 题目:给定二位平面上一堆点,找到一条直线,使其穿过的点数量最多. 解法:我的解法只能适用整点,对于实数坐标就得换效率更低的办法了.请参见LeetCode - Max ...

  9. 《Cracking the Coding Interview》——第7章:数学和概率论——题目5

    2014-03-20 02:20 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...

随机推荐

  1. 笨办法学Python(三十四)

    习题 34: 访问列表的元素 列表的用处很大,但只有你能访问里边的内容时它才能发挥出作用来.你已经学会了按顺序读出列表的内容,但如果你要得到第 5 个元素该怎么办呢?你需要知道如何访问列表中的元素.访 ...

  2. CVE-2017-8464 LNK文件(快捷方式)远程代码执行漏洞复现

    北京时间2017年6月13日凌晨,微软官方发布6月安全补丁程序,“震网三代” LNK文件远程代码执行漏洞(CVE-2017-8464)和Windows搜索远程命令执行漏洞(CVE-2017-8543) ...

  3. Ruby SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B:

    最近使用ruby-china的源连接不上 使用gem update遇到这个问题, 原来是ruby没有包含SSL证书,所以Https的链接被服务器拒绝. 解决方法很简单,首先在这里下载证书(http:/ ...

  4. IOS 拦截所有push进来的子控制器

    /** * 能拦截所有push进来的子控制器 */ - (void)pushViewController:(UIViewController *)viewController animated:(BO ...

  5. lambda Expression的使用方法

    Expression<Func<your class, bool>> whereExp = f => true;//类似1=1,初始化条件 if (!string.IsN ...

  6. 数据库连接-ADO.NET

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huo065000/article/details/25830291       非常早就知道了ADO ...

  7. 2018年Linux运维人员必会开源运维工具体系总结

    操作系统:Centos,Ubuntu,Redhat,suse,Freebsd 网站服务:nginx,apache,lighttpd,php,tomcat,resin数据库:MySQL,MariaDB, ...

  8. 2、SpringBoot+MybatisPlus整合-------BaseCRUD

    开发工具:STS 代码下载链接:GitHub管理代码 版本: Springboot:1.5.14.RELEASE 使用2.0以上的Springboot,会报出一些异常.欢迎知道异常原因的大牛解惑. M ...

  9. typeid操作符

    typeid() operator返回type_info,返回值不可拷贝.不可赋值 // Illustrates the typeid operator. #include <iostream& ...

  10. Hibernate进阶学习4

    Hibernate进阶学习4 深入学习hibernate的查询语句 测试HQL查询 package com.hibernate.test; import com.hibernate.domain.Cu ...