zoukankan      html  css  js  c++  java
  • Hdoj 1058.Humble Numbers 题解

    Problem Description

    A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.
    Write a program to find and print the nth element in this sequence

    Input

    The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.

    Output

    For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.

    Sample Input

    1
    2
    3
    4
    11
    12
    13
    21
    22
    23
    100
    1000
    5842
    0
    

    Sample Output

    The 1st humble number is 1.
    The 2nd humble number is 2.
    The 3rd humble number is 3.
    The 4th humble number is 4.
    The 11th humble number is 12.
    The 12th humble number is 14.
    The 13th humble number is 15.
    The 21st humble number is 28.
    The 22nd humble number is 30.
    The 23rd humble number is 32.
    The 100th humble number is 450.
    The 1000th humble number is 385875.
    The 5842nd humble number is 2000000000.
    

    Source

    University of Ulm Local Contest 1996


    思路

    丑数的因子只有2,3,5,7,所以可以用从1开始乘这四个因子得到新的数字之后加到数组里面,更新1为2,如此迭代计算下去到算出5842个值,但是这样会存在重复元素问题还要考虑去重问题,数组的元素也不是有序的。

    改善这个思路可以有:

    设数组a存放所有的丑数,设立4个游标pos分别对应4个因子2,3,5,7,一个数组暂时存储所得的结果。每次都用游标值*4个因子,然后找到最小的值放入数组a,直到数量为5842为止。

    注意一个坑:111的英文是one hundred and eleventh,是th结尾的!!!!!!,112,113同理,实际上不止是这三个数,只要符合一定的特征。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    int num[6000];
    
    
    int findMin(int a[],bool f[],int len)
    {
    	int minvalue = a[0];
    	for(int i=0;i<len;i++)
    		minvalue = min(minvalue, a[i]);
    	
    	for(int i=0;i<len;i++)
    		minvalue == a[i] ? f[i] = true : f[i] = false;
    	return minvalue;
    }
    
    void Init()
    {
    	num[1] = 1;
    	bool vis[6000];
    	int tmp[4];
    	int pos[4] = {1,1,1,1};
    	int fac[4] = {2,3,5,7};
    	
    	for(int i=2;i<=5842;i++)
    	{
    		for(int j=0;j<4;j++)
    			tmp[j] = num[pos[j]] * fac[j];
    		
    		int minvalue = findMin(tmp,vis,4);
    		num[i] = minvalue;
    		for(int j=0;j<4;j++)
    			if(vis[j])
    				pos[j]++;
    	}
    }
    int main()
    {
    	int n;
    	Init();
    	while(cin>>n && n!=0)
    	{
    		string suffix = "th";
    		if(n%10==1 && n%100!=11)  
                suffix = "st";  
            else if(n%10==2 && n%100!=12)  
                suffix = "nd";  
            else if(n%10==3 && n%100!=13)  
                suffix = "rd";  
      
    		cout<<"The "<<n<<suffix<<" humble number is "<<num[n]<<"."<<endl; 
        }
    	return 0;
    }
    
  • 相关阅读:
    小技巧与阅读感悟
    Android常见漏洞介绍(1):Intent Scheme URL 攻击
    利用Mallet工具自动挖掘文本Topic
    Xie Tao的几个小建议PPT:关于读Paper,如何与他人交流,赶Deadline
    Python解析DNS数据包
    iOS 横竖屏适配 笔记
    多工程联编,cocopods的使用
    Call to unavailable function 'system': not available on iOS
    xxx referenced from: xxx in xxx.o
    YYModel学习总结YYClassInfo(1)
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/9852951.html
Copyright © 2011-2022 走看看