• 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。


    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include<iostream>
    #include<vector>
    using namespace std;
    
    
    class Solution {
    public:
    	int GetUglyNumber_Solution(int index) {
    		int Max = 0;//记录最大丑数
    		int num = 0;
    		vector<int> vec;
    		int i = 1;
    
    		if (index == 0)//如果index是0,返回0
    			return 0;
    
    		while (i <= index)
    		{
    			if (i == 1)
    				vec.push_back(i);
    			else
    			{
    				int num1 = searchNum(vec, 2);
    				int num2 = searchNum(vec, 3);
    				int num3 = searchNum(vec, 5);
    				int min = minNum(num1, num2, num3);
    				vec.push_back(min);
    			}
    			i++;
    		}
    		Max = *(vec.end() - 1);
    		return	Max;
    	}
    
    	int searchNum(vector<int> vec, int multiplier)//数组里面的数都乘以一个数之后,返回第一个大于vec数组里面的那个数
    	{
    		vector<int> copyVec;
    		int finanlNum = *(vec.end() - 1);
    		int flag = 0;
    		for (auto it = vec.begin(); it < vec.end(); it++)
    		{
    			copyVec.push_back((*it)*multiplier);
    		}
    		for (auto it = copyVec.begin(); it < copyVec.end(); it++)
    		{
    			if (*it > finanlNum)
    			{
    				flag = *it;
    				break;
    			}
    
    		}
    		return flag;
    	}
    
    	int minNum(int num1, int num2, int num3)//返回三个数字里面的最小值
    	{
    		int min = num1 < num2 ? num1 : num2;
    		min = min < num3 ? min : num3;
    	//	cout << "min:" << min << endl;
    		return min;
    	}
    };
    
    
    int main()
    {
    	
    	int num;
    	Solution so;
    //	num = so.GetUglyNumber_Solution(1);
    
    	while (cin >> num)
    	{
    		cout << "第" << num <<"个数是:"<< so.GetUglyNumber_Solution(num) <<endl;
    	}
    	vector<int> a;
    	
    	
    return 0;
    }
  • 相关阅读:
    HDU 5818 Joint Stacks
    HDU 5816 Hearthstone
    HDU 5812 Distance
    HDU 5807 Keep In Touch
    HDU 5798 Stabilization
    HDU 5543 Pick The Sticks
    Light OJ 1393 Crazy Calendar (尼姆博弈)
    NEFU 2016省赛演练一 I题 (模拟题)
    NEFU 2016省赛演练一 F题 (高精度加法)
    NEFU 2016省赛演练一 B题(递推)
  • 原文地址:https://www.cnblogs.com/wdan2016/p/5972614.html
Copyright © 2020-2023  润新知