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

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

第7周編程題在線測試

  1. 數字字符串轉換爲整型數
    題目內容

    從鍵盤輸入一串字符(假設字符數少於8個),以回車表示輸入結束,編程將其中的數字部分轉換爲整型數並以整型的形式輸出。
    函數原型爲 int Myatoi(char str[]);
    其中,形參數組str[]對應用戶輸入的字符串,函數返回值爲轉換後的整型數。
    解題思路的關鍵是:1)判斷字符串中的字符是否是數字字符;2)如何將數字字符轉換爲其對應的數字值;3)如何將每一個轉換後的數字值加起來形成一個整型數。
    程序運行結果示例1:
    Input a string:7hg09y↙
    709
    程序運行結果示例2:
    Input a string:9w2k7m0↙
    9270
    程序運行結果示例3:
    Input a string:happy↙
    0
    輸入提示信息:“Input a string:”
    輸入格式: “%7s”
    輸出格式:"%d\n"

代碼實現

#include <stdio.h>
#include <stdlib.h>
int Myatoi(char str[]);
int main()
{
    char a[8];
    int b;
    printf("Input a string:");
    scanf("%s",a);
    b=Myatoi(a);
    printf("%d",b);
    return 0;
}
int Myatoi(char str[])
{
    int i,u,sum=0;
    u=strlen(str);
    for(i=0;i<u;i++)
    {
        if(str[i]>=48 && str[i]<=57)
        {
            sum*=10;
            sum+=str[i];
            sum-=48;
        }
    }
    if(sum==0)
        return 0;
    else
        return sum;
}
  1. 查找子串
    題目內容

    用字符數組作函數參數,編程實現在從鍵盤輸入的字符串(假設長度小於80)中查找與指定的子串,並輸出該子串在字符串中首次出現的位置,如果該字符不存在,則輸出"Not found!"。
    函數原型:int SearchString(char s[], char d[])
    函數功能:在字符數組s中查找子串d,返回d在s中首次出現的位置,若找不到,則返回-1。
    程序運行結果示例1:
    Input a string:How are you!↙
    Input another string:are↙
    Searching results:5
    程序運行結果示例2:
    Input a string:hello↙
    Input another string:are↙
    Not found!
    程序運行結果示例3:
    Input a string:You are a student.↙
    Input another string:you↙
    Not found!
    輸入第一個字符串的提示信息:“Input a string:”
    輸入第二個字符串的提示信息:“Input another string:”
    輸入格式: gets()
    輸出格式:“Searching results:%d\n”
    沒找到子串的輸出提示信息: “Not found!\n”

代碼實現

#include <stdio.h>
#include <stdlib.h>
int SearchString(char s[], char d[]);
int main()
{
    char a[80],b[80];
    int c;
    printf("Input a string:");
    gets(a);
    printf("Input another string:");
    gets(b);
    c=SearchString(a,b);
    if(c==-1)
    {
        printf( "Not found!\n");
    }
    else
    {
        printf("Searching results:%d\n",c);
    }
    return 0;
}
int SearchString(char s[], char d[])
{
    int a,b,c,e,f,q;
    b=strlen(s);
    f=strlen(d);
    for(a=0;a<b;a++)
    {
            q=1;
        for(e=0,c=a;e<f;e++,c++)
        {
            if(s[c]!=d[e])
            {
                q=0;
            }            
        }
        if(q==1)
            return a+1;
    }
    return -1;
}
  1. 統計重複字符
    題目內容

    輸入一串字符(字符數小於80),以回車表示輸入結束,編程計算並輸出這串字符中連續重複次數最多的字符和重複次數。如果重複次數最多的字符有兩個,則輸出最後出現的那一個。
    已知函數原型
    //函數功能:統計字符串中連續重複次數最多的字符及其重複的次數
    //函數參數:str指向待統計的字符串,指針形參tag返回重複字符最後出現的下標位置
    //函數返回值:返回字符重複的次數
    int CountRepeatStr(char str[], int *tag);
    求解思路:設置一個計數器,遍歷字符串中的所有字符,若str[i] == str[i+1],則計數器加1,同時判斷計數器的值是否大於記錄的最大重複次數max,若大於,則用計數器的值更新max,並記錄該字符最後出現的位置i+1.若str[i] != str[i+1],則計數器重新初始化爲1。遍歷結束時,函數返回max的值。
    程序運行結果示例1:
    Input a string:
    2344455555↙
    5:5
    程序運行結果示例2:
    Input a string:
    sgf222257↙
    2:4
    輸入提示信息:“Input a string:\n”
    輸入格式: 用gets()輸入字符串
    輸出格式:"%c:%d\n"

