哈工大C语言程序设计精髓第十一周

news/2024/7/11 1:14:53 标签: html, jquery, css

由于这些代码也是我初学时写的代码,故其中的规范程度及简洁程度并不很好(此处我后来写的有可以参考一下->C语言代码规范),但是能很好的接近出初学者的水平,也更有参考价值!排版不易,喜欢就点个赞吧!如有问题,请勿吐槽,欢迎留言互相学习。

第11周编程题在线测试

1. 山地训练


题目内容
为了能在下一次跑步比赛中有好的发挥,小白在一条山路上开始了她的跑步训练。她希望能在每次训练中跑得尽可能远,不过她也知道农场中的一条规定:女孩子独自进山的时间不得超过M秒(1 <= M <= 10,000,000)。假设整条山路划分成T个长度相同的路段(1 <= T <= 100,000),并且小白用si表示第i个路段的路况,用u、f、d这3个字母分别表示第i个路段是上坡、平地、下坡。小白跑完一段上坡路的耗时是U秒(1 <= U <= 100),跑完一段平地的耗时是F秒(1 <= F <= 100),跑完一段下坡路的耗时是D秒(1 <= D <= 100)。注意,沿山路原路返回时,原本是上坡的路段变成了下坡路段,原本是下坡的路段变成了上坡路段。小白想知道,在能按时返回农场的前提下,她最多能在这条山路上跑多少个路段。请你编程帮助她计算。
函数原型:long Fun(long M, long T, long U, long F, long D, char str[]);
函数功能:计算在限时M秒内T个路段的情况下,最多往返可跑的路段数。
参数:M,T,U,F,D分别代表限时、路段数,以及上坡、平地、下坡的耗时
数组str保存整条山路的路段状况
返回值:最多可跑的路段数
程序运行结果示例1:
Input M,T,U,F,D: 13 5 3 2 1↙
Input conditions of road: ufudf↙
num=3
程序运行结果示例2:
Input M,T,U,F,D: 4000 8 18 10 5↙
Input conditions of road: fuffdfud↙
num=7
进山时间等信息的输入提示: “Input M,T,U,F,D:”
路况输入提示信息: “Input conditions of road:”
进山时间等数据的输入格式: “%ld%ld%ld%ld%ld”
路况等数据的输入格式: “%s”
输出格式: “num=%ld\n”
这题有问题,应该是出题的错误

代码实现

#include <stdio.h>
#include <stdlib.h>
long Fun(long M, long T, long U, long F, long D, char str[]);
int main()
{
    long m,t,u,f,d,q;
    char str[1000];
    printf("Input M,T,U,F,D:");
    scanf("%ld%ld%ld%ld%ld",&m,&t,&u,&f,&d);
    printf("Input conditions of road:");
    scanf("%s",str);
    q=Fun(m,t,u,f,d,str);
    printf("num=%ld\n",q);
    return 0;
}
long Fun(long M, long T, long U, long F, long D, char str[])
{
    long s=0,i;
    for(i=0;i<T;i++)
    {
        if(str[i]=='f')
        {
            s+=F+F;
        }
        else
        {
            s+=D+U;
        }
        if(s>=M)
        {
            return i;
        }
    }
    return i-1;
}

2. 奇偶数分离


题目内容
输入n个整数(n从键盘输入,假设n的值不超过100),按奇偶数分成两组并输出。输出两行,第一行为所有奇数,第二行为所有偶数,保持数据的相对顺序与输入顺序相同。
函数原型如下所示:
void Seperate(int a[], int n); //数组a[]存放用户输入的n个整数
解题思路:用两个循环分别输出奇数和偶数,在输出第一个数时用"%d"格式字符,在输出其余数时用",%d"格式字符,用标志变量记录和判断是否是第一个奇数或偶数。
程序运行结果示例1:
Input n: 7↙
Input numbers: 5 9 47 82 0 6 7↙
5,9,47,7
82,0,6
程序运行结果示例2:
Input n: 8↙
Input numbers: -2 3 5 0 23 62 79 83↙
3,5,23,79,83
-2,0,62
输入提示信息:“Input n:”
“Input numbers:”
输入格式: “%d”
每行第一个数据的输出格式:"%d"
每行第二个及以后数据的输出格式:",%d"

代码实现

