經典C語言程序設計100例 -- C 和 Python 版 (01 - 05)

寫在開頭的話
Python 重寫C語言程序設計百例,每篇5題,每題分別用C語言和Python實現,方便對比。
    C語言編譯器:GCC 4.8.2
    Python版本:Python 3.4
寫這個系列是受這位博主(http://blog.csdn.net/berguiliu?viewmode=contents)的啓發,自己想重寫一遍,使用更加Python化的風格——簡潔。如有問題或疏漏,非常歡迎在評論中指出。

【01】各位互異的三位數
題目:輸出由數字{1, 2, 3, 4}組成的所有三位數,不能重複,每個數字最多隻能出現一次。
思路:採用窮舉法。從這4個數字中選擇3個數字進行全排列,然後過濾掉不符合條件的。
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中不等式可以連寫,這樣可簡化代碼。利用列表推導式也是簡化代碼的一個手段。

【02】if-else 練習
題目:企業發放的獎金根據利潤提成。利潤(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,求應發放獎金總數?
思路:主要時考察分支判斷結構的掌握。
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))

【03】完全平方數
題目:一個整數,它加上100後是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?
         (如果一個數的平方根的平方等於該數,這說明此數是完全平方數)
思路:採用窮舉法。
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)
點評:Python中 “**” 表示求冪級數

【04】閏年判斷
題目:輸入年月日,判斷這一天是這一年的第幾天?
思路:每月的天數相加,再加上當前的號數。關鍵點:閏年的二月有29天,如果輸入的月份大於兩個月時,需要判斷是否爲閏年來決定二月的天數。
閏年定義:公元年數可被4整除爲閏年,但是整百的年數必須是可以被400整除的纔是閏年。(四年一閏,百年不閏,四百年再閏)
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】比較三個數的大小
題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。
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 的內置排序函數,實際開發時比較有用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章