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

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

第6周編程題在線測試

  1. 計算階乘的和v2.0
    題目內容

      假設有這樣一個三位數m,其百位、十位和個位數字分別是a、b、c,如果m= a!+b!+c!,則這個三位數就稱爲三位階乘和數(約定0!=1)。請編程計算並輸出所有的三位階乘和數。
    函數原型: long Fact(int n);
    **函數功能:**計算n的階乘
    **輸入格式:**無
    輸出格式:"%d\n"

代碼實現:

#include <stdio.h>
#include <stdlib.h>
long Fact(int n);
int main()
{
    int a,b,c;
    int m;
    for(m = 100;m<=999;m++)
    {
        a = m/100,b = (m%100)/10,c = m%10;
    if(Fact(a)+Fact(b)+Fact(c)==m)
        printf("%d\n",m);
    }
    return 0;
}
long Fact(int n)
{
    if(n == 0)
        return 1;
    else
        return n*Fact(n-1);
}
  1. 計算最大的三位約數
    題目內容

      從鍵盤任意輸入一個數n(1000<=n<=1000000),編程計算並輸出n的所有約數中最大的三位數(即最大的三位約數)。如果n小於1000或者大於1000000,則輸出“Input error!”。
    函數原型: int Func(int n);
    函數功能:計算n的所有約數中最大的三位數
    程序運行結果示例1:
    Input n:555555↙
    777
    程序運行結果示例2:
    Input n:1000↙
    500
    程序運行結果示例3:
    Input n:800↙
    Input error!
    輸入提示信息:“Input n:”
    輸入錯誤提示信息:“Input error!\n”
    輸入格式: “%d”
    輸出格式: “%d\n”

代碼實現:

#include <stdio.h>
#include <stdlib.h>
int Func(int n);
int main()
{
    int n;
    printf("Input n:");
    scanf("%d",&n);
    if(n>1000000 || n<1000)
        printf("Input error!\n");
    else
    printf("%d\n",Func(n));
    return 0;
}
int Func(int n)
{
    int a;
    for(a=999;a>=100;a--)
    {
        if(n%a==0)
            return a;
    }
    return 1;
}
  1. 孔融分梨
    題目內容

      孔融沒有兄弟姐妹,到了週末,就找堂兄孔明、堂姐孔茹、堂弟孔偉等7個堂兄妹來到家裏玩。孔融媽媽買了8個梨給孩子們喫,結果小黃狗桐桐淘氣叼走了一個,大花貓鑫鑫偷偷藏了一個。孔融搶過剩下的6個梨,媽媽止住他,說他要和大家平分喫。孔融不高興,說8個人怎麼分6個梨?媽媽說可以用分數解決這個問題。孔融學過分數,說把每個梨切8個相等的塊,每個人拿6塊就行了。媽媽說不用切那麼多塊,每個梨切4個相等的塊,每個人拿3塊正好。孔融糊塗了。孔明說,我來教你。於是孔明給孔融講起了分數的化簡。
      分數化簡要化簡到最簡形式,比如12/20可以化簡成6/10和3/5,但3/5是最簡形式;100/8可以化簡成 50 /4和 25 /2 , 而25/2 爲最簡形式。爲了降低難度,不要求將假分數(如7/2)化簡成帶分數(3 )形式。請編程幫助孔融將任意一個分數化簡成最簡形式。先從鍵盤輸入兩個整數m和n(1<=m,n<=10000) ,其中m表示分子,n表示分母。然後輸出分數化簡後的最簡形式。
    函數原型:int Gcd(int a, int b);
    函數功能:計算a和b的最大公約數,輸入數據超出有效範圍時返回-1。
    程序的運行結果示例1:
    Input m,n:8,14↙
    4/7
    程序的運行結果示例2:
    Input m,n:-13,31↙
    Input error!
    程序的運行結果示例3:
    Input m,n:7,0↙
    Input error!
    程序的運行結果示例4:
    Input m,n:210,35↙
    6/1
    輸入提示信息: “Input m,n:”
    輸入錯誤提示信息: “Input error!\n”
    輸入格式:"%d,%d"
    輸出格式:"%d/%d\n"

代碼實現:

#include <stdio.h>
#include <stdlib.h>
int Gcd(int a, int b);
int main()
{
    int m,n;
    printf("Input m,n:");
    scanf("%d,%d",&m,&n);
    if(m<1 || n>10000 || n==0)
        printf("Input error!\n");
    else
        printf("%d/%d\n",m/Gcd(m,n),n/Gcd(m,n));
    return 0;
}
int Gcd(int a, int b)
{
    int c;
    while (b!=0)
    {
        c = a%b;
        a = b;
        b = c;
    }
    return a;
}
  1. 素數求和
    題目內容

      從鍵盤任意輸入一個整數n,編程計算並輸出1~n之間的所有素數之和。
    函數原型:int IsPrime(int x);
    函數功能:判斷x是否是素數,若函數返回0,則表示不是素數,若返回1,則代表是素數
    程序運行結果示例1:
    Input n:8↙
    sum=17
    程序運行結果示例2:
    Input n:10↙
    sum=17
    程序運行結果示例3:
    Input n:-12↙
    sum=0
    輸入提示信息:“Input n:”
    輸入格式:"%d"
    輸出格式:“sum=%d\n”

代碼實現:

#include<stdio.h>
#include<math.h>
int IsPrime(int x);
int main()
{
    int m,n,sum = 0;
    printf("Input n:");
    scanf("%d",&n);
    for(m = 2; m <=n; m++)
    {
        if(IsPrime(m))
        {            
          sum += m;
        }
    }
    printf("sum=%d\n",sum);
    return 0;
}

int IsPrime(int x)
{
    int i;
    for(i = 2; i <= sqrt(x); i++)
    {
        if(x%i == 0)return 0;
    }
    return 1;
}

第六週練兵區——編程題

  1. 繪製金字塔
    題目內容

    要求用戶從鍵盤輸入一個大寫字母,使用嵌套循環產生像下面這樣的金字塔圖案:
    ____A
    ___ABA
    __ABCBA
    _ABCDCBA
    程序運行結果示例1:
    Please input a capital:
    D↙
    ____A
    ___ABA
    __ABCBA
    _ABCDCBA
    程序運行結果示例2:
    Please input a capital:
    F↙
    ______A
    _____ABA
    ____ABCBA
    ___ABCDCBA
    __ABCDEDCBA
    ABCDEFEDCBA
    (說明:上面運行結果示例中,每行字母前面的下劃線"
    “代表屏幕上實際輸出的是空格,最後一行前面有一個空格,倒數第二行有兩個空格,以此類推。)
    輸入提示信息:“Please input a capital:\n”
    輸入格式: “%c”
    輸出格式:”%c"

代碼實現:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b,c,d;
    char q;
    printf("Please input a capital:\n");
    scanf("%c",&q);
    for(a=65;a<=q;a++)
    {
        for(b=q-a;b>=0;b--)
        {
            printf(" ");
        }
        for(c=65;c<=a-1;c++)
        {
            printf("%c",c);
        }
         printf("%c",a);
         for(d=a-1;d>=65;d--)
         {
             printf("%c",d);
         }
         printf("\n");
    }
    return 0;
}
  1. 循環嵌套的應用
    題目內容

    編寫程序產生如下輸出:
    F
    FE
    FED
    FEDC
    FEDCB
    FEDCBA
    輸入格式: 無
    輸出格式:"%c"

代碼實現:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char s=70;
    int a,b,c;
    for(a=65;a<=s;a++)
    {
        c=s;
        for(b=a-65;b>=0;b--)
        {
            printf("%c",c);
            c--;
        }
        printf("\n");
     }
	return 0;
}
  1. 利用泰勒級數計算sinx的值
    題目內容

      利用泰勒級數計算sinx的值,要求最後一項的絕對值小於,並統計出此時累加了多少項。請用“利用前項來計算後項”的方法計算累加項,不要使用pow函數編寫程序。程序中所有實數的數據類型都是double類型。
    程序的運行結果示例1:
    Input x:
    3↙
    sin(x)=0.141,count=9
    程序的運行結果示例2:
    Input x:
    10↙
    sin(x)=-0.544,count=18
    輸入提示信息:“Input x:\n”
    輸入格式: “%lf”
    輸出格式:“sin(x)=%.3f,count=%d\n”

代碼實現:

#include<stdio.h>
#include<math.h>
double fact(double n);
int main()
{
    int i = 1,sign = -1;
    double x,sum = 0,p,q;
    printf("Input x:\n");
    scanf("%lf",&x);
    q = x;
    do
    {
        sign = -sign;
        p = q*sign/fact(2*i - 1);
        sum += p;
        q = q*x*x;
        i++;
    }while(fabs(p) >= 1e-5);
    printf("sin(x)=%.3f,count=%d\n",(float)sum,i - 1);
    return 0;
}
double fact(double n)
{
    if(n == 0)return 1.0;
    else return n*fact(n - 1);
}
  1. 計算100~200之間的所有素數之和
    題目內容

    計算100~200之間的所有素數之和,判別一個數是否是素數請用給定的函數實現。
    函數原型:int fun(int m);
    說明
    參 數:m 是要進行判斷的數;
    返回值:若數 m 是素數,則返回值爲1;否則返回值爲0。
    輸入格式:
    輸出格式: “sum=%d\n”