#include <stdio.h>
#include <stdlib.h>
void Seperate(int a[], int n);
int main()
{
    int n,a[100],i;
    printf("Input n:");
    scanf("%d",&n);
    printf("Input numbers:");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    Seperate(a,n);
    return 0;
}
void Seperate(int a[], int n)
{
    int b[100],c[100],i,y,t;
    for(y=i=t=0;y<n;y++)
    {
        if(a[y]%2==0)
        {
            b[i]=a[y];
            i++;
        }
        else
        {
            c[t]=a[y];
            t++;
        }
    }
    printf("%d",c[0]);
    for(y=1;y<t;y++)
    {
        printf(",%d",c[y]);
    }
    printf("\n%d",b[0]);
    for(y=1;y<i;y++)
    {
        printf(",%d",b[y]);
    }
}

3. 子串判断


题目内容
从键盘输入两个长度小于80的字符串A和B,且A的长度大于B的长度,编程判断B是不是A的子串,如果是,则输出”Yes”,否则输出”No”。这里所谓的该串的子串是指字符串中任意多个连续的字符组成的子序列。
函数原型:int IsSubString(char a[], char b[]);
函数功能:判断b是否是a的子串,是则返回1,否则返回0
程序运行结果示例1:
Input the first string: Abcdefghijk123↙
Input the second string: 123↙
Yes
程序运行结果示例2:
Input the first string: abefsfl↙
Input the second string: befs↙
Yes
程序运行结果示例3:
Input the first string: aAbde↙
Input the second string: abc↙
No
输入第一个字符串的提示信息: “Input the first string:”
输入第二个字符串的提示信息: “Input the second string:”
输入格式: 用 gets()函数
输出格式
是子串,输出: “Yes\n”
不是子串,输出: “No\n”

代码实现

#include <stdio.h>
#include <stdlib.h>
int SearchString(char s[], char d[]);
int main()
{
    char a[80],b[80];
    int c;
    printf("Input the first string:");
    gets(a);
    printf("Input the second string:");
    gets(b);
    c=SearchString(a,b);
    if(c==0)
    {
        printf( "No\n");
    }
    else
    {
        printf("Yes\n");
    }
    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 1;
    }
    return 0;
}

4. 星期查找


题目内容
任意输入英文的星期几,通过查找如图所示的星期表,输出其对应的数字,若查到表尾,仍未找到,则输出错误提示信息。
提示:用一个二维字符数组weekDay来存放如图所示的星期表的内容(字符串)。输入待查找的字符串,然后在星期表中顺序查找与输入字符串相匹配的字符串。找到的字符串在星期表数组中的第一维下标(行号)即为题目所求。
程序运行结果示例1:
Please enter a string:
Friday↙
Friday is 5
程序运行结果示例2:
Please enter a string:
Fruday↙
Not found!
输入提示信息:“Please enter a string:\n”
输入格式: 字符串输入采用gets()函数
输出格式
找到了,输出:"%s is %d\n"
没找到,输出:“Not found!\n”

代码实现

#include<stdio.h>
#include<string.h>
#define N 20
int main(void)
{
    char weekDay[7][N] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    char str[N];
    char (*p)[N] = weekDay;
    printf("Please enter a string:\n");
    gets(str);
    for(int i = 0; i < 7; i++)
    {
        if(strcmp(str,p + i) == 0)
        {
            printf("%s is %d\n",str,i);
            return 0;
        }
    }
    printf("Not found!\n");
    return 0;
}

练兵区——编程题

1. 找出按字典顺序排在最前面的国名


题目内容
输入5个国名,编程找出并输出按字典顺序排在最前面的国名。
提示:所谓字典顺序就是将字符串按由小到大的顺序排列,因此找出按字典顺序排在最前面的国名指的就是最小的字符串。
程序的运行结果示例:
Input five countries’ names:
America↙
China↙
Japan↙
England↙
Sweden↙

The minimum is:America
输入提示信息:“Input five countries’ names:\n”
输入格式: 国名输入用gets()函数
输出格式:“The minimum is:%s\n”

代码实现

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    char a[5][10],*min=a[0];
    int i;
    printf("Input five countries' names:\n");
    for(i=0;i<5;i++)
    {
        gets(a[i]);
    }
    for(i=1,*min=a[0];i<5;i++)
    {
        if(strcmp(min,a[i])>0)
        {
            strncpy(min,a[i],sizeof(a[i]));
        }
    }
    printf("The minimum is:%s\n", min);
    return 0;
}

2. 学生成绩管理系统V2.0


