【微软100题】求1+2+…+n
来源:互联网 发布:mac系统迁移到ssd 编辑:程序博客网 时间:2024/05/29 04:32
题目:求1+2+…+n,
要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
package test;/** * 题目:求1+2+...+n,要求不能使用乘除法,for,while,if,else,switch,case,条件判断语句(A?B:C) * @author Zealot * *思路,定义数组,设置大小,试用try,catch跳出递归。 *递归终止条件:数组越界,捕获异常 */public class Sum {private static int sum =0;private static int[] array = new int[100];private static int n = 0;private void add() {n++;try{array[n-1] =n;sum+=n;add();} catch(Exception e) {System.out.println("终止");}}public static void main(String[] args) {Sum s = new Sum();s.add();System.out.println(sum);}}
0 0
- 【微软100题】求1+2+…+n
- 求1+2+...+n 【微软面试100题 第十二题】
- 微软面试100题系列---求1+2+3+...+n
- 10. 微软面试题:题目:求1+2+…+n,
- 【从零单排之微软面试100题系列】12之求1+2+3+…+n
- 微软100题(12) 求1+2+3+...+n,不用减乘除
- 【微软100题】输入n,用最快的方法求Fibonacci数列的第n项
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- 求1+2+…+n
- CentOS 6.4 安装chrome
- 黑马程序员——类方法和关键字self
- 移动开发
- 常见算法在实际项目中的应用
- ubuntu下安装基本开发套件
- 【微软100题】求1+2+…+n
- stl---Deque小结
- selinux 和mysql
- Commons-logging + Log4j入门指南
- 读书札记-2013年12月
- DSP6000的几个简单优化技巧
- 数字信号处理的学习资源
- 长沙专场--【长沙专场】2015届SO挑战赛(晋级赛20日上午)
- 开源组件介绍