Python 练习实例4(Python 100例)
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:
程序代码:
i = int(raw_input('yuar:'))
j = int(raw_input('month:'))
k = int(raw_input('day:'))
arr = [31,28,31,30,31,30,31,31,30,31,30,31]
r = 0
for idx in range(0,13):
if j-1>idx:
r+=(arr[idx])
if (j>2)and((i%4==0)and(i%100!=0)or(i%400==0)):
r=r+1
print r+k
Python 练习实例5(Python 100例)
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
程序代码:
方法一:
x=int(raw_input('x:'))
y=int(raw_input('y:'))
z=int(raw_input('z:'))
if y>x:
min=x
max=y
elif y min=y y=x y=min if z>y: print (x,y,z) elif z print (z,x,y) else: print (x,z,y) 方法二: l = [] for i in range(3): x = int(raw_input('integer:\n')) l.append(x) l.sort() print l 【程序32】题目:Press any key to change color, do you want to try it. Please hurry up! (c语言经典编程实例100题) 程序代码: 方法一: #include #include #include int textbackground(short iColor) { HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(hd, &csbInfo); return SetConsoleTextAttribute(hd, (iColor<<4)|(csbInfo.wAttributes&~0xF0)); } void main(void) { int color; for(color=0;color<16;color++) { textbackground(color);//设置文本的背景颜色 cprintf("this is color%d\r\n",color); cprintf("press any key to continue\r\n"); getch();//输入的字符看不见 } } 方法二: #include #include #include #include int main() { printf("This is color %d\r\n", 0); printf("Press any key to continue\r\n"); system("color 1f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 1); printf("Press any key to continue\r\n"); system("color 2f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 2); printf("Press any key to continue\r\n"); system("color 3f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 3); printf("Press any key to continue\r\n"); system("color 4f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 4); printf("Press any key to continue\r\n"); system("color 5f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 5); printf("Press any key to continue\r\n"); system("color 6f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 6); printf("Press any key to continue\r\n"); system("color 7f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 7); printf("Press any key to continue\r\n"); system("color 8f"); system ("pause"); system("cls"); getch(); printf("This is color %d\r\n", 8); printf("Press any key to continue\r\n"); system("color 9f"); system ("pause"); system("cls"); return 0; }