总结的部分题目思路与代码,待完善。
【剑指offer-第二版】部分题目与解答【C++版本】
题目:
我们把只包含因子2,3,5的数称作丑数。求按从小到大的顺序的第1500个丑数。例如,6,8都是丑数,但14不是丑数,因为它包含因子7。习惯上我们把1当作第一个丑数。
解题思路:
1.首先要理解丑数的概念。换个角度理解,因为丑数只能被2,3,5整除。也就是说,如果一个数能被2整除,就继续除以2,能被3整除,就继续除以3,能被5整除,就继续除以5。这样到最后得到的结果是1,那么这个数就是一个丑数。
2.根据丑数的定义,一个丑数应该是另一个丑数乘上2,3,5的结果(1除外)。
3.采用空间换时间的方法。建立一个数组来按从小到大的顺利记录已经找到的丑数。生成数组的下一个丑数,那么该丑数必定是数组中的某一个丑数乘2,3或者5生成的。记录一个t2,t2是数组中已有的一个数,排在它前面的每一个丑数乘以2得到的结果都会小于等于已知的最大的丑数,t2是第一个乘2大于数组中最大的丑数的那个数。同样记录一个t3和t5。
4.那么要添加到数组中的下一个丑数就是t2×2,t3×3,t5×5中最小的那一个。
5.按照这个思路向数组中添加丑数,直到找到需要的那个丑数为止。
可以AC的代码【C++版本】
//与原书略不同的实现方法
//原书使用指针来爬,效率应该是更好的
#include <vector>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int getUglyNum(int index)
{
//错误输入的情况
if(index <= 0)return 0;
//特殊输入的情况
if(index == 1)return 1;
//声明丑数数组,初始化第一个元素为1
vector<int> uglyNum{1};
//初始化t2,t3,t5为1
int t2 = 1,t3 = 1,t5 = 1;
//
while(uglyNum.size() != index){
int u2 = t2*2;
int u3 = t3*3;
int u5 = t5*5;
int next = min(u2,min(u3,u5));
uglyNum.push_back(next);
//这里使用upper_bound来在丑数数组中查找下一个t2,t3,t5的为止
//upper_bound返回最后一个可以插入next/i的位置,即返回的是乘i大于已知最大丑数的第一个数的位置
vector<int>::iterator iter2 = upper_bound(uglyNum.begin(),uglyNum.end(),next/2);
vector<int>::iterator iter3 = upper_bound(uglyNum.begin(),uglyNum.end(),next/3);
vector<int>::iterator iter5 = upper_bound(uglyNum.begin(),uglyNum.end(),next/5);
//如果位置为1的位置,说明ti应该还是1
if(iter2 == uglyNum.begin())
t2 = 1;
else
t2 = *(iter2);
if(iter3 == uglyNum.begin())
t3 = 1;
else
t3 = *(iter3);
if(iter5 == uglyNum.begin())
t5 = 1;
else
t5 = *(iter5);
}
return uglyNum[index-1];
}
int main()
{
cout << "Please enter a integer:" << endl;
int test;
while(cin >> test){
cout << getUglyNum(test) << endl;
}
return 0;
}