
第10次落地时,共经过多少米?第10次反弹多高?
1.程序分析:见下面注释
2.程序源代码:
Sn=100.0
Hn=Sn/2
for n in range(2,11):
Sn +=2 * Hn
Hn /=2
prin t 'Total of road is %f % Sn
prin t 'The ten th is %f meter' % Hn
III
题目2:打印出所有的水仙花数”所谓水仙花数”是指一个三位数,其各位数字立方和 等于该数
本身。例如:153是一个 水仙花数”因为153=1的三次方+ 5的三次方+ 3的三次方
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
2.程序源代码:
III
for n in range(100,1001):
i=n / 100
j=n / 10 % 10 k=n % 10
if i * 100 + j * 10 + k==i + j 2 + k 3:
prin t "%-5d" % n
III
题目3:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多 少?
1•程序分析:可填在百位、十位、个位的数字都是 1、2、3、4。组成所有的排列后再去
掉不满足条件的排列。
2.程序源代码:
III
for i in ran ge(1,5):
for j in ran ge(1,5):
for k in ran ge(1,5):
if( i !=k ) and (i !=j) and (j !=k):
print i,j,k
III
题目4 :企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10% ; 利润高
于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可 可提
成7.5% ; 20万到40万之间时,高于20万元的部分,可提成5% ; 40万到60万之间时 高于
40万元的部分,可提成3% ; 60万到100万之间时,高于60万元的部分,可提成1.5%, 高于
100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总 数?
1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
2.程序源代码:
III
bonus1=100000 * 0.1
bonus2=bonusl + 100000 * 0.500075 bonus4=bonus2 + 200000 * 0.5
bon us6=bo nus4 + 200000 * 0.3 bonus10=bonus6 + 400000 * 0.15 i=i nt(raw_i nput(' in put gai n:n'))
if i <=100000:
bonus=i * 0.1
elif i <=200000:
bonus=bonus1 + (i - 100000) * 0.075
elif i <=400000:
bon us=bo nus2 + (i - 200000) * 0.05
elif i <=600000:
bon us=bo nus4 + (i - 400000) * 0.03
elif i <=1000000:
bon us=bo nus6 + (i - 600000) * 0.015 else:
bon us=bo nu s10 + (i - 1000000) * 0.01
prin t 'b onu s=',b onus
III
题目5 :一个整数,它加上100后是一个完全平方数,再加上 268又是一个完全平方数, 请问该数是多少?
268后再开
1•程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上 方,如果开方后
的结果满足如下条件,即是结果。请看具体分析:
2.程序源代码:
#in elude "math.h"
mai n()
{
long int i,x,y, z;
for (i=1;i<100000;i++)
{ x=sqrt(i+100);
y=sqrt(i+268);
if(x x==i+100&&y y==i+268)
prin tf(" n%ld n",i);
}
} import math
for i in ran ge(10000):
#转化为整型值
x=i nt(math.sqrt(i + 100))
y=i nt(math.sqrt(i + 268))
if(x * x==i + 100) and (y * y==i + 268):
print i
题目6 :输入某年某月某日,判断这一天是这一年的第几天?
1•程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上 5天即本年的
第几天,特殊
情况,闰年且输入月份大于3时需考虑多加一天。
2.程序源代码: year=i nt(raw_i nput('year: n'))
mon th=i nt(raw_i nput('m on th: n'))
day=in t(raw_i nput('day:n'))
mon ths=(0,31,59,90,120,151,181,212,243,273,304,334)
if 0 v=month <=12:
sum=mon ths[m onth - 1]
else:
prin t 'data error'
sum +=day
leap=0
if (year % 400==0) or ((year % 4==0) and (year % 100 !=0)):
leap=1
if (leap==1) and (month > 2):
sum +=1
prin t 'it is the %dth day.' % sum
III
题目7:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到 x上,先将x与y进行比较,如果x>y则将x
与y的值进行交换, 然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小
2.程序源代码:
III
|=[]
for i in ran ge(3):
x=i nt(raw_i nput('i nteger: n'))
l.appe nd(x)
l.sort()
print l
题目8:用*号输出字母C的图案。
1•程序分析:可先用'*'号在纸上写出字母C,再分行输出
2.程序源代码: prin t 'Hello Python world! n'
prin t '*' * 10
for i in ran ge(5):
print ''
prin t '*' * 10
prin t '* n' * 6
题目9:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
1•程序分析:字符共有256个。不同字符,图形不一样。
2.程序源代码:
a=176
b=219
print chr(b),chr(a),chr(a),chr(a),chr(b) print chr(a),chr(b),chr(a),chr(b),chr(a) print chr(a),chr(a),chr(b),chr(a),chr(a) print chr(a),chr(b),chr(a),chr(b),chr(a) print chr(b),chr(a),chr(a),chr(a),chr(b)
题目10 :输出9*9 口诀。
1•程序分析:分行与列考虑,共 9行9列,i控制行,j控制列
2.程序源代码:
#i nclude "stdio.h"
mai n()
{
int i,j,result;
prin tf(" n");
for (i=1;i<10;i++)
{ for(j=1;j<10;j++)
{
result=i*j;
prin tf("%d*%d=%-3d",i,j,result);
}
III
for i in ran ge(1,10):
for j in ran ge(1,10):
result=i * j
prin t '%d * %d=% -3d' % (i,j,result)
prin t ''
题目11 :要求输出国际象棋棋盘。
1•程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方 格。
2.程序源代码:
#i nclude "stdio.h"
mai n()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
if((i+j)%2==0)
prin tf("%c%c",219,219);
else
printf("");
III
import sys
for i in ran ge(8):
for j in ran ge(8):
if(i + j) % 2==0:
sys.stdout.write(chr(219))
sys.stdout.write(chr(219))
else:
sys.stdout.write('')
prin t ''
III
题目12 :打印楼梯,同时在楼梯上方打印两个笑脸。
1•程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。
2.程序源代码:
III
import sys
sys.stdout.write(chr(1))
sys.stdout.write(chr(1))
prin t ''
for i in ran ge(1,11):
for j in ran ge(1,i):
sys.stdout.write(chr(219)) sys.stdout.write(chr(219))
print ”
III
题目13 :古典问题:有一对兔子,从出生后第 3个月起每个月都生一对兔子,小兔子长
到第三个月
后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1•程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21•…
2.程序源代码:
mai n()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
{ prin tf("%12ld %12ld",f1,f2);
if(i%2==0) prin tf(" n");
f1=f1+f2;
f2=f1+f2;
}
} f1=1
f2=1
for i in ran ge(1,21): if (i % 2)==0:
prin t ''
f1=f1 + f2
f2=f1 + f2
题目14 :判断101-200之间有多少个素数,并输出所有素数。
1•程序分析:判断素数的方法:用一个数分别去除 2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
2.程序源代码: h=0
leap=1
from math import sqrt from sys import stdout for m in ran ge(101,201): k=i nt(sqrt(m + 1)) for i in ran ge(2,k + 1): if m % i==0:
leap=0
break
if leap==1:
prin t '%-4d' % m
h +=1
if h % 10==0: print ''
Ieap=1
prin t 'The total is %d' % h
III
题目15 :将一个正整数分解质因数。例如:输入 90,打印出90=233*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数 k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
⑵如果*>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整 数你n,
重复执行第一步。
⑶如果n不能被k整除,则用k+1作为k的值重复执行第一步。
2.程序源代码: from sys import stdout
n=i nt(raw_i nput("i nput nu mber: n"))
prin t " n=%d" % n
for i in ran ge(2 ,n + 1):
while n !=i:
if n % i==0:
stdout.write(str(i))
stdout.write("*")
n=n / i
else:
break
题目16 :利用条件运算符的嵌套来完成此题:学习成绩 >=90分的同学用A表示,60-
分之间的用B表示,
60分以下的用C表示。
1•程序分析:(a>b)?a:b这是条件运算符的基本例子。
2.程序源代码:
不支持这个运算符
III
score=in t(raw_i nput(' in put score:n'))
if score >=90:
grade二'A'
elif score >=60:
grade二'B'
else:
grade=C
prin t '%d bel ongs to %s' % (score,grade)
III
题目17 :输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1•程序分析:利用while语句,条件为输入的字符不为’n'.
2.程序源代码:
III
import stri ng
s=raw_i nput(' in put a stri ng:n')
letters=0 space=0
digit=O
others=0
for c in s:
if c.isalpha():
letters +=1
elif c.isspace():
space +=1
elif c.isdigit():
digit +=1
else:
others +=1
prin t 'char=%d,space=%d,digit=%d,others=%d' % (letters,space,digit,others)
III
题目18 :求s=a+aa+aaa+aaaa+aa...a勺值,其中 a是一个数字。例如 2+22+222+2222+22222(此时
共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。
2.程序源代码:
Tn=0
Sn=[]
n=i nt(raw_i nput(' n=:n')) a=i nt(raw_i nput('a=: n'))
for count in ran ge( n):
Tn=Tn + a
a=a * 10
Sn. appe nd(T n)
print Tn
Sn=reduce(lambda x,y : x + y,S n)
print Sn
III
题目19 :一个数如果恰好等于它的因子之和,这个数就称为 完数”例如6=1 + 2 + 3.编 程
找出1000以内的所有完数。
1.程序分析:请参照程序 <--上页程序14.
2.程序源代码:
from sys import stdout for j in ran ge(2,1001): k=[]
n=-1
s=j
for i in ran ge(1,j):
if j % i==0:
n +=1
s -=i
k.appe nd(i)
if s==0:
print j for i in ran ge( n):
stdout.write(k[i])
stdout.write('')
print k[n]
100道Python编程题及答案(二) 题目21 :将一个数组逆序输出。
1.程序分析:用第一个与最后一个交换
2.程序源代码:
if n ame =='ma in a=[9,6,5,4,1]
N=le n(a)
print a
for i in ran ge(le n(a) / 2):
a[i],a[N - i - 1]=a[N - i - 1],a[i]
print a
题目22 :猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃 了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少
1.程序分析:采取逆向思维的方法,从后往前推断。
2.程序源代码:
x2=1
for day in ran ge(9,0,-1):
x1=(x2 + 1) * 2
x2=x1
print x1
III
题目23 :两个乒乓球队进行比赛,各出三人。甲队为 a,b,c三人,乙队为x,y,z三人。已 抽签决定
比赛名单。有人向队员打听比赛的名单。 a说他不和x比,c说他不和x,z比,请编程序
找出
三队赛手的名单。
1•程序分析:判断素数的方法:用一个数分别去除 2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
2.程序源代码: for i in ran ge(ord('x'),ord('z') + 1):
for j in ran ge(ord('x'),ord('z') + 1):
if i !=j:
for k in ran ge(ord('x'),ord('z') + 1):
if (i !=k) and (j !=k):
if (i !=ord('x')) and (k !=ord('x')) and (k !=ord('z')):
prin t 'order is a -- %st b -- %stc--%s' % (chr(i),chr(j),chr(k))
Illi
题目24 :打印出如下图案(菱形)
1•程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。
2.程序源代码: from sys import stdout
for i in ran ge(4):
for j in ran ge(2 - i + 1):
stdout.write('')
for k in ran ge(2 * i + 1):
stdout.write('*')
for i in ran ge(3):
for j in ran ge(i + 1):
stdout.write('')
for k in ran ge(4 - 2 * i + 1):
stdout.write('*')
题目25 :有一分数序列:2/1 , 3/2 , 5/3 , 8/5 , 13/8 , 21/13…求出这个数列的前 20项 之和。
1.程序分析:请抓住分子与分母的变化规律。
2.程序源代码:
#方法一
a=2.0
b=1.0
s=0
for n in ran ge(1,21):
s +=a / b
t=a
a=a + b
b=t
print s
#方法二
s=0.0
for n in ran ge(1,21):
s +=a / b
b,a=a , a + b
print s
s=0.0
for n in ran ge(1,21):
s +=a / b b,a=a , a + b
print s
#方法三 l=[]
for n in ran ge(1,21):
b,a=a,a + b
1.appe nd(a / b)
print reduce(lambda x,y: x + y,l)
III
题目 26 :求 1+2!+3!+...+20!的和
1•程序分析:此程序只是把累加变成了累乘
2.程序源代码:
III
#方法一
n=0
s=0
t=1
for n in ran ge(1,21):
t *=n
s +=t
prin t '1! + 2! + 3! + ... + 20!=%d' % s
#方法二
s=0
l=ra nge(1,21) def op(x):
r=1 for i in ran ge(1,x + 1):
r *=i
return r
s=sum(map(op,l))
prin t '1! + 2! + 3! + ... + 20!=%d' % s
III
题目27 :利用递归方法求5!。
1.程序分析:递归公式:fn=fn_1*4!
2.程序源代码:
def fact(j):
sum=0
if j==0:
sum=1
else:
sum=j * fact(j - 1)
return sum
100道Python编程题及答案(三)
题目41 :画图,综合例子。
1.程序分析:
2.程序源代码:
键盘不知道如何响应,先不写这个
#in elude "graphics.h"
#defi ne LEFT 0
#defi ne TOP 0
#defi ne RIGHT 639
#defi ne BOTTOM 479
#defi ne LINES 400
#defi ne MAXCOLOR 15
mai n()
{
int driver,mode,error;
int x1,y1;
int x2,y2;
int dx1,dy1,dx2,dy2,i=1;
int coun t=0;
int color=0;
driver=VGA;
mode=VGAHI;
in itgraph(& driver,&m ode,"");
x1=x2=y1=y2=10;
dx1= dy 1=2;
dx2=dy2=3;
while(!kbhit())
{
lin e(x1,y1,x2,y2);
x1+=dx1;y1+=dy1; x2+=dx2;y2+dy2;
if(x1<=LEFT||x1>=RIGHT)
dx 1= -dx1;
if(y1v=T0P||y1>=B0TT0M)
dy 1= -dy1;
if(x2<=LEFT||x2>=RIGHT) dx2=-dx2;
if(y2<=TOP||y2>=BOTTOM) dy2=-dy2;
if(++cou nt>LINES)
{
setcolor(color);
color=(color>=MAXCOLOR)?0:++color;
}
}
closegraph();
}
题目42 :学习static定义静态变量的用法
1•程序分析:
2.程序源代码:
III
# python没有这个功能了,只能这样了 :)
def varfu nc():
var=0
prin t 'var=%d' % var
var +=1
if name =='main ':
for i in ran ge(3):
varfunc()
# attribut of class
#作为类的一个属性吧
class Static:
StaticVar=5
def varfun c(self):
self.StaticVar +=1
print self.StaticVar
print Static.StaticVar
a=Static()
for i in ran ge(3):
a.varf un c()
III
题目43 :学习使用auto定义变量的用法
1•程序分析:
2.程序源代码:
没有auto关键字,使用变量作用域来举例吧
num=2 def autof un c():
num=1
prin t 'i nternal block num=%d' % num
num +=1
for i in ran ge(3):
prin t 'The num=%d' % num
num +=1
autof un c()
题目44 :学习使用static的另一用法。
1•程序分析:
2.程序源代码:
有一个static变量的用法,python是没有,演示一个python作用域使用方法
class Num:
nNu m=1 def in c(self):
self. nNum +=1
prin t ' nNu m=%d' % self. nNum
if name =='main ':
nNum=2
in st=Num()
for i in ran ge(3):
nNum +=1
prin t 'The num=%d' % nNum
in st.i nc()
题目45 :学习使用external的用法
1•程序分析:
2.程序源代码:
external.py 代码:
import exter nal if n ame =='ma in print external.add(10,20)
题目46 :学习使用register定义变量的方法。
1•程序分析:
2.程序源代码:
没有register关键字,用整型变量代替
tmp=0
for i in ran ge(1,101):
tmp +=i
prin t 'The sum is %d' % tmp
题目47 :宏#define命令练习⑴
1•程序分析:
2.程序源代码:
没有C语言的宏,就这么写了
TRUE=1
FALSE=O
def SQ(x):
return x * x
prin t 'Program will stop if in put value less tha n 50.'
aga in=1
while aga in:
num=in t(raw_i nput('Please in put nu mber'))
prin t 'The square for this nu mber is %d' % (SQ( num))
if num >=50:
agai n=TRUE
else:
aga in=FALSE
III
题目48 :宏#define命令练习(2)
1•程序分析:
2.程序源代码:
#i nclude "stdio.h"
#defi ne excha nge(a,b) { int t;
t=a;
a=b;
b=t;
}'
这个宏定义python不支持
def excha nge(a,b):
a,b=b,a
return (a,b)
if name =='main ':
x=10
y=20
prin t 'x=%d,y=%d' % (x,y)
x,y=excha nge(x,y)
prin t 'x=%d,y=%d' % (x,y)
题目49 :宏#define命令练习 ⑶
1•程序分析:
2.程序源代码:
#defi ne LAG >
#defi ne SMA <
#defi ne EQ==
#in elude "stdio.h" void mai n()
{
int i=10;
int j=20;
if(i LAG j)
printf("40: %d larger than %d n",i,j);
else if(i EQ j)
prin tf("40: %d equal to %d n ",i,j);
else if(i SMA j)
prin tf("40:%d smaller than %d n ",i,j);
else
prin tf("40: No such value. n");
}
不知道如何用python实现类似的功能
if name =='main ':
i=10
j=20
if i > j:
prin t '%d larger than %d' % (i,j)
elif i==j:
prin t '%d equal to %d' % (i,j)
elif i < j:
prin t '%d smaller than %d' % (i,j) else:
prin t 'No such value'
III
题目50 : #if #ifdef和#ifndef的综合应用。
1.程序分析:
2.程序源代码:
#i nclude "stdio.h"
#defi ne MAX
#defi ne MAXIMUM(x,y) (x>y)?x:y
#defi ne MINIMUM(x,y) (x>y)?y:x
void mai n()
{
int a=10,b=20;
#ifdef MAX
prin tf("40: The larger o ne is %dn ”,MAXIMUM(a,b));
#else
printf("40: The lower one is %dn",MINIMUM(a,b));
#en dif
#ifndef MIN
printf("40: The lower one is %dn",MINIMUM(a,b));
#else
prin tf("40: The larger o ne is %dn ”,MAXIMUM(a,b));
#en dif
#u ndef MAX #ifdef MAX prin tf("40: The larger o ne is %dn ”,MAXIMUM(a,b));
#else
printf("40: The lower one is %dn",MINIMUM(a,b));
#en dif
#defi ne MIN
#ifndef MIN
printf("40: The lower one is %dn",MINIMUM(a,b));
#else
prin tf("40: The larger o ne is %dn ”,MAXIMUM(a,b));
#en dif
}
这个还是预处理的用法,python不支持这样的机制,演示lambda的使用
III
MAXIMUM=lambda x,y : (x > y) * x + (x < y) * y
MINIMUM=lambda x,y : (x > y) * y + (x < y) * x
if name =='main ':
a=10
b=20
prin t 'The largar o ne is %d' % MAXIMUM(a,b)
prin t 'The lower one is %d' % MINIMUM(a,b)
III
题目51 :学习使用按位与&。
1•程序分析:0&0=0; 0&仁 0; 1 &0=0; 1 &1=1 2.程序源代码:
Ill
if n ame =='ma in a=077
b=a & 3
prin t 'a & b=%d' % b
b &=7
prin t 'a & b=%d' % b
题目52 :学习使用按位或|。
1•程序分析:0|0=0; 0|1= 1; 1|0=1; 1|1=1
2.程序源代码: if n ame =='ma in a=077 b=a | 3
prin t 'a | b is %d' % b b |=7
prin t 'a | b is %d' % b
题目53 :学习使用按位异或A。
1•程序分析:0八0=0; 0A1=1; 1A0=1; 1A1=0
2.程序源代码: if n ame =='ma in
a=077 b=a A 3
prin t 'The a a 3=%d' % b
b a=7
prin t 'The a a b=%d' % b
III
题目54 :取一个整数a从右端开始的4〜7位。
程序分析:可以这样考虑:
⑴先使a右移4位。
⑵设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
III
if name =='main ':
a=i nt(raw_i nput(' in put a nu mber: n'))
b=a >> 4
c=〜(〜0 << 4)
d=b & c
prin t '%ot%o' %(a,d)
III
题目55 :学习使用按位取反〜。
1.程序分析:~0=1; ~仁0;
2.程序源代码:
如何查看复数的16进制数
if n ame =='ma in a=234
b=〜a
prin t 'The a's 1 compleme nt is %d' % b a=〜a
prin t 'The a's 2 compleme nt is %d' % a
III
题目56 :画图,学用circle画圆形。
1•程序分析:
2.程序源代码:
#in clude "graphics.h"
mai n()
{
int driver,mode,i;
float j=1,k=1;
driver=VGA;mode=VGAHI;
in itgraph(& driver,&m ode,"");
setbkcolor(YELLOW);
for(i=0;i<=25;i++)
{
setcolor(8);
circle(310,250,k);
k=k+j;
j=j+0.3; if name =='main ':
from Tkin ter import *
canv as=Ca nv as(width=800, height=600, bg='yellow')
canv as.pack(expa nd=YES, fill=BOTH)
k=1
j=1
for i in ran ge(0,26):
canv as.create_oval(310 - k,250 - k,310 + k,250 + k, width=1) k +=j
j +=0.3
main loop()
题目57 :画图,学用line画直线
1•程序分析:
2.程序源代码: if n ame =='ma in from Tkin ter import *
canv as=Ca nv as(width=300, height=300, bg='gree n')
canv as.pack(expa nd=YES, fill=BOTH) x0=263
y0=263
y1=275 x仁 275
for i in ran ge(19):
canvas.create」ne(x0,y0,x0,y1, width=1, fill='red')
x0=x0 - 5
y0=y0 - 5
x1=x1 + 5
y1=y1 + 5
x0=263
y1=275
y0=263
for i in ran ge(21):
canv as.create_li ne(x0,y0,x0,y1,fill='red')
x0 +=5
y0 +=5
y1 +=5
main loop()
题目58 :画图,学用rectangle画方形。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位
2.程序源代码:
if n ame =='ma in
from Tkin ter import
root=Tk() root.title('Ca nvas')
canv as=Ca nv as(root,width=400,height=400,bg='yellow') x0=263
y0=263
y1=275
x仁 275
for i in ran ge(19):
canv as.create_recta ngle(x0,y0,x1,y1)
x0 -=5
y0 -=5
x1 +=5
y1 +=5
canv as.pack()
root.ma inlo op()
题目59 :画图,综合例子。
1•程序分析:
2.程序源代码: if name =='main ': from Tkin ter import canv as=Ca nv as(width=300,height=300,bg='gree n') ca nvas.pack(expa nd=YES,fill=BOTH)
x0=150
yO=1OO
canv as.create_oval(x0 - 10,y0 - 10,x0 + 10,y0 + 10) canv as.create_oval(x0 - 20,y0 - 20,x0 + 20,y0 + 20) canv as.create_oval(x0 - 50,y0 - 50,x0 + 50,y0 + 50) import math
B=0.809
for i in ran ge(16):
a=2 * math.pi / 16 * i
x=math.ceil(x0 + 48 * math.cos(a))
y=math.ceil(y0 + 48 * math.s in (a) * B)
canv as.create_li ne(x0,y0,x,y,fill='red')
canv as.create_oval(x0 - 60,y0 - 60,x0 + 60,y0 + 60)
for k in ran ge(501):
for i in ran ge(17):
a=(2 * math.pi / 16) * i + (2 * math.pi / 180) * k
x=math.ceil(x0 + 48 * math.cos(a))
y=math.ceil(y0 + 48 + math.s in (a) * B)
canv as.create_li ne(x0,y0,x,y,fill='red')
for j in ran ge(51):
a=(2 * math.pi / 16) * i + (2* math.pi / 180) * k - 1
x=math.ceil(x0 + 48 * math.cos(a)) y=math.ceil(y0 + 48 * math.sin(a) * B) canv as.create」ne(xO,yO,x,y,fill='red')
main loop()
III
<<100道Python编程题及答案(二)
100道Python编程题及答案(四)
10行如下图)
题目61:打印出杨辉三角形(要求打印出
1•程序分析: if name =='main ':
a=[]
for i in ran ge(10):
a.appe nd([])
for j in ran ge(10):
a[i].appe nd(0)
for i in ran ge(10):
a[i][0]=1
a[i][i]=1
for i in ran ge(2,10):
for j in ran ge(1,i):
a[i][j]=a[i - 1][j-1] + a[i - 1][j]
from sys import stdout
for i in ran ge(10):
for j in ran ge(i + 1):
stdout.write(a[i][j])
stdout.write('')
III
题目62 :学习putpixel画点。
1•程序分析:
2.程序源代码:
#i nclude "stdio.h"
#in elude "graphics.h"
mai n()
{
int i,j,driver=VGA,mode=VGAHI;
in itgraph(& driver,&m ode,"");
setbkcolor(YELLOW);
for(i=50;i<=230;i+=20) for(j=50;jv=230;j++) putpixel(i,j,1);
for(j=50;j<=230;j+=20)
for(i=50;i<=230;i++) putpixel(i,j,1);
}
III
III
题目63 :画椭圆ellipse
1•程序分析:
2.程序源代码:
III
if name =='main ':
from Tkin ter import *
x=360
y=160
top=y - 30
bottom=y - 30
canv as=Ca nv as(width=400,height=600,bg='white')
for i in ran ge(20):
can vas.create_oval(250 - top,250 - bottom,250 + top,250 + bottom)
top -=5
bottom +=5
canv as.pack()
main loop()
题目 :利用 ellipse and rectangle 画图。
1•程序分析:
2.程序源代码: if name =='main ':
from Tkin ter import *
canv as=Ca nv as(width=400,height=600,bg='white') left=20 right=50
top=50
num=15
for i in ran ge( nu m):
canvas.create_oval(250 - right,250 - left,250 + right,250 + left)
can vas.create_oval(250 - 20,250 - top,250 + 20,250 + top)
canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2)) right +=5
left +=5
top +=10
canv as.pack()
main loop()
题目65 :一个最优美的图案
1•程序分析:
2.程序源代码: import math class PTS: def init (self): self.x=0 self.y=0 poin ts=[] def Lin eToDemo(): from Tkin ter import *
scree nx=400
scree ny=400
canv as=Ca nv as(width=scree nx,height=scree ny ,bg='white')
AspectRatio=0.85
MAXPTS=15
h=scree ny
w=scree nx
xce nter=w / 2
yce nter=h / 2
radius=(h - 30) / (AspectRatio * 2) - 20
step=360 / MAXPTS
an gle=0.0
for i in ran ge(MAXPTS):
rads=angle * math.pi / 180.0
p=PTS()
p.x=xce nter + in t(math.cos(rads) * radius)
p.y=yce nter - in t(math.s in( rads) * radius * AspectRatio)
an gle +=step
poin ts.appe nd(p)
canv as.create_oval(xce nter - radius,yce nter - radius,
xce nter + radius,yce nter + radius)
for i in ran ge(MAXPTS): for j in ran ge(i,MAXPTS): canv as.create_li ne(poi nts[i].x,poi nts[i].y,poi nts[j].x,poi nts[j].y)
canv as.pack()
main loop()
if name =='main ':
Li neToDemo()
题目66 :输入3个数a,b,c,按大小顺序输出
1.程序分析:利用指针方法。
2.程序源代码:
if name =='main ':
n仁in t(raw_i nput(' n仁:n'))
n 2=i nt(raw_i nput(' n2=: n'))
n 3=i nt(raw_i nput(' n3=: n'))
def swap(p1,p2):
return p2,p1
if n1 > n2 : n1, n2=swap(n1,n2)
if n1 > n3 : n1, n3=swap(n1,n3)
if n2 > n3 : n2,n3=swap(n2,n3)
print n1, n2,n3
题目67:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组
1.程序分析:谭浩强的书中答案有问题。
2.程序源代码:
Ill
def inp(nu mbers):
for i in ran ge(9):
nu mbers.appe nd(i nt(raw_i nput('i nput a nu mber: n'))) nu mbers.appe nd(i nt(raw_i nput('i nput a nu mber: n'))) p=0
def max_m in( array):
max=min=0
for i in range(1,len(array) - 1):
p=i
if array[p] > array[max] : max=p
elif array[p] < array[min] : min=p
k=max
l=min
array[0],array[l]=array[l],array[0] array[9],array[k]=array[k],array[9] def outp( nu mbers):
for i in ran ge(le n(nu mbers)):
print nu mbers[i]
if name =='main ':
array=[]
in p(array)
max_mi n(array) outp(array)
Ill
题目68 :有n个整数,使其前面各数顺序向后移 m个位置,最后m个数变成最前面的 m个数
1•程序分析:
2.程序源代码: if name =='main ':
n=in t(raw_i nput('the total nu mber is:n'))
m=i nt(raw_i nput('back m:n'))
def move(arra y,n, m):
array_e nd=arra y[n - 1]
for i in ran ge( n - 1,-1,- 1):
array[i]=array[i - 1]
array[O]=array_e nd
m -=1
if m > 0:move(array ,n,m)
nu mber=[]
for i in ran ge( n):
nu mber.appe nd(i nt(raw_i nput(' in put a nu mber: n')))
prin t 'orig nal nu mber:', nu mber
move( nu mber, n,m)
prin t 'after moved:' ,nu mber
III
题目69 :有n个人围成一圈,顺序排号。从第一个人开始报数(从 1到3报数),凡报 到3的人退出
圈子,问最后留下的是原来第几号的那位。
1.程序分析:
2.程序源代码:
III
if name =='main ':
nm ax=50
n=in t(raw_i nput('please in put the total of nu mbers:')) num=[]
for i in ran ge( n):
nu m.appe nd(i + 1)
i=0
k=0
m=0
while m < n - 1:
if num[i] !=0 : k +=1
if k==3:
nu m[i]=0
k=0
m +=0
i +=1
if i==n : i=0
i=0
while nu m[i]==0: i +=1 print nu m[i]
Ill
题目70 :写一个函数,求一个字符串的长度,在 main函数中输入字符串,并输出其长 度。
1•程序分析:
2.程序源代码
就这样吧
if n ame =='ma in s=raw_i nput('please in put a stri ng:n')
prin t 'the stri ng has %d characters.' % len(s)
题目71 :编写input()和output()函数输入,输出5个学生的数据记录
1•程序分析:
2.程序源代码:
使用list来模拟结构(不使用class)
stu=[stri ng,stri ng,list]
N=3
#stu
#num : stri ng
#n ame : stri ng
#score[4]: list
stude nt=[]
stude nt. appe nd(['','',[]])
def in put_stu(stu):
for i in ran ge(N):
stu[i][O]=raw_i nput(' in put stude nt num:n') stu[i][1]=raw_i nput(' in put stude nt n ame:n') for j in ran ge(3):
stu[i][2].appe nd(i nt(raw_i nput('score:n')))
def output_stu(stu):
for i in ran ge(N):
prin t '%-6s%-10s' % ( stu[i][0],stu[i][1])
for j in ran ge(3):
prin t '%-8d' % stu[i][2][j]
if name =='main ':
in put_stu(stude nt)
print stude nt
output_stu(stude nt)
III
题目72 :创建一个链表。
1.程序分析:
2.程序源代码:
if n ame =='ma in ptr=[]
num=in t(raw_i nput('please in put a nu mber: n'))
ptr.appe nd(num)
print ptr
III
题目73 :反向输出一个链表。
1•程序分析:
2.程序源代码:
III
if name =='main ':
ptr=[]
for i in ran ge(5):
num=in t(raw_i nput('please in put a nu mber: n'))
ptr.appe nd(num)
print ptr
ptr.reverse()
print ptr
III
100道Python编程题及答案(五)
题目81 : 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数, 请问该数是多少?
import math
num=1
while True:
if math.sqrt( num + 100)-i nt(math.sqrt( num + 100))==0 and math.sqrt( num + 268)-
in t(math.sqrt( num + 268))==0:
print(num)
break
num +=1
题目82 :输入某年某月某日,判断这一天是这一年的第几天?
import datetime
import time
dtstr=str(raw_i nput('E nter the datetime:(20151215):'))
dt=datetime.datetime.strptime(dtstr, "%Y%m%d")
an other_dtstr=dtstr[:4] +'0101'
ano ther_dt=datetime.datetime.strptime(a no ther_dtstr, "%Y%m%d")
print (in t((dt-a no ther_dt).days) + 1)
题目83 :有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
a=1
b=1
for i in ran ge(1,21,2):
prin t '%d %d'%(a,b),
a +=b
b +=a
题目84 :判断101-200之间有多少个素数,并输出所有素数。
#!/usr/b in/pyth on
#-- cod in g:utf-8 -
from math import sqrt
def main():
for i in ran ge(101,201):
flag=1
k=i nt(sqrt(i))
for j in ran ge(2,k+1):
if i%j==0:
flag=0
break
if flag==1:
prin t '%5d'%(i),
if name =="main ":
mai n()
题目85 :打印出所有的 水仙花数”所谓 水仙花数”是指一个三位数,其各位数字立方和 等于该数本身。例如:153是一个 水仙花数”,因为153=1的三次方+ 5的三次方+ 3的 三次方。
#!/usr/b in/pyth on
#-- cod in g:utf-8 -
def main():
for i in ran ge(100,1000):
a=i%10
b=i/100
c=(i nt(i/10))%10
if i==a3+b3+c**3:
prin t "%5d"%(i),
if name =="main ": mai n() 题目 :某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的, 加密规则如下:
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二 位和第三位交换。
1•程序分析:
2.程序源代码: from sys import stdout
if name =='main ':
a=i nt(raw_i nput(' in put a nu mber: n'))
aa=[]
aa.appe nd(a % 10)
aa.append(a % 100 / 10)
aa. appe nd(a % 1000 / 100)
aa. appe nd(a / 1000)
for i in ran ge(4):
aa[i] +=5
aa[i] %=10
for i in ran ge(2):
aa[i],aa[3 - i]=aa[3 - i],aa[i]
for i in ran ge(3,-1,-1):
stdout.write(aa[i])
题目90 :专升本一题,读结果
1•程序分析:
2.程序源代码:
III
if name =='main ':
M=5
a=[1,2,3,4,5]
i=0
j=M - 1
while i < M:
a[i],a[j]=a[j],a[i]
print a
i +=1
j -=1
for i in ran ge(5):
print a[i]
III
题目91 :时间函数举例1
1•程序分析:
2.程序源代码: if n ame =='ma in import time
print time.ctime(time.time())
print time.asctime(time」o caltime(time.time())) print time.asctime(time.gmtime(time.time()))
Ill
题目92 :时间函数举例2
1•程序分析:
2.程序源代码: if name =='main ': import time start=time.time() for i in ran ge(3000): print i en d=time.time() print end - start
题目93 :时间函数举例3
1•程序分析:
2.程序源代码: if n ame =='ma in import time start=time.clock() for i in ran ge(10000): print i
en d=time.clock() print 'different is %6.3f % (end - start)
Ill
(版主初学时编的)
题目94 :时间函数举例4,一个猜数游戏,判断一个人反应快慢。
1•程序分析:
2.程序源代码: if name =='main ':
import time
import ran dom
play_it=raw_i nput('do you want to play it.('y' or ' n')') while play_it=='y':
c=raw_i nput('i nput a character: n')
i=ra ndom.ra ndi nt(0,2**32) % 100
prin t 'please in put nu mber you guess:n'
start=time.clock()
a=time.time()
guess=in t(raw_i nput('i nput your guess:n'))
while guess !=i:
if guess > i:
prin t 'please in put a little smaller'
guess=in t(raw_i nput('i nput your guess:n'))
else:
prin t 'please in put a little bigger'
guess=in t(raw_i nput('i nput your guess:n')) en d=time.clock()
b=time.time()
var=(e nd - start) / 18.2
print var
# prin t 'It took you %6.3 seco nds' % time.difftime(b,a))
if var < 15:
prin t 'you are very clever!'
elif var < 25:
prin t 'you are no rmal!'
else:
prin t 'you are stupid!'
prin t 'C on gradulati ons'
prin t 'The nu mber you guess is %d' % i
play_it=raw_i nput('do you want to play it.')
Ill
题目96 :计算字符串中子串出现的次数
1.程序分析:
2.程序源代码:
if n ame =='ma in str1=raw_i nput(' in put a stri ng:n') str2=raw_i nput(' in put a sub stri ng:n') ncoun t=str1.co un t(str2) print ncount
题目97 :从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个 #为止。
1•程序分析:
2.程序源代码:
III
if name =='main ':
from sys import stdout
file name=raw_i nput('i nput a file n ame:n')
fp=ope n(filen ame,"w")
ch=raw_i nput('i nput stri ng:n')
while ch !='#':
fp.write(ch)
stdout.write(ch)
ch=raw_i nput(”)
fp.close()
III
题目98 :从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁 盘文件“tes中保存。
输入的字符串以!结束。
1•程序分析:
2.程序源代码:
III
if name =='main ':
fp=ope n('test.txt','w')
stri ng=raw_i nput('please in put a stri ng:n')
stri ng=stri ng.upper()
fp.write(stri ng)
fp=ope n('test.txt','r')
print fp.read()
fp.close()
III
题目99:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母 顺序排列),
输出到一个新文件C中.
1•程序分析:
2.程序源代码: if name =='main ': import stri ng fp=ope n('JCP099.py') a=fp.read() fp.close()
fp=ope n('JCP098.py') b=fp.read()
fp.close() fp=ope n('C.txt','w') l=list(a + b)
l.sort()
s=''
s=s.jo in (I) fp.write(s)
fp.close()
题目100 :企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10% ;
利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部 分,
可可提成7.5% ; 20万到40万之间时,高于20万元的部分,可提成5% ;
40万到60万之间时高于40万元的部分,可提成3% ; 60万到100万之间时,
高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成, 从键盘输入当月利润I,求应发放奖金总数?
IIHH
i=i nt(raw_i nput('E nter the profit:'))
arr=[1000000,600000,400000,200000,100000,0]
rat=[0.01,0.015,0.03,0.05,0.075,0.1]
r=0
for idx in ran ge(0,6):
if i>arr[idx]:
r+=(i-arr[idx])*rat[idx]
print (i-arr[idx])*rat[idx]
i=arr[idx] print r