代碼實現

#include <stdio.h>
#include <stdlib.h>
int CountRepeatStr(char str[], int *tag);
int main()
{
    char a[80];
    int i=0,t;
    printf("Input a string:\n");
    gets(a);
    t=CountRepeatStr(a,&i);
    printf("%c:%d\n",t,i);
    return 0;
}
int CountRepeatStr(char str[], int *tag)
{
    int i,u,max=0,m=0;
    u=strlen(str);
    for(i=0,*tag=1;i<u-1;i++)
    {
        if(str[i]==str[i+1])
        {
            *tag+=1;
            if(*tag>max)
            {
                max=*tag;
                m=str[i];
            }
            if(*tag==max)
            {
                m=str[i];
            }
        }
        else{*tag=1;}

    }
    *tag=max;
    return m;
}
  1. 凱撒密碼
    題目內容

    凱撒密碼是羅馬擴張時期朱利斯•凱撒(Julius Caesar)創造的,用於加密通過信使傳遞的作戰命令,其原理很簡單,就是通過將字母表中的字母移動一定位置而實現加密。例如,每個字母按字母表順序向後移3位,如a加密後變成d,b加密後變成e,……x加密後變成a,y加密後變成b,z加密後變成c。請編寫一個程序,將用戶從鍵盤輸入的文本字符串(只包含a~z的字符且長度小於100)進行加密後輸出。
    函數原型:void Caesar(char c[]);
    函數功能:計算凱撒密碼
    程序的運行結果示例1:
    Input a string:baidu↙
    edlgx
    程序的運行結果示例2:
    Input a string:xyz↙
    abc
    輸入提示信息:“Input a string:”
    輸入格式: 用 gets()函數
    輸出格式:用 puts()函數

代碼實現

#include <stdio.h>
#include <stdlib.h>
void Caesar(char c[]);
int main()
{
    char a[101];
    printf("Input a string:");
    gets(a);
    Caesar(a);
    return 0;
}
void Caesar(char c[])
{
    int i,n;
    n=strlen(c);
    for(i=0;i<n;i++)
    {
        if(c[i]=='x')
        {
            c[i]='a';
        }
        else if(c[i]=='y')
        {
            c[i]='b';
        }
        else if(c[i]=='z')
        {
            c[i]='c';
        }
        else
        {
            c[i]+=3;
        }
    }
    puts(c);
}

練兵區

  1. 有趣的“迴文”檢測
    題目內容

    英文中有很多的迴文詞,迴文詞的拼法十分有趣,無論是從前往後拼讀,還是從後往前拼讀,他們的拼法和詞義都不變。例如:dad(爸爸),mum(媽媽),noon(中午),eve(前夕),eye(眼睛),pop(流行),deed(行爲),level(水平)等。簡單地說,“迴文”就是指順讀和倒讀都一樣的字符串。現在請你編程輸入一個單詞,判斷它是否是迴文。
    提示
    (1)設置兩個指針pStart和pEnd,讓pStart指向字符串首部,讓pEnd指向字符串尾部。
    (2)利用循環從字符串兩邊對指針所指字符進行比較,當對應的兩字符相等且兩指針未超越對方時,使指針pStart向前移動一個字符位置(加1),使指針pEnd向後移動一個字符位置(減1),一旦發現兩字符不等或兩指針已互相超越(不可能是迴文),則立即停止循環。
    (3)根據退出循環時兩指針的位置,判斷字符串是否爲迴文。
    程序的兩次運行結果如下:
    第1次
    Input string:ABCCBA↙
    Yes!
    第2次
    Input string:student↙
    No!
    輸入提示信息:“Input string:”
    輸入格式: 用gets()函數
    輸出格式
    輸出信息,不是迴文:“No!\n”
    輸出信息,是迴文:“Yes!\n”

代碼實現

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

