google面试题目:寻找丑数--使用double防止数据溢出

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


分析:假设数组ugly[N]中存放不断产生的丑数,
初始只有一个丑数ugly[0]=1,由此出发,
下一个丑数由因子2,3,5竞争产生,得到ugly[0]*2,
ugly[0]*3, ugly[0]*5, 显然最小的那个数是新的丑数,
所以第2个丑数为ugly[1]=2,开始新一轮的竞争,
由于上一轮竞争中,因子2获胜,这时因子2应该乘以ugly[1]才显得公平,
得到ugly[1]*2,ugly[0]*3,ugly[0]*5,
因子3获胜,ugly[2]=3,同理,下次竞争时因子3应该乘以ugly[1],
即:ugly[1]*2, ugly[1]*3, ugly[0]*5, 因子5获胜,得到ugly[3]=5,
重复这个过程,直到第n个丑数产生。总之:
每次竞争中有一个(也可能是两个)因子胜出,下一次竞争中
胜出的因子就应该加大惩罚!


本质就是2 ,3 ,5 的乘积的组合,不过要按照顺序组合,
每次都将2,3,5乘以一个已经是丑数的数,
再比较三者的大小,小的进入数组,然后继续比较
例如:1是丑数,那么下一个2 * 1、3 * 1和5*1,
那么获得的是2,2进入数组,下面比较的是2*2、3*1 和5*1
( 重视因为后面的3*1和5*1的大小并不断定,所以还要持续进行比较)
获得3进入数组,再比较 2 * 2、3 * 2和5*1.


感谢网友的评论,发现了程序的问题
是因为int型表示数据范围有限倒置数据溢出,后来改成unsigned long int 仍然溢出
最后只能改成double 了
三者的表示的数据范围如下:



numeric_limits<int>::max --> 2147483647    第1692个丑数越界
numeric_limits<unsigned long int>::max --> 4294967295  第1849个丑数越界
numeric_limits<double>::max --> 1.79769e+308



************************************************************************/



************************************************************************/
#include<iomanip> // 精度控制头文件
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<limits>
using namespace std;