代碼實現:

#include<stdio.h>
#include<math.h>
int fun(int m);
int main()
{
    int m,sum = 0;
    for(m = 100; m <=200; m++)
    {
        if(fun(m))
        {
          sum += m;
        }
    }
    printf("sum=%d\n",sum);
    return 0;
}

int fun(int m)
{
    int i;
    for(i = 2; i <= sqrt(m); i++)
    {
        if(m%i == 0)return 0;
    }
    return 1;
}
  1. 編程實現一個輸入指定範圍內的整數的函數
    題目內容

      編程實現一個輸入指定範圍內的整數的函數getint,其完整的函數原型爲:int getint(int min, int max);,它負責接收用戶的輸入進行驗證,保證接收的一定是一個介於min和max之間([min, max]區間內)的一個整數並最後返回該整數。如果用戶輸入不合法,則會提示繼續輸入,直到輸入合法時爲止。要求編寫完整的程序並測試你所寫的getint函數。
    程序的運行結果示例:
    Please enter min,max:
    3,100↙
    Please enter an integer [3…100]:
    -2↙
    Please enter an integer [3…100]:
    0↙
    Please enter an integer [3…100]:
    116↙
    Please enter an integer [3…100]:
    58↙
    The integer you have entered is:58
    輸入提示信息:“Please enter min,max:\n”
    “Please enter an integer [%d…%d]:\n”
    輸入格式:
    輸入數據區間的最小值和最大值:"%d,%d"
    輸入指定範圍內的整數: “%d”
    輸出格式:“The integer you have entered is:%d\n”

代碼實現:

#include <stdio.h>
#include <stdlib.h>
int getint(int min, int max);
int main()
{
    int min,max;
    printf("Please enter min,max:\n");
    scanf("%d,%d",&min,&max);
    printf("The integer you have entered is:%d\n",getint(min,max));
    return 0;
}
int getint(int min, int max)
{
    int a;
    do
    {
        printf( "Please enter an integer [%d..%d]:\n",min,max);
        scanf("%d",&a);
    }while(min>a || max<a);
    return a;
}
  1. 程序改錯v2.0
    題目內容

      下面代碼的功能是將百分制成績轉換爲5分製成績,具體功能是:如果用戶輸入的是非法字符或者不在合理區間內的數據(例如輸入的是a,或者102,或-45等),則程序輸出 Input error!,並允許用戶重新輸入,直到輸入合法數據爲止,並將其轉換爲5分制輸出。目前程序存在錯誤,請將其修改正確。並按照下面給出的運行示例檢查程序。
#include<stdio.h>
   int main()
   {
       int score;
       char grade;
       printf("Please input score:");
       scanf("%d", &score);
       if (score < 0 || score > 100)   
             printf("Input error!\n");
        else if (score >= 90) 
             grade = 'A’;
        else if (score >= 80)
             grade = 'B';   
        else if (score >= 70)
             grade = 'C';  
        else if (score >= 60)
             grade = 'D'; 
        else
             grade = 'E'; 
        printf("grade:%c\n", grade);
        return 0;
}
  1. 程序運行結果示例1:
    Please input score:
    a↙
    Input error!
    Please input score:
    -12↙
    Input error!
    Please input score:
    230↙
    Input error!
    Please input score:
    92↙
    grade: A
    程序運行結果示例2:
    Please input score:
    88↙
    grade: B
    程序運行結果示例3:
    Please input score:
    73↙
    grade: C
    程序運行結果示例4:
    Please input score:
    65↙
    grade: D
    程序運行結果示例5:
    Please input score:
    27↙
    grade: E
    輸入提示信息:“Please input score:\n”
    輸入格式: “%d”
    輸出格式
    輸入錯誤時的提示信息:“Input error!\n”
    輸出格式:“grade: %c\n” (注意:%c前面有一個空格)

代碼實現:

