Python 重寫C語言程序設計百例,每篇5題,每題分別用C語言和Python實現,方便對比。
C 語言代碼
#include <stdio.h>
int main()
{
for (int x = 1; x <= 4; ++x)
for (int y = 1; y <= 4; ++y)
for (int z = 1; z <= 4; ++z)
{
if (x != y && x != z && y != z)
{
printf("%d%d%d ", x, y, z);
}
}
return 0;
}
Python 代碼
m = [1, 2, 3, 4]
for x in m:
for y in m:
for z in m:
if (x != y != z) and (x != z):
print('{0}{1}{2}'.format(x, y, z), end=' ')
或
m = [1, 2, 3, 4]
x = [str(x)+str(y)+str(z) for x in m for y in m for z in m
if x != y != z and x != z]
for k in x:
print(int(k), end=' ')
點評:Python中不等式可以連寫,這樣可簡化代碼。利用列表推導式也是簡化代碼的一個手段。於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可可提
成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於
40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於
100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?
C 語言代碼
#include <stdio.h>
int main()
{
double i;
double bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
scanf("%lf",&i);
bonus1=100000*0.1;
bonus2=bonus1+100000*0.075;
bonus4=bonus2+200000*0.05;
bonus6=bonus4+200000*0.03;
bonus10=bonus6+400000*0.015;
if(i<=100000)
bonus=i*0.1;
else if(i<=200000)
bonus=bonus1+(i-100000)*0.075;
else if(i<=400000)
bonus=bonus2+(i-200000)*0.05;
else if(i<=600000)
bonus=bonus4+(i-400000)*0.03;
else if(i<=1000000)
bonus=bonus6+(i-600000)*0.015;
else
bonus=bonus10+(i-1000000)*0.01;
printf("bonus=%lf",bonus);
return 0;
}
Python 代碼
def fun(profit = 0):
# 獎金
bonus = 0
bonus10 = 100000 * 0.1
bonus20 = bonus10 + (200000 - 100000) * 0.075
bonus40 = bonus20 + (400000 - 200000) * 0.05
bonus60 = bonus40 + (600000 - 400000) * 0.03
bonus100 = bonus60 + (1000000 - 600000) * 0.015
if profit <= 100000:
bonus = profit * 0.1
elif profit <= 200000:
bonus = bonus10 + (profit - 100000) * 0.075
elif profit <= 400000:
bonus = bonus20 + (profit - 200000) * 0.05
elif profit <= 600000:
bonus = bonus40 + (profit - 400000) * 0.03
elif profit <= 1000000:
bonus = bonus60 + (profit - 600000) * 0.015
else:
bonus = bonus100 + (profit - 1000000) * 0.01
return bonus
profit = input('profit = ')
print('bonus = ', fun(profit))
C語言代碼
#include <stdio.h>
#include <math.h>
int main()
{
int a = 0, b = 0;
for (int i = 0; i < 10000; ++i)
{
a = sqrt(i + 100);
b = sqrt(i + 268);
if ((a*a == i+100) && (b*b == i+268))
{
printf("%d\n", i);
}
}
return 0;
}
Python 代碼
for x in range(100000):
if int(sqrt(x+100))**2 == x + 100 and int(sqrt(x+268))**2 == x + 268:
print(x)
題目:輸入年月日,判斷這一天是這一年的第幾天?
C 語言代碼
// 輸入: 年-月-日
// 返回:輸入日期距離當年1月1日的天數
int whichDay(int year, int month, int day)
{
// 每月對應的天數
static int M[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 存儲當前日期距離當年年1月1日的天數
int count = 0;
// 如果當年是閏年,設置二月爲29天
if ((year % 4 == 0) ||
((year % 100 == 0) && (year % 400 == 0)))
M[1] = 29;
// 檢查日期是否合法, 如果不合法,返回-1
if (month < 1 || month > 12)
return -1;
if (day < 1 || day > M[month-1])
return -1;
// 計算
for (int i = 0; i < month - 1; ++i)
{
count += M[i];
}
count += day;
return count;
}
Python 代碼
def whichDay(year, month, day):
M = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
count = 0
# 判斷閏年,如果時閏年,設置二月爲29天
if (year % 4 == 0) or (year % 100 == 0 and year % 400 == 0):
M[1] = 29
# 檢查日期合法性
if not (year >= 1 and 1 <= month <= 12 and 1 <= day <= M[month-1]):
return -1
# 計算
for m in M[:month-1]:
count += m
count += day
return count
【05】比較三個數的大小
C語言代碼
#define SWAP(px, py) (*px = *px + *py, \
*py = *px - *py, \
*px = *px - *py)
void fun(int x, int y, int z)
{
if (x > y)
SWAP(&x, &y);
if (y > z)
SWAP(&y, &z);
if (x > y)
SWAP(&x, &y);
printf("%d < %d < %d\n", x, y, z);
}
Python 代碼
def fun(x, y, z):
if x > y:
x,y = y,x
if y > z:
y,z = z,y
if x > y:
x,y = y,x
print('%d < %d < %d' % (x, y, z))
# 利用 Python 內置的排序函數
def fun2(*values):
newValues = sorted(values)
print(newValues)
# 利用 Python 內置的排序函數
def fun2(*values):
newValues = sorted(values)
print(newValues)
點評: fun() 中交換 x 、y 的值很自然,這是Python語言的特點,要多加利用。fun2() 利用了 Python 的內置排序函數,實際開發時比較有用。