Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)
来源:互联网 发布:stl文件怎么测量数据 编辑:程序博客网 时间:2024/05/16 01:57
一、题目大意
本题要求写出前5482个仅能被2,3,5, 7 整除的数。
二、题解
这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算即可。还有一个不同之处在于输出上,这个题要求第n的英语表示。而英语中的表示呢,如果n的个位数是1,用nst表示个位数是2的用,nnd表示;个位数是3的,用nrd表示。但是n的最后两位是11、12、13的还是用nth表示,其他的也是用th表示。
三、java代码
import java.util.Scanner; public class Main { public static String format(int n){ if(n % 10==1 && n% 100 !=11) return n+"st"; if(n % 10==2 && n% 100 !=12) return n+"nd"; if(n % 10==3 && n% 100 !=13) return n+"rd"; return n+"th"; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; int i2_mul; int i3_mul; int i5_mul; int i7_mul; long[] ugly=new long[5843]; i2_mul = 1; i3_mul = 1; i5_mul = 1; i7_mul = 1; ugly[1]=1; for( int i = 2; i <= 5842; i++ ){ ugly[i] = Math.min(Math.min(ugly[i2_mul]*2, Math.min(ugly[i3_mul]*3,ugly[i5_mul]*5)), ugly[i7_mul]*7); if(ugly[i] == ugly[i2_mul]*2 ) i2_mul++; if(ugly[i] == ugly[i3_mul]*3 ) i3_mul++; if(ugly[i] == ugly[i5_mul]*5) i5_mul++; if(ugly[i] == ugly[i7_mul]*7) i7_mul++; } while((n=sc.nextInt())!=0){ System.out.println("The "+format(n)+" humble number is "+ugly[n]+"."); } } }
- Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)
- POJ-1338 Ugly Numbers-被2,3,5整除的数/只包含因子2 3 5的数
- (hdu step 3.2.5)Humble Numbers(从小到大输出因子只有2,3,,5,7的数)
- poj 2247 Humble Numbers
- poj 2247 Humble Numbers
- poj 2247 Humble Numbers
- POJ 2247 Humble Numbers
- poj 2247 Humble Numbers
- poj 2247 Humble Numbers
- POJ 2247 Humble Numbers
- POJ 2247 Humble Numbers
- HDU 1058 Humble Numbers 求质因数2357的数
- Htm5 求同时被3和7整除的数
- POJ 2247 Humble Numble(我的水题之路——丑数2,3,5,7)
- POJ 2247(Humble Numbers)
- POJ 2247 Humble Numbers 笔记
- POJ-2247(pair的运用)(Humble Numbers )
- 求1~100能被7整除的数之和
- 写入注册表,开机启动
- ISTP期刊检索
- Android Audio代码分析2 - 函数getMinBufferSize (补充)
- IOS开发之nsrunloop开发指南
- C调用Python脚本代码
- Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)
- CSS中class和id的区别
- Centos 查看web日志技巧 (涉及到内容较多,性能,安全,进程,命令网络监控...都有)
- zju acmclub 1026:布尔矩阵
- Linux/Unix平台可执行文件格式分析
- PythonChallenge 挑战之路 Level-22
- 动态代理在spring中的应用
- DIV CSS布局
- CocoStudioV0.3.1.0版本发布