int main()
{
    char a[1000];
    int i,y,n,t=1;
    printf("Input string:");
    gets(a);
    n=strlen(a);
    i=0,y=n-1;
    for(;i<=n/2;i++,y--)
    {
        if(a[i]!=a[y])
        {
            t=0;
            break;
        }
    }
    if(t==0)
    {
        printf("No!\n");
    }
    else
    {
        printf("Yes!\n");
    }
    return 0;
}
  1. 學生成績管理系統V1.0
    題目內容

    某班有最多不超過30人(具體人數由鍵盤輸入)參加某門課程的考試,用一維數組作函數參數編程實現如下學生成績管理:
    (1)錄入每個學生的學號和考試成績;
    (2)計算課程的總分和平均分;
    (3)按成績由高到低排出名次表;
    (4)按學號由小到大排出成績表;
    (5)按學號查詢學生排名及其考試成績;
    (6)按優秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5個類別,統計每個類別的人數以及所佔的百分比;
    (7)輸出每個學生的學號、考試成績。
    程序運行結果示例:
    Input student number(n<30):
    6↙
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    1↙
    Input student’s ID, name and score:
    11003001 87↙
    11003005 98↙
    11003003 75↙
    11003002 48↙
    11003004 65↙
    11003006 100↙

    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    2↙
    sum=473,aver=78.83
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    3↙
    Sort in descending order by score:
    11003006 100
    11003005 98
    11003001 87
    11003003 75
    11003004 65
    11003002 48
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    4↙
    Sort in ascending order by number:
    11003001 87
    11003002 48
    11003003 75
    11003004 65
    11003005 98
    11003006 100
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    5↙
    Input the number you want to search:
    11003004
    11003004 65
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    6↙
    <60 1 16.67%
    60-69 1 16.67%
    70-79 1 16.67%
    80-89 1 16.67%
    90-99 1 16.67%
    100 1 16.67%
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    7↙
    11003001 87
    11003002 48
    11003003 75
    11003004 65
    11003005 98
    11003006 100
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    8↙
    Input error!
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    0↙
    End of program!
    輸入格式:
    ( 1 )錄入學生的人數:
    輸入數據格式:"%d"
    提示信息:“Input student number(n<30):\n”
    ( 2 )錄入每個學生的學號和考試成績:
    輸入數據格式:"%ld%f"
    提示信息:“Input student’s ID, name and score:\n”
    輸出格式
    菜單項的輸出顯示:
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of course
    3.Sort in descending order by score
    4.Sort in ascending order by number
    5.Search by number
    6.Statistic analysis
    7.List record
    0.Exit
    Please Input your choice:
    計算課程的總分和平均分:
    輸出總分與平均分格式:“sum=%.0f,aver=%.2f\n”
    按成績由高到低排出名次表:
    輸出格式:"%ld\t%.0f\n"
    提示信息:“Sort in descending order by score:\n”
    按學號由小到大排出成績表:
    輸出格式:"%ld\t%.0f\n"
    提示信息:“Sort in ascending order by number:\n”
    按學號查詢學生排名及其考試成績:
    如果未查到此學號的學生,提示信息:“Not found!\n”
    如果查詢到該學生,輸出格式:"%ld\t%.0f\n"
    按優秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5個類別,統計每個類別的人數以及所佔的百分比:
    成績<60輸出格式:"<60\t%d\t%.2f%%\n"
    成績=100輸出格式:"%d\t%d\t%.2f%%\n"
    其他輸出百分比格式:"%d-%d\t%d\t%.2f%%\n"

代碼實現