题目内容
某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,参考前面章节的“学生成绩管理系统V1.0”,用一维数组和函数指针作函数参数编程实现如下菜单驱动的学生成绩管理系统:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按成绩由低到高排出名次表;
(5)按学号由小到大排出成绩表;
(6)按学号查询学生排名及其考试成绩;
(7)按优秀(90 100)、良好(8089)、中等(70 79)、及格(6069)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;
(8)输出每个学生的学号、考试成绩。
要求程序运行后显示的菜单如下:
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please enter your choice:
然后,根据用户输入的选项执行相应的操作。
程序运行结果示例:
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
1↙
Input student’s ID 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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
4↙
Sort in ascending order by score:
11003002 48
11003004 65
11003003 75
11003001 87
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
5↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
6↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
7↙
<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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
8↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please Input your choice:
9↙
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 score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.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 and score:\n”
( 3 )录入待查询学生的学号:
输入数据格式为:"%ld"
输出格式
计算课程的总分和平均分:
输出总分与平均分格式为:“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 score:\n”
按学号由小到大排出成绩表:
输出格式为:"%ld\t%.0f\n"
提示信息为:“Sort in ascending order by number:\n”
按学号查询学生排名及其考试成绩:
**查询学号输入的提示信息:“Input the number you want to search:\n”
如果未查到此学号的学生,提示信息为:“Not found!\n”;
如果查询到该学生,要求输出格式为:"%ld\t%.0f\n"
按优秀(90 100)、良好(8089)、中等(70 79)、及格(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"
用户输入的菜单项超出0-8的选择范围,输出错误提示信息:“Input error!\n”

代码实现

#include <stdio.h>
#include <stdlib.h>
void DD(int a,long *b,float *c);
void DD1(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 score\n\
5.Sort in ascending order by number\n\
6.Search by number\n\
7.Statistic analysis\n\
8.List record\n\
0.Exit\n\
Please Input your choice:\n");
        scanf("%d",&a);
        if(a==1)
        {
            printf("Input student's ID 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 score:\n");
            DD1(n,&b,&c);
        }
        else if(a==5)
        {
            printf("Sort in ascending order by number:\n");
            EE(n,&b,&c);
        }
        else if(a==6)
        {
            printf("Input the number you want to search:\n");
            FF(n,&b,&c);
        }
        else if(a==7)
        {
            GG(n,&c);
        }
        else if(a==8)
        {
            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 DD1(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=a-1;i>=0;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);
}

3. 月份表示


题目内容
用指针数组保存表示每个月份的英文单词以及“Illegal month”的首地址,然后编程实现:从键盘任意输入一个数字表示月份值n,程序输出该月份的英文表示,若n不在1~12之间,则输出“Illegal month”。
程序的运行结果示例1:
Input month number:
3↙
month 3 is March
程序的运行结果示例2:
Input month number:
12↙
month 12 is December
程序的运行结果示例3:
Input month number:
14↙
Illegal month
月份输入提示信息:“Input month number:\n”
输入格式: “%d”
输出格式
月份正确时输出格式:“month %d is %s\n”
月份错误时输出格式:"%s\n"

代码实现

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    int n,i;
    char a[13][100]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    printf("Input month number:\n");
    scanf("%d",&n);
    if(n>12 || n<1)
    {
        printf("Illegal month");
    }
    else
    {
        printf("month %d is %s\n",n,a[n-1]);
    }
    return 0;
}

4. 程序改错——1


题目内容
从键盘任意输入m个学生n门课程的成绩,然后计算每个学生各门课的总分sum和平均分aver。下面程序存在极为隐蔽的错误,请分析错误的原因,并修改程序,同时按照给出的程序运行示例检查修改后的程序。

#include  <stdio.h>
#define STUD 30            //最多可能的学生人数
#define COURSE 5             //最多可能的考试科目数
void  Total(int *score, int sum[], float aver[], int m, int n);
void  Print(int *score, int sum[], float aver[], int m, int n);
int main(void)
{
         int     i, j, m, n, score[STUD][COURSE], sum[STUD];
         float   aver[STUD];
         printf("Enter the total number of students and courses:\n");
         scanf("%d%d",&m,&n);
         printf("Enter score:\n");
         for (i=0; i<m; i++)
         {
            for (j=0; j<n; j++)
            {
                scanf("%d", &score[i][j]);
            }
        }
        Total(*score, sum, aver, m, n);
        Print(*score, sum, aver, m, n);
        return 0;
}
 
void  Total(int *score, int sum[], float aver[], int m, int n)
{
        int  i, j;
        for (i=0; i<m; i++)
        {
            sum[i] = 0;
            for (j=0; j<n; j++)
            {
                sum[i] = sum[i] + *(score + i * n + j);
            }
            aver[i] = (float) sum[i] / n;
        }
}
 
void  Print(int *score, int sum[], float aver[], int m, int n)
{
        int  i, j;
        printf("Result:\n");
        for (i=0; i<m; i++)
        {
            for (j=0; j<n; j++)
            {
                printf("%4d\t", *(score + i * n + j));
            }
            printf("%5d\t%6.1f\n", sum[i], aver[i]);
     }
}
  1. 程序运行结果示例:
    Enter the total number of students and courses:
    2 3↙
    Enter score:
    90↙
    95↙
    97↙
    82↙
    73↙
    69↙

    Result:
    90 95 97 282 94.0
    82 73 69 224 74.7
    输入m个学生n门课程的提示信息:“Enter the total number of students and courses:\n”
    输入成绩的提示信息:“Enter score:\n”
    输入格式:
    输入m个学生n门课程: “%d%d”
    输入成绩: “%d”
    输出格式
    输出提示信息: “Result:\n”
    m个学生n门课程成绩输出格式:"%4d"
    总分和平均分输出格式:"%5d%6.1f\n"

代码实现

#include  <stdio.h>
#define STUD 30
#define COURSE 5
void  Total(int score[STUD][COURSE], int sum[], float aver[], int m, int n);
void  Print(int score[STUD][COURSE], int sum[], float aver[], int m, int n);
int main(void)
{
         int     i, j, m, n, score[STUD][COURSE], sum[STUD];
         float   aver[STUD];
         printf("Enter the total number of students and courses:\n");
         scanf("%d%d",&m,&n);
         printf("Enter score:\n");
         for (i=0; i<m; i++)
         {
            for (j=0; j<n; j++)
            {
                scanf("%d", &score[i][j]);
            }
        }
        Total(*score, sum, aver, m, n);
        Print(*score, sum, aver, m, n);
        return 0;
}

void  Total(int score[STUD][COURSE], int sum[], float aver[], int m, int n)
{
        int  i, j;
        for (i=0; i<m; i++)
        {
            sum[i] = 0;
            for (j=0; j<n; j++)
            {
                sum[i] = sum[i] + *(*(score+i)+j);
            }
            aver[i] = (float) sum[i] / n;
        }
}

void  Print(int score[STUD][COURSE], int sum[], float aver[], int m, int n)
{
        int  i, j;
        printf("Result:\n");
        for (i=0; i<m; i++)
        {
            for (j=0; j<n; j++)
            {
                printf("%4d", *(*(score+i)+j));
            }
            printf("%5d%6.1f\n", sum[i], aver[i]);
     }
}

5. 程序改错——2


题目内容
下面主函数调用函数SortString()按奥运会参赛国国名在字典中的顺序对其入场次序进行排序,目前程序存在错误,请修改正确,并按照给出的程序运行示例检查修改后的程序。

#include  <stdio.h>
#include  <string.h>
#define   M  150 /* 最多的字符串个数 */
#define   N  10 /* 字符串最大长度 */
void SortString(char *ptr[], int n);
int main()
{
  int    i, n; 
  char   *pStr[M];
  printf("How many countries?\n");
  scanf("%d",&n);
  getchar();        /* 读走输入缓冲区中的回车符 */
  printf("Input their names:\n");
  for (i=0; i<n; i++)  
  {
      gets(pStr[i]);  /* 输入n个字符串 */
  }
  SortString(pStr, n); /* 字符串按字典顺序排序 */
  printf("Sorted results:\n");
  for (i=0; i<n; i++)                    
  {
      puts(pStr[i]);  /* 输出排序后的n个字符串 */
  }
  return 0;
}
void SortString(char *ptr[], int n)
{
  int   i, j;
  char  *temp = NULL;
  for (i=0; i<n-1; i++)    
  {
      for (j=i+1; j<n; j++)
      {
         if (strcmp(ptr[j], ptr[i]) < 0)    
         {
              temp = ptr[i];
              ptr[i] = ptr[j];
              ptr[j] = temp;
         } 
      }   
  } 
}
  1. 程序运行结果示例:
    How many countries?
    5↙
    Input their names:
    China↙
    French↙
    America↙
    Russia↙
    German↙

    Sorted results:
    America
    China
    French
    German
    Russia
    输入国家数量提示信息:“How many countries?\n”
    输入国家名字提示信息:“Input their names:\n”
    输入格式:
    输入国家数量:"%d"
    字符串输入:使用gets()函数;
    输出提示信息:“Sorted results:\n”
    输出格式:使用puts()函数

代码实现

#include  <stdio.h>
#include  <string.h>
#define   M  150 
#define   N  10 
void SortString(char *ptr[], int n);
int main()
{
  int    i, n; 
  char   *pStr[M],str[M][M];
  for(i=0;i<M;i++)
  {
    pStr[i]=str[i];
  }
  printf("How many countries?\n");
  scanf("%d",&n);
  getchar();      
  printf("Input their names:\n");
  for (i=0; i<n; i++)  
  {
      gets(pStr[i]); 
  }
  SortString(pStr, n); 
  printf("Sorted results:\n");
  for (i=0; i<n; i++)                    
  {
      puts(pStr[i]);  
  }
  return 0;
}
void SortString(char *ptr[], int n)
{
  int   i, j;
  char  *temp = NULL;
  for (i=0; i<n-1; i++)    
  {
      for (j=i+1; j<n; j++)
      {
         if (strcmp(ptr[j], ptr[i]) < 0)    
         {
              temp = ptr[i];
              ptr[i] = ptr[j];
              ptr[j] = temp;
         } 
      }   
  } 
}

6. 找数组最值


题目内容
按如下函数原型编程从键盘输入一个m行n列的二维数组,然后计算数组中元素的最大值及其所在的行列下标值。其中,m和n的值由用户键盘输入。已知m和n的值都不超过10。
void InputArray(int *p, int m, int n);
int FindMax(int *p, int m, int n, int pRow, int pCol);//函数返回最大值,pRow和pCol分别返回最大值所在的行列下标
例如,程序的1次运行结果如下:
Input n:
3,4↙
Input 3
4 array:
1 2 3 4↙
5 6 7 8↙
9 0 -1 -2↙

max=9,row=2,col=0
数组行列数输入提示信息: “Input m,n:\n”
数组输入提示信息: "Input %d
%d array:\n"
输入格式:
输入数组行列数:"%d,%d"
输入数组元素:"%d"
输出格式: “max=%d,row=%d,col=%d\n”

代码实现

#include <stdio.h>
#include <string.h>
void InputArray(int *p, int m, int n);
int  FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main()
{
    int m,n,a[10][10]={0},*p=NULL,max=0,*q=0,*w=0,i,y;
    printf("Input m,n:\n");
    scanf("%d,%d",&m,&n);
    p=a;
    printf("Input %d*%d array:\n",m,n);
    InputArray(p,m,n);
    max=FindMax(p,m,n,&q,&w);
    printf( "max=%d,row=%d,col=%d\n",max,q,w);
    return 0;
}
void InputArray(int *p, int m, int n)
{
    int i,y;
    for(i=0;i<m;i++)
    {
        for(y=0;y<n;y++)
        {
            scanf("%d",p+i*n+y);
        }
    }
}
int  FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
    int i,y;
    int max=*p;
    for(i=0;i<m;i++)
    {
        for(y=0;y<n;y++)
        {
            if(max<*(p+i*n+y))
            {
                max=*(p+i*n+y);
                *pRow=i,*pCol=y;
            }
        }
    }
    return max;
}

