33、求按从小到大的顺序的第N个丑数

一、题目

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

二、解法

 1 public class Solution {
 2    public int GetUglyNumber_Solution(int index) {
 3          if(index <= 0)
 4              return 0;
 5          if(index < 7)
 6              return index;
 7          int[] res = new int[index];
 8          res[0] = 1;
 9          int t2 = 0,//记录乘以2的个数
10              t3 = 0,//记录乘以3的个数
11              t5 = 0,//记录乘以5的个数
12              i  = 1;
13          for(; i < index; i++){
14              res[i] = Math.min(res[t2]*2, Math.min(res[t3]*3, res[t5]*5));
15              if(res[i] == res[t2]*2) t2++;//如果最小的等于res[t2]*2,那么t2加1
16              if(res[i] == res[t3]*3) t3++;
17              if(res[i] == res[t5]*5) t5++;
18          }
19          return res[index-1];
20         }
21 }
时间: 2024-07-30 13:46:49

33、求按从小到大的顺序的第N个丑数的相关文章

c语言:把只含因子2、3和5的数称为丑数,求按从小到大的顺序的第1500个丑数(两种方法比较)

把只含因子2.3和5的数称为丑数,求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因为它包含因子7.习惯上把1当作第1个丑数. 算法1:逐个判断每个整数是不是丑数的解法,直观但不够高效 #include<stdio.h> int ugly(int number) { while (number % 2 == 0) { number /= 2; } while (number % 3 == 0) { number /= 3; } while (number % 5 == 0

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

public class Solution { public int GetUglyNumber_Solution(int index) { if(index<=0){ return 0; } int[] p = new int[index]; p[0]=1; int p2=0; int p3=0; int p5=0; int num=1;//索引 while (num < index){ int min = Min(p[p2]*2,p[p3]*3,p[p5]*5); p[num] = min

33 丑数

题目描述 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 丑数只能被2,3,5整除,所以我们一直除以2,一直除以3,一直除以5 ,如果最后等于1就是丑数 解法1:暴力 超时了 最后. 1 public class Solution { 2 public int GetUglyNumber_Solution(int index) { 3 int ug_cnt=0; 4

[剑指offer] 33. 丑数

题目描述 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数.  leetcode原题,题解链接 class Solution { public: int GetUglyNumber_Solution(int index) { if (index < 7) return index; vector<int> res(index); res[0] = 1; in

【剑指Offer】33:丑数

题目描述 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 题解: 1 //所有的丑数分为三种类型 2*i,3*i,5*i ,其中 i是数组中的元素,一开始只有1 2 public static int GetUglyNumber_Solution(int index) { 3 // 0-6的丑数分别为0-6 4 if(index<7) { 5 return in

Java-第三章-从键盘输入3个整数,然后将输入的整数按照从小到大的顺序放在abc,并输出3个变量的值

import java.util.*; public class lianxi72_3 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); System.out.println("请输入a的值:"); int a=s.nextInt(); System.out.println("请输入b的值:")

c语言:输入4个整数,要求按从小到大的顺序输出。

输入4个整数,要求按从小到大的顺序输出. 解:程序: #include<stdio.h> int main() { int t,a,b,c,d; printf("请输入4个数:"); scanf("%d,%d,%d,%d",&a,&b,&c,&d); if (a > b) { t = a; a = b; b = t; } if (a > c) { t = a; a = c; c = t; } if (a >

1126: 零起点学算法33——求多项式

1126: 零起点学算法33--求多项式 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2614  Accepted: 1356[Submit][Status][Web Board] Description 形如1-2+3-4...+n,你会编写吗? Input 输入1个正整数n(多组数据) n<=1000 Output 输出1-2+3-4...+n的值(每组数据一行) Sample Input

输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。

ASCII码排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符. 输入 第一行输入一个数N,表示有N组测试数据.后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之间无空格. 输出 对于每组输入数据,输出一行,字符中间用一个空格分开. 样例输入 2 qwe asd 样例输出 e q w a d s #include <iostream> using namespace