哈工大C語言程序設計精髓第七週

由於這些代碼也是我初學時寫的代碼,故其中的規範程度及簡潔程度並不很好(此處我後來寫的有可以參考一下->C語言代碼規範),但是能很好的接近出初學者的水平,也更有參考價值!排版不易,喜歡就點個贊吧!如有問題,請勿吐槽,歡迎留言互相學習。

第7周編程題在線測試

  1. n層嵌套平方根的計算
    題目內容

    編寫程序利用遞歸法實現如下所示n層嵌套平方根的計算:
    遞歸函數原型:double Y(double x, int n);
    程序運行結果示例1:
    Please input x and n:16,1↙
    Result=4.00
    程序運行結果示例2:
    Please input x and n:16,2↙
    Result=4.47
    程序運行結果示例3:
    Please input x and n:16,3↙
    Result=4.52
    程序運行結果示例4:
    Please input x and n:16,0↙
    Result=0.00
    輸入提示信息:“Please input x and n:”
    輸入格式: “%lf,%d”
    輸出格式: “Result=%.2f\n”

代碼實現

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double Y(double x, int n);
int main()
{
    double x;
    int n;
    printf("Please input x and n:");
    scanf("%lf,%d",&x,&n);
    printf("Result=%.2f\n",Y(x,n));
    return 0;
}
double Y(double x, int n)
{
    double y;
    for(y=0;n>0;n--)
    {
        y+=x;
        y=sqrt(y);
    }
    return y;
}
  1. 遞歸法求和
    題目內容

    用遞歸方法計算如下求和計算
    sum = 1 + 2 + 3 + … + n
    遞歸函數原型:int Sum(int n);
    程序運行結果示例1:
    Please input n:5↙
    sum=15
    程序運行結果示例2:
    Please input n:0↙
    data error!
    程序運行結果示例3:
    Please input n:-3↙
    data error!
    輸入提示信息:“Please input n:”
    輸入錯誤提示信息:“data error!\n”
    輸入格式:"%d"
    輸出格式:“sum=%d\n”

代碼實現

#include <stdio.h>
#include <stdlib.h>
int Sum(int n);
int main()
{
    int n;
    printf("Please input n:");
    scanf("%d",&n);
    if(n>0)
    {
        printf("sum=%d\n",Sum(n));
    }
    else
    {
        printf("data error!\n");
    }
    return 0;
}
int Sum(int n)
{
    int a;
    for(a=0;n>0;n--)
    {
        a+=n;
    }
    return a;
}
  1. 猴子喫桃程序_擴展3
    題目內容

    猴子第一天摘了若干個桃子,吃了一半,不過癮,又多吃了1個。第二天早上將剩餘的桃子又喫掉一半,並且又多吃了1個。此後每天都是喫掉前一天剩下的一半零一個。到第n天再想喫時,發現只剩下1個桃子,問第一天它摘了多少桃子?爲了加強交互性,由用戶輸入天數n,即假設第n天的桃子數爲1。
    要求採用遞歸法求解。
    遞歸函數原型:int Monkey(int n, int x);
    函數功能:從第n天只剩下一個桃子反向逆推出第1天的桃子數
    程序運行結果示例1:
    Input days n:5↙
    x=46
    程序運行結果示例2:
    Input days n:10↙
    x=1534
    輸入提示信息:“Input days n:”
    輸入格式: “%d”
    輸出格式:“x=%d\n”

代碼實現

#include <stdio.h>
#include <stdlib.h>
int Monkey(int n, int x);
int main()
{
    int n,a;
    printf("Input days n:");
    scanf("%d",&n);
    printf("x=%d\n",Monkey(n,a));
    return 0;
}
int Monkey(int n, int x)
{
    int a;
    for(a=1;n>1;n--)
    {
        a=(a+1)*2;
    }
    return a;
}
  1. 網購打折商品V2.0
    題目內容

    某網上購物網站對用戶實行優惠,買家購物貨款p越多,則折扣越多。
    標準如下:
    p<100元 沒有折扣
    100元≤p<200元 5%折扣
    200元≤p<500元 8%折扣
    500元≤p<1000元 10%折扣
    1000元≤p 15%折扣
    【提示】:從題意可以看出,折扣的變化是有規律的。當購物金額達到“100元”的2倍、5倍、10倍時,折扣值就會發生變化。假如一個變量c代表100的倍數,則當c<1時,無折扣;當1≤c<2時,折扣d=5%;當2≤c<5時,折扣d=8%;當5≤c<10時,折扣d=10%;當10≤c時,折扣d=15%。
    注:程序中與價格相關的數據類型爲float
    程序運行結果示例1:
    Input payment:90↙
    price = 90.0
    程序運行結果示例2:
    Input payment:100↙
    price = 95.0
    程序運行結果示例3:
    Input payment:300↙
    price = 276.0
    程序運行結果示例4:
    Input payment:1000↙
    price = 850.0
    程序運行結果示例5:
    Input payment:650.5↙
    price = 585.5
    輸入提示:“Input payment:”
    輸入格式: “%f”
    輸出格式:“price = %.1f\n” (注:等號左右均有空格)

