Python的一些简单示例(2)
来源:互联网 发布:mac口红dare you和d 编辑:程序博客网 时间:2024/05/19 15:40
1、判断是否为素数
import mathx = 7for i in range(2,(int)(math.sqrt(x))+1): if(x%i == 0): print("x is not a sushu") breakelse: print("x is a sushu")//当for非正常结束则不执行else,如果for正常结束则执行else
2、依次打印50个素数
import mathcount = 0num = 2while(count < 50): for i in range(2,(int)(math.sqrt(num))+1): if(num%i == 0): break else: print("%4d" % num,end=" ") count += 1 num += 1
3、判断一个数是不是回文数
original = 12321num = originalresult = 0while(num != 0): result = result*10 + num%10 num = int(num/10)if(original == result): print("the num is a palin\n")else: print("the num is not a palin\n")
4、判断一个数是不是回文素数。
import mathdef is_palin(num): temp = num result1 = 0 while(temp != 0): result1 = result1*10 + temp%10 temp = int(temp/10) if(num == result1): return True else: return Falsedef is_parim(num): for i in range(2,int(math.sqrt(num))+1): if(num % i == 0): return False break else: return Truenum = 13131if(is_palin(num) and is_parim(num)): print("ok")else: print("no")
5、判断2033年的一月一号是星期几?【1800年1月1日是星期三,如果我可以得到从1800年1月1日到2033年1月1日的总天数,那么我用(3+total days)%7结果即是1800年1月1日是星期几】
def is_leap(year): if((year%4 == 0 and year%100 != 0) or (year%400 == 0)) : return True else: return Falsedef is_month_days(year,month): if(month in (1,3,5,7,8,10,12)): return 31 elif(month in (4,6,9,11)): return 30 elif(is_leap(year)): return 29 else: return 28def is_total_days(year,month): days = 0 for i in range(1800,year): if(is_leap(i)): days += 366 else: days += 365 for j in range(1,month): days += is_month_days(year,j) return daysdef is_which_day(year,month): return (3+is_total_days(year,month))%7year = 2033month = 12print(is_which_day(year,month))
6、斐波那契数
(非递归)
def unfib(n): if(n==1 or n==2): return 1 f1 = 1 f2 = 1 i = 2 while(i < n): f3 = f1 + f2 f1 = f2 f2 = f3 i+=1 return f3print(unfib(6))
(递归)
def fib(n): if(n==1 or n==2): return 1 else: return fib(n-1)+fib(n-2)print(fib(9))
7、随机停车问题
import randomdef parking(low,high): if(high - low < 1): return 0 else: x = random.uniform(low,high-1) return parking(low,x) + 1 + parking(x+1,high)print(parking(0,5))
8、汉诺塔问题
def hanoi(n,A,B,C): if(n == 1): print("move ",n," from ",A,"to ",C) else: hanoi(n-1,A,C,B) print("move ",n," from ",A,"to ",C) hanoi(n-1,B,A,C)hanoi(3,"left","mid","right")结果如下: move 1 from left to rightmove 2 from left to midmove 1 from right to midmove 3 from left to rightmove 1 from mid to leftmove 2 from mid to rightmove 1 from left to right
9、统计一个字符串中元音字母的个数
def vowle_count(s): count = 0 for c in s: if(c in ('a','e','i','o','u','A','E','I','O','U')): count += 1 return countprint(vowle_count("hello world"))
10、判断文件中的哪些名字是回文数,并且将其打印出来
(非递归)
def parlindrom(s): low = 0 high = len(s)-1 while(low < high): if(s[low] != s[high]): return False low += 1 high -= 1 return Truef = open("test.txt","r")for line in f: if(parlindrom(line.strip())): print(line.strip())f.close()
(递归)
def parlindrom(s): if(len(s) <= 1): return True else: if(s[0] != s[-1]): return False else: return parlindrom(s[1:-1])f = open("test.txt","r")for line in f: if(parlindrom(line.strip())): print(line.strip())f.close()
0 0
- Python的一些简单示例(2)
- Python的一些简单示例
- Java的一些简单示例(2)
- C++的一些简单示例(2)
- Java的一些简单示例(1)
- C++的一些简单示例(3)
- C++的一些简单示例(4)
- C++的一些简单示例(5)
- C++的一些简单示例
- python 爬虫的简单示例
- C#调用Python脚本的简单示例,IronPython(转载)
- 一些简单的Python语法
- python 一些简单的测试
- 最简单的 python 多线程示例
- python log模块的简单示例
- [Python]TCP编程的简单示例
- [Python]UDP编程的简单示例
- Python(2.7.x)多线程的简单示例
- LinkedHashMap源码剖析
- 什么是RF、IF信号
- presentViewController:animated:YES view will not appear until user taps again
- 拉链表
- mysql连接池-druid
- Python的一些简单示例(2)
- 设计模式是一个程序的主体!
- 59. Spiral Matrix II
- 字符串换行
- 导出(下载)数据库查询的数据生成csv文件
- ARM杂项伪指令
- 【bzoj3930】【SCOI2015】【选数】【容斥】
- Bzoj3028:食物:生成函数
- Jqyery根据行序号和列序号获取单元格