c语言程序,要求输入一个n数输出第n个丑数。丑数是素因子只有2.3.5.7。非常急,谢谢。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言程序,要求输入一个n数输出第n个丑数。丑数是素因子只有2.3.5.7。非常急,谢谢。相关的知识,希望对你有一定的参考价值。

含因子2,3,5的正整数被称作丑数   不含7   1不是丑数。

判断方法

首先除2,直到不能整除为止,然后除5到不能整除为止,然后除3直到不能整除为止。最终判断剩余的数字是否为1,如果是1则为丑数,否则不是丑数。

你的题目是不是输入n个整数,输出所有丑数。

#include <stdio.h>
int iscn(int num);
#define maxSize 10//输入的最大数字个数 可修改
int main()

    int cnum[maxSize],num,i,j=0;
    printf("输入最多%d个正整数(不足%d个,可以输入负数结束输入):\\n",maxSize,maxSize);
    for(i=0;i<maxSize;i++)
    
        scanf("%d",&num);
        if(num<0)
            break;
        if(iscn(num))
            cnum[j++]=num;
    
    printf("其中所有丑数为:\\n");
    for(i=0;i<j;i++)
        printf("%d ",cnum[i]);
    return 0;

int iscn(int num)//检查整数是否为丑数 是返回1  不是返回0

    int chu;
    chu=num;
    if(num==1)
        return 0;
    while(1)
    
        if(chu%2==0)
            chu=chu/2;
        else
            break;
    
    while(1)
    
        if(chu%5==0)
            chu=chu/5;
        else
            break;
    
    while(1)
    
        if(chu%3==0)
            chu=chu/3;
        else
            break;
    

    if(chu==1)
        return 1;
    else
        return 0;

参考技术A #include <iostream>  
#include <climits>  
using namespace std;  
//遍历法找丑数   
int IsUgly(int num)//判断是否是  
  
    while (num %2 == 0)  
      
        num /= 2;  
      
    while (num %3 == 0)  
      
        num /= 3;  
      
    while (num %5 == 0)  
      
        num /= 5;  
      
    if (num == 1)  
        return 1;  
    else  
        return 0;//not an ugly number  
  
void  GetUglyNumber(int index)  
//找到第index个丑数  
    int i , time =0 ;  
    if (index < 1)  
      
        cout << "error input " << endl;  
        exit(EXIT_FAILURE);  
      
    for (i=1 ; i< INT_MAX && time < index ; i++)  
      
        if ( IsUgly(i) )  
          
            time ++ ;  
//  cout << i << " " ;  
          
      
    cout << i-1 << endl;  
  
int main()  
  
    int Number;  
    cout << "Input a number : " ;  
    cin >> Number ;  
    GetUglyNumber(Number);  
    return 0;  
  
Input a number : 7
8
Press any key to continue

参考技术B 根据题意首先要得到一组排好序的丑数,然后根据这个n去查,显然如果这个n大于已有集合的数量则自动产生后续的丑数。
有一个链接给出了一种方法:http://blog.csdn.net/coder_xia/article/details/6707600
不过可以采用另一种方法:
根据丑数的定义:“所谓丑数,就是不能被2,3,5以外的其他素数整除的数。1,2,3,4,5,6,8,9,10,12,15是最前面的11个丑数。”
我们知道第一个是1,其次是2,3,这是我们知道的最初三个丑数,而且我们知道:
3<2^2=4<5,4<5<2*3<2^3,等等,可以根据这些约束选择后续丑数应该是乘2,3或5,相应的顺序也可以知道,从而进行一组一组的增加,然后根据n判断它应该在第几个组的第几个位置。
参考技术C 以哪个数为基准的第n个呢

~
~
~
参考技术D 。。。。。。。。

2 3 5 吧 有7吗

算法63----丑数动态规划

一、题目:

编写一个程序,找出第 n 个丑数。

丑数就是只包含质因数 2, 3, 5正整数

示例:

输入: n = 10
输出: 12
解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。

说明:  

  1. 1 是丑数。
  2. n 不超过1690。

思路:动态规划

t1,t2,t3三个变量记录乘以2,3,5的个数。如:

技术分享图片

 

代码:

    def nthUglyNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        ‘‘‘
        #超出时间限制
        
        if n == 0:
            return 0
        i = 1
        while i < n+1:
            m = i
            while m != 1:
                if not m % 2:
                    m = m // 2
                elif not m % 3:
                    m = m //3
                elif not m % 5:
                    m = m // 5
                else:
                    n += 1
                    break
            i +=1
        return n
        ‘‘‘
        #动态规划
        if n<=0:
            return 0
        t1,t2,t3 = 0 , 0 , 0
        res = [1]
        while len(res) < n:
            res.append(min(res[t1]*2,res[t2]*3,res[t3]*5))
            if res[-1] == res[t1] * 2:
                t1 += 1
            if res[-1] == res[t2] * 3:
                t2 += 1
            if res[-1] == res[t3] * 5:
                t3 += 1
        return res[-1]

 

以上是关于c语言程序,要求输入一个n数输出第n个丑数。丑数是素因子只有2.3.5.7。非常急,谢谢。的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 264.丑数II

算法63----丑数动态规划

九度-题目1214:丑数

264. 丑数 II-动态规划-(263. 丑数)

LeetCode——264. 丑数 II

leetcode 丑数 II java