while i >0:k += m[i-1]* t # t记录某一位置对应的权值t *=10i -=1
4.确定程序框架
程序的流程图如图所示。
5.完整的程序
%%time
# 回文数if __name__ =='__main__':m =[1]*17count =0print("No. number it's square(palindrome)")for n inrange(1,256):# 穷举n的取值范围k, i, t, a =0,0,1, n*n # 计算n的平方squ = awhile a !=0:# 从低到高分解数a的每一位存于数组m[1]~m[16]m[i]= a %10a //=10i +=1while i >0:k += m[i-1]* t # t记录某一位置对应的权值t *=10i -=1if k == squ:count +=1print("%2d%10d%10d"%(count, n, n*n))
No. number it's square(palindrome)1 1 12 2 43 3 94 11 1215 22 4846 26 6767 101 102018 111 123219 121 14641
10 202 40804
11 212 44944
CPU times: user 2.72 ms, sys: 0 ns, total: 2.72 ms
Wall time: 1.99 ms
%%time
# 回文数判断if __name__ =='__main__':x =int(input("请输入一个5位数整数:"))if x <10000or x >99999:print("输入错误")else:ten_thousand = x //10000#拆分最高位万位thousand = x %10000//1000#拆分千位ten = x %100//10#拆分十位indiv = x %10#拆分个位if indiv == ten_thousand and ten == thousand:print("%d是回文数"%x)else:print("%d不是回文数"%x)
12321是回文数
CPU times: user 60.4 ms, sys: 11.3 ms, total: 71.7 ms
Wall time: 5.86 s
一 引例
求解二元一次方程组 { a 11 x 1 a 12 x 2 b 1 a 21 x 1 a 22 x 2 b 2 \begin{cases} a_{11}x_1a_{12}x_2b_1\\ a_{21}x_1a_{22}x_2b_2\\ \end{cases} {a11x1a12x2b1a21x1a22x2b2 解: 1 a 21 − 2 a 11 ⇒ x 2 a 11 b 2 − a…