2018暑期SICNU-ACM组集训报告(4)

题目:
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500’th ugly number.
Input
There is no input to this program.
Output
Output should consist of a single line as shown below, with ‘<number>’ replaced by the number
computed.
Sample Output
The 1500'th ugly number is <number>.

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=72

题意:
找丑数,丑数只能被2或3或5整除,输出第1500个丑数(1包含在内)

AC代码:

#include<bits/stdc++.h>
int min(int a,int b)
{
    return a>b?b:a;
}
int main()
{
  int ugly[1505] = {1};
  int n2=0,n3=0,n5=0;
  int i;
  for(i=1;i<1500;i++)
  {
    for(;n2<i;n2++)
      if(ugly[n2]*2>ugly[i-1]) break;
    for(;n3<i;n3++)
      if(ugly[n3]*3>ugly[i-1]) break;
    for(;n5<i;n5++)
      if(ugly[n5]*5>ugly[i-1]) break;
    ugly[i]=min(ugly[n2]*2,ugly[n3]*3);
    ugly[i]=min(ugly[i],ugly[n5]*5);
  }

  cout<<"The 1500'th ugly number is"<< ugly[1499])<<endl;
  return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • 自从分开以后 独家自制的红烧肉,变得奢侈不已,变成遥不可及。因为再怎么努力还原制作,也没有了那个味。 每周一煲的排...
    诗意贝蜀黍阅读 559评论 0 4
  • 爱一个不值得爱的人,终究会伤心自己为难他人,何不放手?放过他也放过自己,宁愿做灯红酒绿中张牙舞爪的鬼,也不要做现实...
    离岸之旅阅读 219评论 0 0
  • 千古边城沱江扭,渔舟一叶江自流,晚来篝火古城醉朦胧。闲似独钓参差柳,采风少女绰影留,玉笛声中信笔勾勒扭。 ...
    乆勼若谷阅读 356评论 0 2
  • 很少会把当下的事件去记录,也仅仅是在自己内心的某一夹层里刻录下了一次次的感觉和情绪。我的身体真的很敏感,会...
    繩繩兮不可名阅读 112评论 0 0