`
85977328
  • 浏览: 1871083 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

算法1

 
阅读更多
用JAVA打印:
1  
1   1  
1   2   1  
1   3   3   1  
1   4   6   4   1  
1   5   10   10   5   1 
..........................
............................


import java.util.LinkedList;
import java.util.List;

public class Test2 {

	public static void main(String[] args) {
		List<Integer> current = new LinkedList<Integer>();
		current.add(1);
		for (int i = 0; i < 6; i++) {
			System.out.println(current);
			current = getList(current);
		}
	}

	public static List<Integer> getList(List<Integer> list) {
		List<Integer> result = new LinkedList<Integer>();
		if (list.size() == 1) {
			result.add(1);
			result.add(1);
			return result;
		}

		for (int i = 0; i < list.size() + 1; i++) {
			if (i == 0 || i == list.size()) {
				result.add(1);
				continue;
			}
			result.add(list.get(i) + list.get(i - 1));
		}
		return result;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics