阶乘因式分解
2014-02-25 21:46
183 查看
/*描述 给定两个数m,n,其中m是一个素数。 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m。 输入 第一行是一个整数s(0<s<=100),表示测试数据的组数 随后的s行, 每行有两个整数n,m。 输出 输出m的个数。 样例输入 2 100 5 16 2 样例输出 24 15*/ import java.util.*; public class Main9 { public static int n; public static int[][] ar; public static int sum; public static int tmp; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); ar = new int [2]; for (int i = 0; i < n; i++) { ar[i][0] = sc.nextInt(); ar[i][1] = sc.nextInt(); } //这里能优化么??? for (int i = 0; i < n; i++) { sum = 0; for (int j = ar[i][0]; j > 0; j--) { if (j % ar[i][1] == 0) { sum++; tmp = j / ar[i][1]; while (tmp % ar[i][1] == 0 && tmp != 0) { tmp = tmp / ar[i][1]; sum++; } } } System.out.println(sum); } } }
相关文章推荐
- 桥接,NAT,Host Only的区别
- jboss:在standalone.xml中设置系统属性(system-properties)
- AssetManager
- 学习Linux驱动要求
- SIFT算法的应用--目标识别之Bag-of-words模型
- 命令模式
- Irrlicht学习备忘录——13 RenderToTexture
- 第一个Java3D小程序SimpleCone
- 回顾
- OpenGL: 转换矩阵
- 10个必需的iOS开发工具和资源
- sqlserver锁表、解锁、查看销表
- 处理文件名和目录
- asp.net中实现页面间的参数传递
- Delphi 7下最小化到系统托盘
- SecureCRT设置彩色和显示中文
- stl-基本知识
- service--wifi服务
- 按钮绑定回车事件
- 关于cout 栈运用的实例解析