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 ;
}

最新文章

  1. HTTP常见头域
  2. C#面向接口编程详解(1)——思想基础
  3. codeforces 496B. Secret Combination 解题报告
  4. 【读书笔记《Android游戏编程之从零开始》】20.游戏开发基础(游戏数据存储)
  5. PHP+socket+SMTP、POP3协议发送、接收邮件
  6. Oracle 如何对时间进行简单加减运算
  7. 修改ECSHOP后台的商品列表里显示该商品品牌
  8. Linux概述
  9. JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类
  10. Android实战开发租赁管理软件(适配UI,数据的存储,多线程下载)课程分享
  11. 轻量级原生 ajax 函数,支持 get/array post/array post/json
  12. 解决无法打开myeclipse--&gt;“The default workspace&#39;D: /myeclipse spaceis in use or cannot be created. Please choose a different one”
  13. BZOJ2199: [Usaco2011 Jan]奶牛议会(2-SAT)
  14. C# 多线程下 静态类字段异常
  15. MyBatis基础入门《十六》缓存
  16. git第二节----git clone与git tag
  17. Oracle常用方法备份
  18. springMVC非注解常用的&quot;处理器映射器&quot;、&quot;适配器&quot;、&quot;处理器&quot;
  19. ios开发之--UIButton中imageView和titleLabel的位置调整
  20. Android Sqlite 简单SQL语句

热门文章

  1. python IDE--pycharm安装及使用
  2. react及flux架构范例Todomvc分析
  3. 幻灯片的JQuqey的制作效果,只要几行代码
  4. c#按钮如何避免重复点击后报错
  5. js另类值交换
  6. shell编程中的vim命令说明
  7. Map详解
  8. Spring Boot Shiro权限管理--自定义 FormAuthenticationFilter验证码整合
  9. [异常笔记] spring boot 启动-2018040201
  10. Redis 数据类型List链表