#include<stdio.h>
   int main()
   {
       int score=66,a;
       char grade;
       printf("Please input score:\n");
       a=scanf("%d", &score);
       while(a==0 || score>100 || score<0)
       {
           if(a==0)
           {
               getchar();            
           }
                printf("Input error!\n");
                printf("Please input score:\n");
                a=scanf("%d", &score);
        }
                switch(score/10)
                {
                case 10:
                case 9:
                    grade = 'A';
                    printf("grade: %c\n", grade);
                    break;
                case 8:
                    grade = 'B';
                    printf("grade: %c\n", grade);
                    break;
                case 7:
                    grade = 'C';
                    printf("grade: %c\n", grade);
                    break;
                case 6:
                    grade = 'D';
                    printf("grade: %c\n", grade);
                    break;
                case 5:
                case 4:
                case 3:
                case 2:
                case 1:
                case 0:
                    grade = 'E';
                    printf("grade: %c\n", grade);
                    break;
                }
        return 0;
}
  1. 編程計算a+aa+aaa+…+aa…a(n個a)的值
    題目內容

    編程計算 a+aa+aaa+…+aa…a(n個a)的值,n和a的值由鍵盤輸入。例如,當n=4,a=2,表示計算2+22+222+2222的值。
    程序運行結果示例:
    Input a,n:
    2,4↙
    sum=2468
    輸入提示信息:“Input a,n:\n”
    輸入格式: “%d,%d”(先輸入a,後輸入n)
    輸出格式: “sum=%ld\n”

代碼實現:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,n,d;
    long c = 0,sum = 0;
    printf("Input a,n:\n");
    scanf( "%d,%d", &a, &n);
        for(d = 1;d <= n; d++)
        {
            c = c*10 + a;
        sum += c;}
    printf("sum=%ld\n",sum);
    return 0;
}
  1. 搬磚問題
    題目內容

      n塊磚( 27<n<=77 ),36人搬,男搬4,女搬3,兩個小孩擡一塊磚,要求一次搬完,問男人、女人和小孩各需多少人?請用窮舉法編程求解,n的值要求從鍵盤輸入。輸出結果按照男人數量升序給出(見下面示例3)。
    程序的運行結果示例1:
    Input n(27<n<=77):
    28↙
    men=0,women=4,children=32
    程序的運行結果示例2:
    Input n(27<n<=77):
    36↙
    men=3,women=3,children=30
    程序的運行結果示例3:
    Input n(27<n<=77):
    60↙
    men=2,women=14,children=20
    men=7,women=7,children=22
    men=12,women=0,children=24
    輸入提示: “Input n(27<n<=77):\n”
    輸入格式: “%d”
    輸出格式:“men=%d,women=%d,children=%d\n”

代碼實現:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,a,b,c;
    printf("Input n(27<n<=77):\n");
    scanf("%d",&n);
    for (a=0;a<=n/4;a++)
        for (b=0;b<=n/3;b++)
           for (c=0;c<=n*2;c++)
            if(a+b+c==36 && c%2==0 && a*4+b*3+c/2==n)
               {printf("men=%d,women=%d,children=%d\n",a,b,c);}
    return 0;
}
  1. 編程輸出某年某月有多少天(考慮到閏年)
    題目內容

    從鍵盤輸入一個年份和月份,輸出該月有多少天(考慮閏年),用switch語句編程。
    程序運行結果示例1:
    Input year,month:
    2015,3↙
    31 days
    程序運行結果示例2:
    Input year,month:
    2015,4↙
    30 days
    程序運行結果示例3:
    Input year,month:
    2016,2↙
    29 days
    程序運行結果示例4:
    Input year,month:
    2014,2↙
    28 days
    程序運行結果示例5:
    Input year,month:
    2015,13↙
    Input error!
    輸入提示信息:“Input year,month:\n”
    輸入格式: “%d,%d”
    輸出格式:
    輸入錯誤時的提示信息:“Input error!\n”
    輸出格式:
    “31 days\n”
    “30 days\n”
    “29 days\n”
    “28 days\n”

代碼實現:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int year,month;
    printf("Input year,month:\n");
    scanf("%d,%d",&year,&month);
    if(month>12 || month<0)
    {
        printf("Input error!\n");
        goto END;
    }
    year = fabs(year);
    if((year%400==0)||(year%4==0 && year%100!=0))
    {
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    printf("31 days\n");
                    break;
            case 4:
            case 6:
            case 9:
            case 11:
                    printf( "30 days\n");
                    break;
            case 2:
                printf( "29 days\n");
                break;
        }
    }
    else
    {
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    printf("31 days\n");
                    break;
            case 4:
            case 6:
            case 9:
            case 11:
                    printf( "30 days\n");
                    break;
            case 2:
                printf( "28 days\n");
                break;
        }
    END: ;
    return 0;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章