#include <stdio.h>
#include <stdlib.h>
void DD(int a,long *b,float *c);
void EE(int a,long *b,float *c);
void FF(int a,long *b,float *c);
void GG(int a,float *c);
int main()
{
    int n,i,u,a=1,max;
    long b[100];
    float c[100],sum=0;
    printf("Input student number(n<30):\n");
    scanf("%d",&n);
    while(1!=0)
    {
        printf("Management for Students' scores\n\
1.Input record\n\
2.Caculate total and average score of course\n\
3.Sort in descending order by score\n\
4.Sort in ascending order by number\n\
5.Search by number\n\
6.Statistic analysis\n\
7.List record\n\
0.Exit\n\
Please Input your choice:\n");
        scanf("%d",&a);
        if(a==1)
        {
            printf("Input student's ID, name and score:\n");
            for(u=0;u<n;u++)
            {
                scanf("%ld%f",&b[u],&c[u]);
            }
        }
        else if(a==2)
        {
            for(u=0;u<n;u++)
            {
                sum+=c[u];
            }
            printf("sum=%.0f,aver=%.2f\n",sum,sum/n);
        }
        else if(a==3)
        {
            printf("Sort in descending order by score:\n");
            DD(n,&b,&c);
        }
        else if(a==4)
        {
            printf("Sort in ascending order by number:\n");
            EE(n,&b,&c);
        }
        else if(a==5)
        {
            printf("Input the number you want to search:\n");
            FF(n,&b,&c);
        }
        else if(a==6)
        {
            GG(n,&c);
        }
        else if(a==7)
        {
            EE(n,&b,&c);
        }
        else if(a==0)
        {
            printf("End of program!\n");
            break;
        }
        else
        {
            printf("Input error!\n");
        }
    }
    return 0;
}
void DD(int a,long *b,float *c)
{
    int i,y,r;
    float t;
    for(i=0;i<a-1;i++)
    {
        for(y=i+1;y<a;y++)
        {
            if(*(c+i)<=*(c+y))
            {
                t=*(c+y),r=*(b+y);
                *(c+y)=*(c+i),*(b+y)=*(b+i);
                *(c+i)=t,*(b+i)=r;
            }
        }
    }
    for(i=0;i<a;i++)
    {
        printf("%ld\t%.0f\n",*(b+i),*(c+i));
    }
}
void EE(int a,long *b,float *c)
{
    int i,y,r;
    float t;
    for(i=0;i<a-1;i++)
    {
        for(y=i+1;y<a;y++)
        {
            if(*(b+i)<=*(b+y))
            {
                t=*(c+y),r=*(b+y);
                *(c+y)=*(c+i),*(b+y)=*(b+i);
                *(c+i)=t,*(b+i)=r;
            }
        }
    }
    for(i=a-1;i>=0;i--)
    {
        printf("%ld\t%.0f\n",*(b+i),*(c+i));
    }
}
void FF(int a,long *b,float *c)
{
    long int d,t=0,i;
    scanf("%ld",&d);
    for(i=0;i<a;i++)
    {
        if(*(b+i)==d)
        {
            printf("%ld\t%.0f\n",d,*(c+i));
            t=1;
            break;
        }
    }
    if(t==0)
    {
        printf("Not found!\n");
    }
}
void GG(int a,float *c)
{
    int i,q,w,e,r,t,y;
    q=w=e=r=t=y=0;
    for(i=0;i<a;i++)
    {
        if(*(c+i)<60)
        {
            q++;
        }
        else if(*(c+i)<70)
        {
            w++;
        }
        else if(*(c+i)<80)
        {
            e++;
        }
        else if(*(c+i)<90)
        {
            r++;
        }
        else if(*(c+i)<100)
        {
            t++;
        }
        else if(*(c+i)==100)
        {
            y++;
        }
    }
    printf("<60\t%d\t%.2f%%\n",q,(float)(100*q)/a);
    printf("60-69\t%d\t%.2f%%\n",w,(float)(100*w)/a);
    printf("70-79\t%d\t%.2f%%\n",e,(float)(100*e)/a);
    printf("80-89\t%d\t%.2f%%\n",r,(float)(100*r)/a);
    printf("90-99\t%d\t%.2f%%\n",t,(float)(100*t)/a);
    printf("100\t%d\t%.2f%%\n",y,(float)(100*y)/a);
}
  1. 程序改錯——1
    題目內容

    下面程序的功能是,從鍵盤輸入兩個字符串,分別存放在字符數組d和s中,通過調用子函數MyStrcat( )將這兩個字符串連接起來,並將連接後的字符串存放在字符數組r中,同時輸出連接後的字符串。已知每個字符數組的最大長度爲80。下面給出的程序存在錯誤,找到錯誤的原因後,請修改正確。並按照給出的程序運行結果示例檢查你的程序。
    已知函數原型:char* MyStrcat(char *dest, char *source);//函數返回連接後的字符串的首地址