代碼實現

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float p;
    printf("Input payment:");
    scanf("%f",&p);
    if(p<100)
    {
        printf("price = %.1f\n",p);
    }
    else if(p<200)
    {
        p*=0.95;
        printf("price = %.1f\n",p);
    }
    else if(p<500)
    {
        p*=0.92;
        printf("price = %.1f\n",p);
    }
    else if(p<1000)
    {
        p*=0.90;
        printf("price = %.1f\n",p);
    }
    else
    {
        p*=0.85;
        printf("price = %.1f\n",p);
    }
    return 0;
}

練兵區——編程題

  1. 諧均值計算
    題目內容

    兩數值的諧均值可以這樣計算:首先對兩數值的倒數取平均值,最後再取倒數。編寫一個帶有兩個double參數的函數,計算這兩個參數的諧均值。函數原型爲:
    double Calculate(double x,double y);
    程序運行結果示例1:
    Input two doubles:
    3 4↙
    1/((1/x+1/y)/2) = 3.429
    程序運行結果示例2:
    Input two doubles:
    6.5 3.8↙
    1/((1/x+1/y)/2) = 4.796
    輸入提示信息:“Input two doubles:\n”
    輸入格式: “%lf%lf”
    輸出格式:“1/((1/x+1/y)/2) = %0.3f\n” (注意:等號的兩邊各有一個空格)

代碼實現

#include <stdio.h>
#include <stdlib.h>
double Calculate(double x,double y);
int main()
{
    double a,b;
    printf("Input two doubles:\n");
    scanf("%lf%lf",&a,&b);
    printf("1/((1/x+1/y)/2) = %0.3f\n",Calculate(a,b));
    return 0;
}
double Calculate(double x,double y)
{
    return 1/((1/x+1/y)/2);
}
  1. 輸出指定行列數的字符
    題目內容

    編寫一個函數,函數原型:void Chline(char ch, int column, int row);
    該函數的3個參數是一個字符和兩個整數。字符參數是需要輸出的字符。第一個整數說明了在每行中該字符輸出的個數,而第二個整數指的是需要輸出的行數。編寫一個調用該函數的程序。
    程序運行結果示例1:
    input a char:
    k↙
    input column and row:
    2 3↙
    kk
    kk
    kk
    程序運行結果示例2:
    input a char:
    a↙
    input column and row:
    3 2↙
    aaa
    aaa
    字符輸入提示信息:“input a char:\n”
    行列數輸入提示信息:“input column and row:\n”
    輸入格式:
    “%c”
    “%d%d”
    輸出格式:"%c"

代碼實現

#include <stdio.h>
#include <stdlib.h>
void Chline(char ch, int column, int row);
int main()
{
    char a;
    int b,c,i;
    printf("input a char:\n");
    scanf("%c",&a);
    printf("input column and row:\n");
    scanf("%d%d",&b,&c);
    for(i=0;i<c;i++)
    {
        Chline(a,b,c);
        printf("\n");
    }
    return 0;
}
void Chline(char ch, int column, int row)
{
    int d;
    for(d=0;d<column;d++)
    {
        printf("%c",ch);
    }
}
  1. 魔術師猜數
    題目內容

    在一種室內互動遊戲中,魔術師要每位觀衆心裏想一個三位數abc(a、b、c分別是百位、十位和個位數字),然後魔術師讓觀衆心中記下acb、bac、bca、cab、cba五個數以及這5個數的和值。只要觀衆說出這個和是多少,則魔術師一定能猜出觀衆心裏想的原數abc是多少。例如,觀衆甲說他計算的和值是1999,則魔術師立即說出他想的數是443,而觀衆乙說他計算的和值是1998,則魔術師說:“你算錯了!”。請編程模擬這個數字魔術遊戲。要求用函數實現,函數原型爲:int Magic(int m);
    其中形參m代表觀衆計算的和值。
    輸入格式:"%d"
    輸出格式:
    觀衆計算錯誤,魔術師給出的結論:“The sum you calculated is wrong!\n”
    觀衆計算正確,魔術師給出的結論:“The number is %d\n”
    輸入樣例1:
    1998↙
    輸出樣例1:
    The_sum_you_calculated_is_wrong!
    輸入樣例2:
    1999↙
    輸出樣例2:
    The_number_is_443