7. 冒泡排序


题目内容
采用冒泡法进行升序排序法的基本原理是:对数组中的n个数执行n-1遍检查操作,在每一遍执行时,对数组中剩余的尚未排好序的元素进行如下操作:对相邻的两个元素进行比较,若排在后面的数小于排在前面的数,则交换其位置,这样每一遍操作中都将参与比较的数中的最大的数沉到数组的底部,经过n-1遍操作后就将全部n个数按从小到大的顺序排好序了。程序的某次运行结果如下:
Input n: 10↙
Input 10 numbers: 2 9 3 4 0 6 8 7 5 1↙
Sorting results: 0 1 2 3 4 5 6 7 8 9
输入数据个数提示:“Input n:”
输入数据提示:“Input %d numbers:”
输入格式: “%d”
输出提示:“Sorting results:”
输出格式:"%4d"

代码实现

#include <stdio.h>
#include <string.h>
int main()
{
    int n,i,a[100],y,t;
    printf("Input n:");
    scanf("%d",&n);
    printf("Input %d numbers:",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(y=i+1;y<n;y++)
        {
            if(a[i]>a[y])
            {
                t=a[y];
                a[y]=a[i];
                a[i]=t;
            }
        }
    }
    printf("Sorting results:");
    for(i=0;i<n;i++)
    {
        printf("%4d",a[i]);
    }
    return 0;
}