#include <stdio.h>
#include <string.h>
int main(void)
{
        char *first, *second, *result;
        printf("Input the first string:\n");
        gets(first);
        printf("Input the second string:\n");
        gets(second);
        result = MyStrcat(first, second);
        printf("The result is : %s\n", result);
        return 0;
}
char* MyStrcat(char *dest, char *source)
{
        int i = 0;
        while (*(dest+i)!='\0')   i++;
        for (; *(source+i)!='\0'; i++)
        {
            *(dest+i) = *(source+i);   
        }
        return dest;
}
  1. 程序運行結果示例1:
    Input the first string:
    fri↙
    Input the second string:
    end↙
    The result is : friend
    程序運行結果示例2:
    Input the first string:
    home↙
    Input the second string:
    work↙
    The result is : homework
    輸入提示信息:“Input the first string:\n”
    輸入提示信息:“Input the second string:\n”
    輸入格式: 使用gets()函數
    輸出格式:“The result is : %s\n”

代碼實現

#include <stdio.h>
#include <string.h>
int main(void)
{
        char first[200], second[200];
        int i=0,y=0;
        printf("Input the first string:\n");
        gets(first);
        printf("Input the second string:\n");
        gets(second);
        while (*(first+i)!='\0')   i++;
        while (*(second+y)!='\0')   y++;
        for (y=0; *(second+y)!='\0'; y++)
        {
            *(first+i+y) = *(second+y);
        }
        *(first+i+y)=='\0';
        printf("The result is : %s\n", first);
        return 0;
}
  1. 程序改錯——2
    題目內容

    下面程序的功能是輸出如示例所示形式的數據,目前程序中存在錯誤,找到錯誤的原因後,請修改正確,並按照給出的程序運行結果示例檢查你的程序。
    程序中用到的函數原型如下:
    void YH(int a[][ARR_SIZE], int n);
    void PrintYH(int a[][ARR_SIZE], int n);
    程序運行結果示例:
    1
    1 1
    1 2 1
    1 3 3 1
#include<stdio.h>
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void YH(int a[][ARR_SIZE], int n)
{
        int  i, j ;
        for (i=1; i<=n; i++)
        {  
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<=n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }       
        }
}
void PrintYH(int a[][ARR_SIZE], int n)
{
        int i , j ;
        for (i=1; i<n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
            }
             printf("\n");
        }
  1. 輸入格式: 無
    輸出格式: “%4d”

代碼實現

#include<stdio.h>
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void  YH(int a[][ARR_SIZE], int  n)
{
        int  i, j ;
        for (i=1; i<n; i++)
        {
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
}
void  PrintYH(int a[][ARR_SIZE], int  n)
{
        int i , j ,t=1;
        for (i=1; i<n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
                if(j==n-1)
                {
                    t=0;
                }
            }
            if(t==1)
             {
                 printf("\n");
             }
        }
}
  1. 出售金魚
    題目內容

    買買提將養的一缸金魚分五次出售:第一次賣出全部的一半加二分之一條;第二次賣出餘下的三分之一加三分之一條;第三次賣出餘下的四分之一加四分之一條;第四次賣出餘下的五分之一加五分之一條;最後賣出剩下的11條。問原來魚缸中共有幾條魚?
    輸入格式: 無
    輸出格式:“There are %d fishes at first.\n”

代碼實現

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

int main()
{
    float s=11,i;
    for(i=4;i>0;i--)
    {
        s+=1/(i+1);
        s*=(i+1)/i;
    }
    printf("There are %d fishes at first.\n",(int)s);
    return 0;
}
  1. 找最值
    題目內容

    從鍵盤任意輸入10個整數,用指針變量作函數參數編程計算最大值和最小值,並返回它們所在數組中的位置。函數原型如下所示:
    int FindMax(int num[], int n, int *pMaxPos);//函數返回最大值,pMaxPos返回最大值所在的下標
    int FindMin(int num[], int n, int *pMinPos);//函數返回最小值,pMaxPos返回最小值所在的下標
    程序運行結果示例:
    Input 10 numbers:
    -1 2 3 45 92 8 9 12 7 8↙
    Max=92,Position=4,Min=-1,Position=0
    輸入提示信息:“Input 10 numbers:\n”
    輸入格式: “%d”
    輸出格式:“Max=%d,Position=%d,Min=%d,Position=%d\n”