代碼實現

#include <stdio.h>
#include <stdlib.h>
int Magic(int m);
int main()
{
    int a;
    scanf("%d",&a);
    if(Magic(a)==0)
    {
        printf("The sum you calculated is wrong!\n");
    }
    else
    {
        printf("The number is %d\n",Magic(a));
    }
    return 0;
}
int Magic(int m)
{
    int a,b,c,d;
    for(d=100;d<=999;d++)
    {
        a = d/100,b = (d%100)/10,c = d%10;
    if((a+b+b+c+c)*100+(a+a+b+c+c)*10+a+a+b+b+c==m)
    {
        return d;
    }
    else
        continue;
    }
    return 0;
}
  1. 計算禮炮聲響次數
    題目內容

    在海軍節開幕式上,有A、B、C三艘軍艦要同時開始鳴放禮炮各21響。已知A艦每隔5秒放1次,B艦每隔6秒放1次,C艦每隔7秒放1次。假設各炮手對時間的掌握非常準確,請編程計算觀衆總共可以聽到幾次禮炮聲。
    **輸入格式:**無
    輸出格式:“n=%d”

代碼實現

#include <stdio.h>
#include <string.h>
int main()
{
    int s,n,i,t=21;
    for(s=0,i=1;i<=21;i++,t++)
    {
        s+=6;
        if(s%5==0 && s<=105)
        {
            t--;
        }
    }
    for(s=0,i=1;i<=21;i++,t++)
    {
        s+=7;
        if(s%5==0 && s%6==0 && s<=105)
        {
            t--;
        }
        else if((s%5==0 && s<=105)||(s%6==0 && s<=126))
        {
            t--;
        }
    }
    printf("n=%d",t);
    return 0;
}
  1. 水手分椰子
    題目內容

    n(1<n<=5)個水手在島上發現一堆椰子,先由第1個水手把椰子分爲等量的n堆,還剩下1個給了猴子,自己藏起1堆。然後,第2個水手把剩下的n-1堆混合後重新分爲等量的n堆,還剩下1個給了猴子,自己藏起1堆。以後第3、4個水手依次按此方法處理。最後,第n個水手把剩下的椰子分爲等量的n堆後,同樣剩下1個給了猴子。請用迭代法編程計算並輸出原來這堆椰子至少有多少個,n的值要求從鍵盤輸入。若輸入的n值超出要求的範圍,程序輸出"Error!"。
    **提示:**分成的等量的堆數應該與水手的數量一致.
    程序運行結果示例1:
    Input n(1<n<=5):
    5↙
    y=3121
    程序運行結果示例2:
    Input n(1<n<=5):
    7↙
    Error!
    輸入提示信息: “Input n(1<n<=5):\n”
    輸入格式: “%d”
    輸出格式:“y=%d\n”
    輸入錯誤提示信息:“Error!\n”

代碼實現

#include <stdio.h>
int divide(int n, int m);
static int people;
int main()
{
    int i;
    int n;
    printf("Input n(1<n<=5):\n");
    scanf("%d", &n);
    people = n;
    if (n <= 1 || n > 5)
    {
        printf("Error!\n");
    }
    else
    {
        for (i = 1; ; i++)
        {
            if (divide(i, n))
            {
                printf("y=%d\n", i);
                break;
            }
        }
    }
    return 0;
}
int divide(int n, int m)
{
    if (n / people == 0 || n % people != 1)
        return 0;
    if (m == 1)
        return 1;
    return divide(n - n / people - 1, m - 1);
}
  1. 遞歸法計算遊戲人員的年齡
    題目內容

    有n個人圍坐在一起,問第n個人多大年紀,他說比第n-1個人大2歲;問第n-1個人,他說比第n-2個人大2歲,…,問第3個人,他說比第2個人大2歲;問第2個人,他說比第1個人大2歲。第1個人說自己10歲,問第n個人多大年紀。
    遞歸函數原型:unsigned int ComputeAge(unsigned int n);
    提示:
    計算年齡的遞歸公式爲:
    輸入格式: “%u”
    輸出格式: “The person’s age is %u\n”
    輸入樣例1:
    5↙
    輸出樣例1:
    The_person’s_age_is_18
    輸入樣例2:
    10↙
    輸出樣例2:
    The_person’s_age_is_28
    (注意:在輸出中,“_”代表空格,如果直接將上段示例粘貼到代碼中,應將其替換爲空格。)