8. 删除字符串中与某字符相同的字符


题目内容
在字符串中删除与某字符相同的字符,要求用字符数组作函数参数。
程序运行结果示例:
Input a string:
hello, my friend!↙
Input a character:
!↙
Results:hello, my friend
输入字符串的提示信息: “Input a string:\n”
输入单个字符的提示信息: “Input a character:\n”
输入格式:
字符串输入用 gets()函数
单个字符输入用 getchar()函数
输出格式:“Results:%s\n”

代码实现

#include <stdio.h>
#include <string.h>
int main()
{
    char a[100],b,c[100];
    int i,y;
    printf( "Input a string:\n");
    gets(a);
    printf( "Input a character:\n");
    b=getchar();
    for(i=y=0;i<strlen(a);i++)
    {
        if(a[i]!=b)
        {
            c[y]=a[i];
            y++;
        }
    }
    c[y]='\0';
    printf("Results:%s\n",c);
    return 0;
}

9. 求最大数和最小数的最大公约数


题目内容
从键盘输入10个正整数,求出最大数,最小数,以及他们的最大公约数。要求用数组实现。
程序运行结果示例1:
Input 10 numbers:
15 23 56 87 94 105 78 19 22 43↙
maxNum=105
minNum=15
15
程序运行结果示例2:
Input 10 numbers:
33 1 2 9 8 7 5 4 0 10↙
maxNum=33
minNum=0
输入提示信息:“Input 10 numbers:\n”
输入格式: “%d”
输出格式
最大数输出格式:“maxNum=%d\n”
最小数输出格式:“minNum=%d\n”
最大公约数输出格式:"%d"

