2014-03-20 02:29

题目:将质因数只有3, 5, 7的正整数从小到大排列,找出其中第K个。

解法:用三个iterator指向3, 5, 7,每次将对应位置的数分别乘以3, 5, 7,取三者中最小的数作为生成的下一个结果。可以一次性生成整个序列,因为这个序列一般不会很长,增长率是指数级的。

代码:

 // 7.7 Find the kth number that has no prime factors other than 3, 5 or 7.
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; int main()
{
vector<int> v;
int n = ;
int p3, p5, p7;
int res3, res5, res7;
int min_res;
const int MAX = ; v.push_back();
p3 = p5 = p7 = ;
while (true) {
res3 = v[p3] * ;
res5 = v[p5] * ;
res7 = v[p7] * ;
min_res = min(res3, min(res5, res7));
if (min_res > MAX) {
break;
}
if (res3 == min_res) {
++p3;
}
if (res5 == min_res) {
++p5;
}
if (res7 == min_res) {
++p7;
}
v.push_back(min_res);
}
printf("count = %u\n", v.size()); while (scanf("%d", &n) == ) {
if (n < || n >= (int)v.size()) {
printf("Out of range.\n");
} else {
printf("%d\n", v[n]);
}
} return ;
}

《Cracking the Coding Interview》——第7章:数学和概率论——题目7的更多相关文章

  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章:数学和概率论——题目6

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

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

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

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

    2014-03-20 02:16 题目:只用加法和赋值,实现减法.乘法.除法. 解法:我只实现了整数范围内的.减法就是加上相反数.乘法就是连着加上很多个.除法就是减到不能减为止,数数总共减了多少个. ...

随机推荐

  1. Docker build 安装报错, Could not open requirments file: [Errno 2] No such file or directory:'requirements.txt'

    docker安装教程 https://docs.docker.com/get-started/part2/#build-the-app 相关帖子 https://stackoverflow.com/q ...

  2. LA 3635 派

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  3. Codeforces Round #347 (Div.2)_A. Complicated GCD

    题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...

  4. 2.NBU管理NetBackup

    2管理NetBackup2.1NetBackup作业任务监视 NetBackup任务监视器可以监视备份.恢复和归档任务的状态,也可以监视NetBackup本身数据库的备份. 2.1.1Activity ...

  5. aop 和castle 的一些 学习文章

    https://www.cnblogs.com/zhaogaojian/p/8360363.html

  6. C#中类的成员

    一.C#中类的成员 1. 类的成员 类中的数据和函数都称为类的成员.类的成员可以分为两类: ?类本身所声明的. ?从基类中继承来的. 如果在类声明中没有指定基类,则该类将继承System.Object ...

  7. node.js启动调试方式

    node.js启动调试方式(nodeJs不能像js一样在控制台调试) 以express项目为例,启动路径是localhost:3000 一.通过node命令启动 node server/bin/www ...

  8. Ubuntu 16.04安装docker(2018年最新)

    参考https://blog.csdn.net/bingzhongdehuoyan/article/details/79411479 http://www.cnblogs.com/lighten/p/ ...

  9. hdu_3501_Calculation 2

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  10. Hibernate进阶学习3

    Hibernate进阶学习3 测试hibernate的多表关联操作(一对多,多对一,多对多) 表之间的关系主要在类与元数据配置文件中体现 package com.hibernate.domain; i ...