/*the smallest in the three*/
//无符号长整型    防止数据溢出
double mymin(double a, double b, double c)
{
    double temp = (a < b ? a : b);
    return (temp < c ? temp : c);
}
//无符号长整型    防止数据溢出
double FindUgly(int n)
{
    double* ugly = new double[n];
    ugly[0] = 1;
    int index2 = 0;
    int index3 = 0;
    int index5 = 0;
    int index = 1;
    while (index < n)
    {
        double val = mymin(ugly[index2]*2, ugly[index3]*3, ugly[index5]*5);
        //竞争产生下一个丑数
        if (val == ugly[index2]*2)
        //将产生这个丑数的index*向后挪一位;
            ++index2;
        if (val == ugly[index3]*3)
        //这里不能用elseif,因为可能有两个最小值,这时都要挪动;
            ++index3;
        if (val == ugly[index5]*5)
            ++index5;
        ugly[index++] = val;
    }
    double result = ugly[n-1]; //ugly[0] = 1  是第一个丑数
    delete[] ugly;
    return result;
}
void test1_29()
{
    for(int i=1;i<30;i++)
    cout<<"the "<<i<<" th ugly number is "<<FindUgly(i)<<endl;
}
void test1680_1700()
{
    for(int i=1680;i<1701;i++)
    cout<<"the "<<i<<" th ugly number is "<<setprecision(10)<<FindUgly(i)<<endl;
}
void test1800_1991()
{
    for(int i=1800;i<1991;i++)
    cout<<"the "<<i<<" th ugly number is "<<setprecision(10)<<FindUgly(i)<<endl;
}
void test1991_2013()
{
    for(int i=1991;i<2014;i++)
    cout<<"the "<<i<<" th ugly number is "<<setprecision(10)<<FindUgly(i)<<endl;
}
int main()
{
    cout<<"numeric_limits<int>::max --> "
        <<(std::numeric_limits<int>::max)()<<endl;
    cout<< "numeric_limits<unsigned long int>::max --> "
        <<(std::numeric_limits<unsigned long int>::max)()<<endl;
    cout<< "numeric_limits<double>::max --> "
        <<(std::numeric_limits<double>::max)()<<endl;
    test1_29();
    cout<<endl<<"----------------------------"<<endl;
    test1680_1700();
    cout<<endl<<"----------------------------"<<endl;
    test1800_1991();
    cout<<endl<<"----------------------------"<<endl;
    test1991_2013();






    return 0;


}
/***************************
numeric_limits<int>::max --> 2147483647
numeric_limits<unsigned long int>::max --> 4294967295
numeric_limits<double>::max --> 1.79769e+308
the 1 th ugly number is 1
the 2 th ugly number is 2
the 3 th ugly number is 3
the 4 th ugly number is 4
the 5 th ugly number is 5
the 6 th ugly number is 6
the 7 th ugly number is 8
the 8 th ugly number is 9
the 9 th ugly number is 10
the 10 th ugly number is 12
the 11 th ugly number is 15
the 12 th ugly number is 16
the 13 th ugly number is 18
the 14 th ugly number is 20
the 15 th ugly number is 24
the 16 th ugly number is 25
the 17 th ugly number is 27
the 18 th ugly number is 30
the 19 th ugly number is 32
the 20 th ugly number is 36
the 21 th ugly number is 40
the 22 th ugly number is 45
the 23 th ugly number is 48
the 24 th ugly number is 50
the 25 th ugly number is 54
the 26 th ugly number is 60
the 27 th ugly number is 64
the 28 th ugly number is 72
the 29 th ugly number is 75


----------------------------
the 1680 th ugly number is 2025000000
the 1681 th ugly number is 2038431744
the 1682 th ugly number is 2040733440
the 1683 th ugly number is 2048000000
the 1684 th ugly number is 2050312500
the 1685 th ugly number is 2066242608
the 1686 th ugly number is 2073600000
the 1687 th ugly number is 2097152000
the 1688 th ugly number is 2099520000
the 1689 th ugly number is 2109375000
the 1690 th ugly number is 2123366400
the 1691 th ugly number is 2125764000
the 1692 th ugly number is 2147483648
the 1693 th ugly number is 2149908480
the 1694 th ugly number is 2152336050
the 1695 th ugly number is 2160000000
the 1696 th ugly number is 2176782336
the 1697 th ugly number is 2187000000
the 1698 th ugly number is 2197265625
the 1699 th ugly number is 2211840000
the 1700 th ugly number is 2214337500


----------------------------
the 1800 th ugly number is 3456000000
the 1801 th ugly number is 3486784401
the 1802 th ugly number is 3499200000
the 1803 th ugly number is 3515625000
the 1804 th ugly number is 3538944000
the 1805 th ugly number is 3542940000
the 1806 th ugly number is 3583180800
the 1807 th ugly number is 3587226750
the 1808 th ugly number is 3600000000
the 1809 th ugly number is 3623878656
the 1810 th ugly number is 3627970560
the 1811 th ugly number is 3645000000
the 1812 th ugly number is 3662109375
the 1813 th ugly number is 3673320192
the 1814 th ugly number is 3686400000
the 1815 th ugly number is 3690562500
the 1816 th ugly number is 3732480000
the 1817 th ugly number is 3750000000
the 1818 th ugly number is 3774873600
the 1819 th ugly number is 3779136000
the 1820 th ugly number is 3796875000
the 1821 th ugly number is 3822059520
the 1822 th ugly number is 3826375200
the 1823 th ugly number is 3840000000
the 1824 th ugly number is 3869835264
the 1825 th ugly number is 3874204890
the 1826 th ugly number is 3888000000
the 1827 th ugly number is 3906250000
the 1828 th ugly number is 3932160000
the 1829 th ugly number is 3936600000
the 1830 th ugly number is 3955078125
the 1831 th ugly number is 3981312000
the 1832 th ugly number is 3985807500
the 1833 th ugly number is 4000000000
the 1834 th ugly number is 4026531840
the 1835 th ugly number is 4031078400
the 1836 th ugly number is 4050000000
the 1837 th ugly number is 4076863488
the 1838 th ugly number is 4081466880
the 1839 th ugly number is 4096000000
the 1840 th ugly number is 4100625000
the 1841 th ugly number is 4132485216
the 1842 th ugly number is 4147200000
the 1843 th ugly number is 4194304000
the 1844 th ugly number is 4199040000
the 1845 th ugly number is 4218750000
the 1846 th ugly number is 4246732800
the 1847 th ugly number is 4251528000
the 1848 th ugly number is 4271484375
the 1849 th ugly number is 4294967296
the 1850 th ugly number is 4299816960
the 1851 th ugly number is 4304672100
the 1852 th ugly number is 4320000000
the 1853 th ugly number is 4353564672
the 1854 th ugly number is 4374000000
the 1855 th ugly number is 4394531250
the 1856 th ugly number is 4423680000
the 1857 th ugly number is 4428675000
the 1858 th ugly number is 4478976000
the 1859 th ugly number is 4500000000
the 1860 th ugly number is 4529848320
the 1861 th ugly number is 4534963200
the 1862 th ugly number is 4556250000
the 1863 th ugly number is 4586471424
the 1864 th ugly number is 4591650240
the 1865 th ugly number is 4608000000
the 1866 th ugly number is 4613203125
the 1867 th ugly number is 4649045868
the 1868 th ugly number is 4665600000
the 1869 th ugly number is 4687500000
the 1870 th ugly number is 4718592000
the 1871 th ugly number is 4723920000
the 1872 th ugly number is 4746093750
the 1873 th ugly number is 4777574400
the 1874 th ugly number is 4782969000
the 1875 th ugly number is 4800000000
the 1876 th ugly number is 4831838208
the 1877 th ugly number is 4837294080
the 1878 th ugly number is 4860000000
the 1879 th ugly number is 4882812500
the 1880 th ugly number is 4897760256
the 1881 th ugly number is 4915200000
the 1882 th ugly number is 4920750000
the 1883 th ugly number is 4976640000
the 1884 th ugly number is 4982259375
the 1885 th ugly number is 5000000000
the 1886 th ugly number is 5033164800
the 1887 th ugly number is 5038848000
the 1888 th ugly number is 5062500000
the 1889 th ugly number is 5096079360
the 1890 th ugly number is 5101833600
the 1891 th ugly number is 5120000000
the 1892 th ugly number is 5125781250
the 1893 th ugly number is 5159780352
the 1894 th ugly number is 5165606520
the 1895 th ugly number is 5184000000
the 1896 th ugly number is 5242880000
the 1897 th ugly number is 5248800000
the 1898 th ugly number is 5273437500
the 1899 th ugly number is 5308416000
the 1900 th ugly number is 5314410000
the 1901 th ugly number is 5368709120
the 1902 th ugly number is 5374771200
the 1903 th ugly number is 5380840125
the 1904 th ugly number is 5400000000
the 1905 th ugly number is 5435817984
the 1906 th ugly number is 5441955840
the 1907 th ugly number is 5467500000
the 1908 th ugly number is 5509980288
the 1909 th ugly number is 5529600000
the 1910 th ugly number is 5535843750
the 1911 th ugly number is 5598720000
the 1912 th ugly number is 5625000000
the 1913 th ugly number is 5662310400
the 1914 th ugly number is 5668704000
the 1915 th ugly number is 5695312500
the 1916 th ugly number is 5733089280
the 1917 th ugly number is 5739562800
the 1918 th ugly number is 5760000000
the 1919 th ugly number is 5804752896
the 1920 th ugly number is 5811307335
the 1921 th ugly number is 5832000000
the 1922 th ugly number is 5859375000
the 1923 th ugly number is 5898240000
the 1924 th ugly number is 5904900000
the 1925 th ugly number is 5971968000
the 1926 th ugly number is 5978711250
the 1927 th ugly number is 6000000000
the 1928 th ugly number is 6039797760
the 1929 th ugly number is 6046617600
the 1930 th ugly number is 6075000000
the 1931 th ugly number is 6103515625
the 1932 th ugly number is 6115295232
the 1933 th ugly number is 6122200320
the 1934 th ugly number is 6144000000
the 1935 th ugly number is 6150937500
the 1936 th ugly number is 6198727824
the 1937 th ugly number is 6220800000
the 1938 th ugly number is 6250000000
the 1939 th ugly number is 6291456000
the 1940 th ugly number is 6298560000
the 1941 th ugly number is 6328125000
the 1942 th ugly number is 6370099200
the 1943 th ugly number is 6377292000
the 1944 th ugly number is 6400000000
the 1945 th ugly number is 6442450944
the 1946 th ugly number is 6449725440
the 1947 th ugly number is 6457008150
the 1948 th ugly number is 6480000000
the 1949 th ugly number is 6530347008
the 1950 th ugly number is 6553600000
the 1951 th ugly number is 6561000000
the 1952 th ugly number is 6591796875
the 1953 th ugly number is 6635520000
the 1954 th ugly number is 6643012500
the 1955 th ugly number is 6710886400
the 1956 th ugly number is 6718464000
the 1957 th ugly number is 6750000000
the 1958 th ugly number is 6794772480
the 1959 th ugly number is 6802444800
the 1960 th ugly number is 6834375000
the 1961 th ugly number is 6879707136
the 1962 th ugly number is 6887475360
the 1963 th ugly number is 6912000000
the 1964 th ugly number is 6973568802
the 1965 th ugly number is 6998400000
the 1966 th ugly number is 7031250000
the 1967 th ugly number is 7077888000
the 1968 th ugly number is 7085880000
the 1969 th ugly number is 7119140625
the 1970 th ugly number is 7166361600
the 1971 th ugly number is 7174453500
the 1972 th ugly number is 7200000000
the 1973 th ugly number is 7247757312
the 1974 th ugly number is 7255941120
the 1975 th ugly number is 7290000000
the 1976 th ugly number is 7324218750
the 1977 th ugly number is 7346640384
the 1978 th ugly number is 7372800000
the 1979 th ugly number is 7381125000
the 1980 th ugly number is 7464960000
the 1981 th ugly number is 7500000000
the 1982 th ugly number is 7549747200
the 1983 th ugly number is 7558272000
the 1984 th ugly number is 7593750000
the 1985 th ugly number is 7644119040
the 1986 th ugly number is 7652750400
the 1987 th ugly number is 7680000000
the 1988 th ugly number is 7688671875
the 1989 th ugly number is 7739670528
the 1990 th ugly number is 7748409780


----------------------------
the 1991 th ugly number is 7776000000
the 1992 th ugly number is 7812500000
the 1993 th ugly number is 7864320000
the 1994 th ugly number is 7873200000
the 1995 th ugly number is 7910156250
the 1996 th ugly number is 7962624000
the 1997 th ugly number is 7971615000
the 1998 th ugly number is 8000000000
the 1999 th ugly number is 8053063680
the 2000 th ugly number is 8062156800
the 2001 th ugly number is 8100000000
the 2002 th ugly number is 8153726976
the 2003 th ugly number is 8162933760
the 2004 th ugly number is 8192000000
the 2005 th ugly number is 8201250000
the 2006 th ugly number is 8264970432
the 2007 th ugly number is 8294400000
the 2008 th ugly number is 8303765625
the 2009 th ugly number is 8388608000
the 2010 th ugly number is 8398080000
the 2011 th ugly number is 8437500000
the 2012 th ugly number is 8493465600
the 2013 th ugly number is 8503056000


Process returned 0 (0x0)   execution time : 1.427 s
Press any key to continue.




****************************/ 


你可能感兴趣的:(C++,算法,内存,面试题,笔试面试)