代码实现

#include <stdio.h>
#include <string.h>
int main()
{
    int a[10],max,min,i,y;
    printf("Input 10 numbers:\n");
    for(i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
    }
    max=min=a[0];
    for(i=1;i<10;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
        }
        if(a[i]<min)
        {
            min=a[i];
        }
    }
    printf("maxNum=%d\n",max);
    printf("minNum=%d\n",min);
    for(y=min;y>0;y--)
    {
        if(min%y==0 && max%y==0)
        {
            printf("%d",y);
            break;
        }
    }
    return 0;
}

10. 数列合并


题目内容
已知两个不同长度的降序排列的数列(假设序列的长度都不超过5),请编程将其合并为一个数列,使合并后的数列仍保持降序排列。
提示】假设两个降序排列的数列分别保存在数组a和数组b中,用一个循环,从前往后依次比较保存在数组a和数组b中的两个剩余序列里的第一个数,将其中的较大者存到数组c中,当一个较短的序列存完后,再将较长的序列剩余的部分依次保存到数组c的末尾。假设两个序列的长度分别是m和n,在比较剩余序列的循环中,用i和j分别记录两个序列待比较的数组元素位置,循环结束后,若i小于m,则说明数组a中的数有剩余,将数组a中剩余的数存到数组c的末尾即可;若j小于n,则说明数组b中的数有剩余,将数组b中剩余的数存到数组c的末尾即可。在第一个循环中,用k记录往数组c中存了多少个数,在第二个循环中,就从k这个位置开始继续存储较长序列中剩余的数。
函数原型:void Merge(int a[], int b[], int c[], int m, int n);
函数功能:将两个长度分别为m和n、降序排列的子序列a和b合并后放到数组c中
程序运行结果示例1:
Input m,n: 3,2↙
Input array a: 5 3 1↙
Input array b: 4 2↙
5 4 3 2 1
程序运行结果示例2:
Input m,n: 3,3↙
Input array a: 31 27 -5↙
Input array b: 98 30 -7↙
98 31 30 27 -5 -7
输入两个数列长度的提示信息:“Input m,n:”
输入数列a的提示信息:“Input array a:”
输入数列b的提示信息:“Input array b:”
输入格式:
数列长度的输入格式:"%d,%d"
数列中每个数据的输入格式:"%d"
输出格式:"%4d"

