# Topic: There are four digits: 1, 2, 3 and 4. # How many different three digits can be formed without repeating numbers? How much is each? # Procedure analysis: traverse all possibilities and shave out duplicates. total = 0 for i in range(1,5): for…
题目: 输出9*9乘法口诀表. 程序分析:分行与分列的考虑,共9行9列,i控制行,j控制列. for i in range(1, 10): for j in range(1, i+1): print('%d*%d=%2d' % (j,i,i*j), end='') print() #右上三角格式输出九九乘法表 for i in range(1,10): for k in range(1,i): print (end=" ") for j in range(i,10): print(&q…
题目:将一个列表的数据复制到另一个列表中. 程序分析:使用列表[:],拿不准可以调用copy模块 import copy a = [,,,,['a','b']] b = a #赋值 c = a[:] #浅拷贝 d = copy.copy(a) #浅拷贝 e = copy.deepcopy(a) #深拷贝 a.append() a[].append('c') print('a=',a) print('b=',b) print('c=',c) print('d=',d) print('e=',e)…
程序设计: 斐波那契数列(Fibonacci sequence),从1,1开始,后面的每一项等于前面两项之和. 图方便就递归实现,图性能就用循环. # for 循环 target = int(input()) res = 0 a,b =1,1 for i in range(target-1): a, b=b, a+b print(a) #a,b=b,a+b是先计算等号右边,右边计算完成再依次赋值给左边. # 递归实现: def Fib(n): return 1 if n<=2 else Fib(…
# 题目: # 输入三个整数x,y,z,请把这三个数由大到小输出. # 程序分析: 练练手就随便找个排序算法实现一下,偷懒就直接调用函数. #方法一:排序 raw = [] for i in range(3): x = int(input('int%d:'%(i))) raw.append(x) for i in range(len(raw)): for j in range(i,len(raw)): if raw[i]>raw[j]: raw[i],raw[j]=raw[j],raw[i] p…
数字组合III 组给出两个整数n和k,返回从1......n中选出的k个数的组合. 您在真实的面试中是否遇到过这个题? Yes 样例 例如 n = 4 且 k = 2 返回的解为: [[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]] 解题 数字组合I 数组组合II 同样式DFS 本题只需要增加判断size 是否等于k的情况 public class Solution { /** * @param n: Given the range of numbers * @par…
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Note that '0' is not included.) Now, we write numbers using these digits, using each digit as many times as we want. For example, if D = {'1','3','5'},…
Combination问题描述:给定n和k,找出1-n之间所有k个数的组合,例如:n=3,k=2,返回 [[1,2] [1,3] [2,3]] 算法分析:利用递归.递归边界就是curr.size()==k. public List<List<Integer>> combine(int n, int k) { List<List<Integer>> result = new ArrayList<>(); List<Integer>…