代碼實現

#include <stdio.h>
#include <stdlib.h>
unsigned int ComputeAge(unsigned int n);
int main()
{
    int a;
    scanf("%u",&a);
    printf("The person's age is %u",ComputeAge(a));
    return 0;
}
unsigned int ComputeAge(unsigned int n)
{
    int a=10;
    for( ;n>1;n--)
    {
        a+=2;
    }
    return a;
}
  1. 遞歸法計算兩個數的最大公約數
    題目內容

    利用最大公約數的性質計算。對正整數a和b,當a>b時,若a中含有與b相同的公約數,則a中去掉b後剩餘的部分a-b中也應含有與b相同的公約數,對a-b和b計算公約數就相當於對a和b計算公約數。反覆使用最大公約數的上述性質,直到a和b相等爲止,這時,a或b就是它們的最大公約數。這三條性質,也可以表示爲:
    性質1 如果a>b,則a和b與a-b和b的最大公約數相同,即Gcd(a, b) = Gcd(a-b, b)
    性質2 如果b>a,則a和b與a和b-a的最大公約數相同,即Gcd(a, b) = Gcd(a, b-a)
    性質3 如果a=b,則a和b的最大公約數與a值和b值相同,即Gcd(a, b) = a = b
    程序運行結果示例1:
    Input a,b:16,24↙
    8
    程序運行結果示例2:
    Input a,b:-2,-8↙
    Input error!
    輸入提示信息:“Input a,b:”
    輸入錯誤提示信息:“Input error!\n”
    輸入格式:"%d,%d"
    輸出格式:"%d\n"

代碼實現

#include <stdio.h>
#include <stdlib.h>
int God(int a, int b);
int main()
{
    int a, b;
    printf("Input a,b:");
    scanf("%d,%d",&a,&b);
    if(a<=0 || b<=0)
    {
        printf("Input error!\n");
    }
    else
    {
        printf("%d\n",God(a,b));
    }
    return 0;
}
int God(int a, int b)
{
    if(a==b)
    {
        return a;
    }
    else if(a>b)
    {
        return God(a-b,b);
    }
    else
    {
        return God(a,b-a);
    }
}
  1. 尋找中位數v1.0
    題目內容

    編寫一個函數返回三個整數中的中間數。函數原型爲: int mid(int a, int b, int c);
    函數功能是返回a,b,c三數中大小位於中間的那個數。
    輸入格式: “%d%d%d”
    輸出格式:“The result is %d\n”
    輸入樣例1:
    12 6 18↙
    輸出樣例1:
    The_result_is_12
    輸入樣例2:
    -9 7 -2↙
    輸出樣例2:
    The_result_is_-2

代碼實現

#include <stdio.h>
#include <stdlib.h>
int mid(int a, int b, int c);
int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    printf("The result is %d\n",mid(a,b,c));
    return 0;
}
int mid(int a, int b, int c)
{
    if(a>b && a<c)
    {
        return a;
    }
    else if(b>a && b<c)
    {
        return b;
    }
    else
    {
        return c;
    }
}
  1. 還原算術表達式
    題目內容

    編寫程序求以下算式中XYZ的值,其中兩數XYZ與YZZ相加的和n(99<n<1000)的值要求從鍵盤輸入。
    程序運行結果示例1:
    Input n(n<1000):
    532↙
    X=3,Y=2,Z=1
    程序運行結果示例2:
    Input n(n<1000):
    977↙
    Invalid
    輸入提示:“Input n(n<1000):\n”
    輸入格式: “%d”
    輸出格式:“X=%d,Y=%d,Z=%d\n”
    計算不成功(無解)的輸出提示:“Invalid\n”

代碼實現

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x,y,z,d,n;
    printf("Input n(n<1000):\n");
    scanf("%d",&n);
    for(x=1;x<=9;x++)
    {
        for(y=1;y<=9;y++)
        {
            for(z=1;z<=9;z++)
            {
                d=x*100+y*100+y*10+z*10+z*2;
                if(d==n)
                {
                    printf("X=%d,Y=%d,Z=%d\n",x,y,z);
                    goto END;
                }
            }
        }
    }
    printf("Invalid\n");
    END: ;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章