代码实现

#include<stdio.h>
#define N 5
void Merge(int a[], int b[], int c[], int m, int n);
int main(void)
{
    int m,n;
    int a[N] = {0};
    int b[N] = {0};
    int c[N*2] = {0};
    printf("Input m,n:");
    scanf("%d,%d", &m, &n);
    printf("Input array a:");

    for(int i = 0; i < m; i++)
        scanf("%d",&a[i]);

    printf("Input array b:");

    for(int i = 0; i < n; i++)
        scanf("%d", &b[i]);

    Merge(a,b,c,m,n);

    for(int i = 0; i <(m + n); i++)
        printf("%4d",c[i]);

    return 0;
}

void Merge(int a[], int b[], int c[], int m, int n)
{
    int i = 0;
    int j = 0;
    int k = 0;
    while(i < m && j < n)
    {
        if(a[i] >= b[j])
        {
            c[k++] = a[i++];
        }
        else
        {
            c[k++] = b[j++];
        }
    }

    while(i < m)
        c[k++] = a[i++];

    while(j < n)
        c[k++] = b[j++];
}

http://www.niftyadmin.cn/n/1237031.html

相关文章

教你怎么用cmd 运行带包的java文件!!!

超详细&#xff01; 有三种方法&#xff0c;但是其实本质都一样&#xff0c;都是让javac命令找到所需编译的源文件&#xff0c;然后执行按照classpath寻找要执行的class文件。&#xff08;ps:以下三种方法都是已经进了对应的文件夹后操作的&#xff09; 方法一&#xff1a;直接…

哈工大C语言程序设计精髓第十二周

由于这些代码也是我初学时写的代码&#xff0c;故其中的规范程度及简洁程度并不很好&#xff08;此处我后来写的有可以参考一下->C语言代码规范&#xff09;&#xff0c;但是能很好的接近出初学者的水平&#xff0c;也更有参考价值&#xff01;排版不易&#xff0c;喜欢就点…

哈工大C语言程序设计精髓第十三周

由于这些代码也是我初学时写的代码&#xff0c;故其中的规范程度及简洁程度并不很好&#xff08;此处我后来写的有可以参考一下->C语言代码规范&#xff09;&#xff0c;但是能很好的接近出初学者的水平&#xff0c;也更有参考价值&#xff01;排版不易&#xff0c;喜欢就点…

面试学习笔记

1.熟悉mysql 存储引擎、事务隔离级别、锁、索引&#xff0c;熟悉sql优化工作 mysql 主要的存储引擎是 innerdb myisam 2.熟练掌握spring、mybatis、spring cloud、spring boot 等主流java框架 3.熟练使用redis ,可以灵活运营redis的五种数据类型&#xff0c;熟悉redis持久化和…

哈工大C语言程序设计精髓第十四周

由于这些代码也是我初学时写的代码&#xff0c;故其中的规范程度及简洁程度并不很好&#xff08;此处我后来写的有可以参考一下->C语言代码规范&#xff09;&#xff0c;但是能很好的接近出初学者的水平&#xff0c;也更有参考价值&#xff01;排版不易&#xff0c;喜欢就点…

哈工大C语言程序设计精髓第十五周

由于这些代码也是我初学时写的代码&#xff0c;故其中的规范程度及简洁程度并不很好&#xff08;此处我后来写的有可以参考一下->C语言代码规范&#xff09;&#xff0c;但是能很好的接近出初学者的水平&#xff0c;也更有参考价值&#xff01;排版不易&#xff0c;喜欢就点…

spring项目在服务器获取时间少8小时

项目场景&#xff1a; 项目的每条sql操作&#xff08;mysql&#xff09;都要写入其更新时间&#xff0c;方便后期查错修改&#xff0c;即datalog。 问题描述&#xff1a; 项目本地运行没有问题&#xff0c;部署到测试服务器测试也没有问题&#xff0c;但是部署到生产服务器就出…

html和CSS基础学习

(1)<meta />用来设置关键字&#xff1b;用来对网页的描述&#xff0c;请求重定向&#xff08;5秒跳转的页面&#xff09; <a>超链接 src表示路径&#xff0c;herf表示地址 <img>标签 插入图片 src图片的路径 相对路径&#xff0c;alt属性表示对图片的说明&…