代碼實現

#include <stdio.h>
#include <stdlib.h>
int FindMax(int num[], int n, int *pMaxPos);
int FindMin(int num[], int n, int *pMinPos);
int main()
{
    int a[10],i,b=0,c=0,w,e;
    printf("Input 10 numbers:\n");
    for(i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
    }
    w=FindMax(a,10,&b);
    e=FindMin(a,10,&c);
    printf("Max=%d,Position=%d,Min=%d,Position=%d\n",w,b,e,c);
    return 0;
}
int FindMax(int num[], int n, int *pMaxPos)
{
    int max=num[0],i;
    for(i=0;i<n-1;i++)
    {
        if(max<=num[i+1])
        {
            max=num[i+1];
            *pMaxPos=i+1;
        }
    }
    return max;
}
int FindMin(int num[], int n, int *pMinPos)
{
    int min=num[0],i;
    for(i=0;i<n-1;i++)
    {
        if(min>=num[i+1])
        {
            min=num[i+1];
            *pMinPos=i+1;
        }
    }
    return min;
}
  1. 楊輝三角形
    題目內容

    編程打印具有如下形式的楊輝三角形,其中輸出數據的行數n從鍵盤輸入,並且n<=10。
    程序運行結果示例1:
    Input n (n<=10):
    5↙
    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    程序運行結果示例2:
    Input n (n<=10):
    7↙
    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1
    1 6 15 20 15 6 1
    輸入提示信息:“Input n (n<=10):\n”
    輸入格式: “%d”
    輸出格式
    輸出數據格式:"%4d"
    數據換行: “\n”

代碼實現

#include<stdio.h>
void  YH(int a[][10], int  n);
void  PrintYH(int a[][10], int  n);
int main(void)
{
    int ARR_SIZE,a[10][10];
    printf("Input n (n<=10):\n");
    scanf("%d",&ARR_SIZE);
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void  YH(int a[][10], int  n)
{
        int  i, j ;
        for (i=1; i<=n; i++)
        {
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<=n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
}
void  PrintYH(int a[][10], int  n)
{
        int i , j ;
        for (i=1; i<=n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
            }
                 printf("\n");
        }
}
  1. 顛倒句子中的單詞順序
    題目內容

    從鍵盤輸入一個句子(假設字符數小於100個),句子中的單詞之間用空格分隔,句子必須以一個標點符號作爲結尾,句子開頭和末尾標點符號前均沒有空格,以回車表示輸入結束,請編程顛倒句中的單詞順序並輸出。
    函數原型:int Inverse(char str1[], char str2[][N])

    程序運行結果示例1:
    Input a sentence:you can cage a swallow can’t you?↙
    you can’t swallow a cage can you?
    程序運行結果示例2:
    Input a string:you are my sunshine!↙
    sunshine my are you!
    程序運行結果示例3:
    Input a sentence:I love you!↙
    you love I!
    輸入提示信息:“Input a sentence:”
    輸入格式: 用gets()函數
    輸出格式
    每個單詞的輸出格式:"%s " (注意: %s後面有一個空格)
    最後一個單詞和標點符號的輸出格式:"%s%c\n"

代碼實現

#include <stdio.h>
#include <stdlib.h>
int Inverse(char str1[], char str2[][100]);
int main()
{
    char str1[100],str2[100][100],a;
    int i;
    printf("Input a sentence:");
    gets(str1);
    a=str1[strlen(str1)-1];
    str1[strlen(str1)-1]='\0';
    i=Inverse(str1,str2);
    for(;i>0;i--)
    {
        printf("%s ",str2[i]);
    }
    printf("%s%c\n",str2[i],a);
    return 0;
}
int Inverse(char str1[], char str2[][100])
{
    int i=0,j=0,k=0;
    while(str1[i]!='\0')
    {
        k=0;
        while(str1[i]!=' '&&str1[i]!='\0')
        {
            str2[j][k]=str1[i];
            k++;
            i++;
        }
        if(str1[i]=='\0') break;
        str2[j][k]='\0';
        j++;
        i++;
    }
    return j;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章