1. 质因数分解

public static List<Integer> factorize(int n){
List<Integer> factors = new ArrayList();
int i;
boolean isPrime = true;
for (i = 2; i <= Math.sqrt(n); i++){
if (n % i == 0){
factors.add(i);
isPrime = false;
break;
}
}
if (isPrime){
factors.add(n);
}else{
factors.addAll(factorize( n / i));
}
return factors;
}

2.题目:求s = a + aa + aaa + aaaa + aa...a的值,其中a是一个数字。例如2 + 22 + 222 + 2222 + 22222(此时共有5个数相加),

解法1:非递归解法

public static int f2(int a, int n){
int s = 0;
int an = 0;
for (int i = 1; i <= n; i++){
an += a * Math.pow(10, n-1);
s += an;
}
return s;
}

解法2:递归解法

 // g = aa...a
public static int g(int a, int n){
if (n > 1){
return g(a, n -1) + a * (int)Math.pow(10, n-1);
}else if ( n == 1){
return a;
}else {
throw new RuntimeException();
}
}
//f = a + aa + aaa + aaaa + aa...a
public static int f(int a, int n){
if (n > 1){
return f(a, n -1) + g(a,n);
}else if ( n == 1){
return a;
}else {
throw new RuntimeException();
}
}

最新文章

  1. Nodejs windows的安装
  2. linux根文件系统制作
  3. Modify textures at runtime
  4. cocos2d-x 小技巧
  5. linux 用grep匹配横线
  6. windows2008 R2 安装wampserver
  7. php单词里的大写字母
  8. sign starfieldtech
  9. Please ensure that adb is correctly located at &#39;...adb.exe&#39; and can be executed.
  10. golang实现udp接入服务器
  11. UI 公钥加密
  12. eclipse xml自动提示
  13. Microsoft CRM-QueryExpression 成员
  14. Tomcat PermGen space的解决方案
  15. 大数据量下的集合过滤—Bloom Filter
  16. Java并发编程:JMM (Java内存模型) 以及与volatile关键字详解
  17. js 获取属性名称
  18. vue中解决跨域问题
  19. Android Exception Type &quot;share_dialog_title&quot; is not translated in en, zh-rTW strings
  20. kibana 的search 的的搜索提示挡住输入框

热门文章

  1. 前端开发 Vue -3axios
  2. 一步步用ABAP Development Tools连接SAP云平台上的ABAP编程环境
  3. Ubuntu 文件和目录常用命令
  4. jmeter——http、jdbc、soap请求
  5. php高精度计算
  6. Python 之 PyMySQL 安装和使用
  7. Java&amp;Selenium数据驱动【DataProvider+TestNG+Mysql】
  8. rabbitmq 公平分发和消息接收确认(转载)
  9. linux实操_shell预定义变量
  10. 18、属性赋值-@Value赋值