C语言程序设计精髓 第11周——指针的孪生兄弟 练兵区——编程题

1找出按字典顺序排在最前面的国名(4分)

题目内容:

输入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”

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include <stdio.h>
#include <string.h>
#define N 100

int main()
{
	char country[N][N], first[N]="zzzzzz";
	printf("Input five countries' names:\n");
	int i, n=5;
	for(i=0; i<n; ++i){
		gets(country[i]);
		if(strcmp(country[i], first)<0){
			strcpy(first, country[i]);
		}
	}
	printf("The minimum is:%s\n", first);
    return 0;
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 1ms 256kb
2
用例2通过 1ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

2学生成绩管理系统V2.0(4分)

题目内容:

某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,参考前面章节的“学生成绩管理系统V1.0”,用一维数组和函数指针作函数参数编程实现如下菜单驱动的学生成绩管理系统:

(1)录入每个学生的学号和考试成绩;

(2)计算课程的总分和平均分;

(3)按成绩由高到低排出名次表;

(4)按成绩由低到高排出名次表;

(5)按学号由小到大排出成绩表;

(6)按学号查询学生排名及其考试成绩;

(7)按优秀(90 ~ 100)、良好(80 ~ 89)、中等(70 ~ 79)、及格(60 ~ 69)、不及格(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)、良好(80 ~ 89)、中等(70 ~ 79)、及格(60 ~ 69)、不及格(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”

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXN 1000
int n; //represent students number
typedef struct {
	long int stuID;
	char name[MAXN];
	float score;
}STUDENT;
STUDENT stu[MAXN];
//0 代表降序 1代表升序 
int cmpByScore1(const void *a, const void *b){
	STUDENT *s1 = (STUDENT *)a;
	STUDENT *s2 = (STUDENT *)b;
	return s1->score - s2->score;
}
int cmpByScore0(const void *a, const void *b){
	STUDENT *s1 = (STUDENT *)a;
	STUDENT *s2 = (STUDENT *)b;
	return s2->score - s1->score;
}
int cmpByNumber0(const void *a, const void *b){
	STUDENT *s1 = (STUDENT *)a;
	STUDENT *s2 = (STUDENT *)b;
	return s2->stuID - s1->stuID;
}
int cmpByNumber1(const void *a, const void *b){
	STUDENT *s1 = (STUDENT *)a;
	STUDENT *s2 = (STUDENT *)b;
	return s1->stuID - s2->stuID;
}
void menu(); 
void inputRecord();
void calc();
void sortByScore(int order);
void sortByNumber(int order);
void searchByNumber();
void analysis();
void print();
void Exit();
int main()
{	
	printf("Input student number(n<30):\n");
	scanf("%d", &n);
	while(1){
		menu();
	}
	
}
void menu(){
	printf("Management for Students' scores\n");
	printf("1.Input record\n");
	printf("2.Caculate total and average score of course\n");
	printf("3.Sort in descending order by score\n");
	printf("4.Sort in ascending order by score\n"); 
	printf("5.Sort in ascending order by number\n");
	printf("6.Search by number\n");
	printf("7.Statistic analysis\n");
	printf("8.List record\n");
	printf("0.Exit\n");
	printf("Please Input your choice:\n");
	int choice;
	scanf("%d", &choice);
	switch(choice){
		case 1: inputRecord(); break;
		case 2: calc(); break;
		case 3: sortByScore(0); break;
		case 4: sortByScore(1); break;
		case 5: sortByNumber(1); break; 
		case 6: searchByNumber(); break;
		case 7: analysis(); break;
		case 8: print(); break;
		case 0: Exit();
		default: printf("Input error!\n");
	}
}
void inputRecord(){
	printf("Input student's ID and score:\n");
	int i;
	for(i=0; i<n; ++i){
		scanf("%ld%f", &stu[i].stuID, &stu[i].score);
	}
}
void calc(){
	int i;
	float sum=0, avg;
	for(i=0; i<n; ++i){
		sum+=stu[i].score;
	}
	avg=sum/n;
	printf("sum=%.0f,aver=%.2f\n", sum, avg);
}
void sortByScore(int order){
	//0 代表降序 1代表升序 
	if(order){
		printf("Sort in ascending order by score:\n");
		qsort(stu, n, sizeof(stu[0]),cmpByScore1);
	}  else {
		printf("Sort in descending order by score:\n");
		qsort(stu, n, sizeof(stu[0]),cmpByScore0);
	}
	
	int i;
	for(i=0; i<n; ++i){
		printf("%ld\t%.0f\n", stu[i].stuID, stu[i].score);
	}
}
void sortByNumber(int order){
	//0 代表降序 1代表升序 
	if(order){
		printf("Sort in ascending order by number:\n");
		qsort(stu, n, sizeof(stu[0]),cmpByNumber1);
	}  else {
		printf("Sort in descending order by number:\n");
		qsort(stu, n, sizeof(stu[0]),cmpByNumber0);
	}
	int i;
	for(i=0; i<n; ++i){
		printf("%ld\t%.0f\n", stu[i].stuID, stu[i].score);
	}
}
void searchByNumber(){
	printf("Input the number you want to search:\n");
	long int id;
	scanf("%ld", &id);
	int i;
	for(i=0; i<n; ++i){
		if(stu[i].stuID==id){
			printf("%ld\t%.0f\n", stu[i].stuID, stu[i].score);
			return;
		}
	}
	printf("Not found!\n");
}
void analysis(){
	int i, cnt[6]={0};
	for(i=0; i<n; ++i){
		if(stu[i].score>=100) cnt[5]++;
		else if(stu[i].score>=90) cnt[0]++;
		else if(stu[i].score>=80) cnt[1]++;
		else if(stu[i].score>=70) cnt[2]++;
		else if(stu[i].score>=60) cnt[3]++;
		else cnt[4]++;
	}
	printf("<60\t%d\t%.2f%%\n", cnt[4], 100*(float)cnt[4]/n);
	
	printf("%d-%d\t%d\t%.2f%%\n", 60, 69, cnt[3], 100*(float)cnt[3]/n);
	printf("%d-%d\t%d\t%.2f%%\n", 70, 79, cnt[2], 100*(float)cnt[2]/n);
	printf("%d-%d\t%d\t%.2f%%\n", 80, 89, cnt[1], 100*(float)cnt[1]/n);
	printf("%d-%d\t%d\t%.2f%%\n", 90, 99, cnt[0], 100*(float)cnt[0]/n);
	printf("%d\t%d\t%.2f%%\n", 100, cnt[5], 100*(float)cnt[5]/n);
}
void print(){
	int i;
	for(i=0; i<n; ++i){
		printf("%ld\t%.0f\n", stu[i].stuID, stu[i].score);
	}
}
void Exit(){
	printf("End of program!\n");
	exit(0);
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 14ms 256kb
2
用例2通过 2ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

3月份表示(4分)

题目内容:

用指针数组保存表示每个月份的英文单词以及“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"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXN 100
char month[][MAXN]={ "January"
					 ,"February"
					 ,"March"
					 ,"April"
					 ,"May"
					 ,"June"
					 ,"July"
					 ,"August"
					 ,"September"
					 ,"October"
					 ,"November"
					 ,"December"};

int main()
{	
	printf("Input month number:\n");
	int n;
	scanf("%d", &n);
	if(n>0 && n<13){
		printf("month %d is %s\n", n, month[n-1]);
	} else {
		printf("Illegal month\n");
	}
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 1ms 256kb
1
用例2通过 1ms 256kb
2
用例3通过 2ms 256kb
1
提交答案本次得分/总分:4.00/4.00分

4程序改错——1(4分)

题目内容:

从键盘任意输入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]);
     }
}

程序运行结果示例:

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"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include  <stdio.h>
#define STUD 30            //最多可能的学生人数
#define COURSE 5             //最多可能的考试科目数
void  Total(int score[][COURSE], int sum[], float aver[], int m, int n);
void  Print(int score[][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]);
//                printf("%d\n", score[i][j]);
            }
        }
        Total(score, sum, aver, m, n);
        Print(score, sum, aver, m, n);
        return 0;
}
 
void  Total(int score[][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[][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]);
     }
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 1ms 256kb
2
用例2通过 1ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

5程序改错——2(4分)

题目内容:

下面主函数调用函数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;
         } 
      }   
  } 
}

程序运行结果示例:

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()函数

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include  <stdio.h>
#include  <string.h>
#define   M  150 /* 最多的字符串个数 */
#define   N  10 /* 字符串最大长度 */
void SortString(char ptr[][N], int n);
int main()
{
  int    i, n; 
  char   pStr[M][N];
  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[][N], int n)
{
  int   i, j;
  char  temp[N];
  for (i=0; i<n-1; i++)    
  {
      for (j=i+1; j<n; j++)
      {
         if (strcmp(ptr[j], ptr[i]) < 0)    
         {
              strcpy(temp, ptr[i]);
			  strcpy(ptr[i], ptr[j]);
			  strcpy(ptr[j], temp); 
//			  temp = ptr[i];
//              ptr[i] = ptr[j];
//              ptr[j] = temp;
         } 
      }   
  } 
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 2ms 256kb
2
用例2通过 1ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

6找数组最值(4分)

题目内容:

按如下函数原型编程从键盘输入一个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”

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include <stdio.h>
#define N 100
int main(){
	int m, n, i, j, a[N][N], row=0, col=0;
	printf("Input m,n:\n");
	scanf("%d,%d", &m, &n);
	printf("Input %d*%d array:\n", m, n);
	for(i=0; i<m; ++i){
		for(j=0; j<n; ++j){
			scanf("%d", &a[i][j]);
			if(a[i][j]>a[row][col]){
				row=i; col=j;
			}
		}
	}
	printf( "max=%d,row=%d,col=%d\n", a[row][col], row, col);
	return 0;
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 1ms 256kb
2
用例2通过 2ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

7冒泡排序(4分)

题目内容:

采用冒泡法进行升序排序法的基本原理是:对数组中的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"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

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

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 13ms 256kb
2
用例2通过 1ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

8删除字符串中与某字符相同的字符(4分)

题目内容:

在字符串中删除与某字符相同的字符,要求用字符数组作函数参数。

程序运行结果示例:

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”

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

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

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 1ms 256kb
2
用例2通过 2ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

9求最大数和最小数的最大公约数(4分)

题目内容:

从键盘输入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"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include <stdio.h>
#define N 10
int GCD(int a, int b){
	if(b==0) return -1;
	return a%b ? GCD(b,a%b) : b;
}
int main(){
	printf("Input 10 numbers:\n");
	int i, a[N], Max=-99, Min=99;
	for(i=0; i<N; ++i){
		scanf("%d", &a[i]);
		if(a[i]>Max) Max=a[i];
		if(a[i]<Min) Min=a[i];
	}
	printf("maxNum=%d\nminNum=%d\n", Max, Min);
	int res=GCD(Max, Min);
	if(res!=-1){
		printf("%d",res );
	}
	return 0;
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 2ms 256kb
2
用例2通过 2ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

10数列合并(4分)

题目内容:

已知两个不同长度的降序排列的数列(假设序列的长度都不超过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"

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb
C

#include <stdio.h>
#define N 100

int main(){
	int m, n, i, j, k, a[N], b[N], c[N];
	printf("Input m,n:");
	scanf("%d,%d", &m, &n);
	printf("Input array a:");
	for(i=0; i<m; ++i){
		scanf("%d", &a[i]);
	} 
	printf("Input array b:");
	for(j=0; j<n; ++j){
		scanf("%d", &b[j]);
	}
	i=j=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++];
	for(i=0; i<k; ++i)
		printf("%4d", c[i]);
	return 0;
}

用例测试结果 运行时间 占用内存 提示 得分
用例1通过 2ms 256kb
2
用例2通过 2ms 256kb
2
提交答案本次得分/总分:4.00/4.00分

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言程序设计精髓MOOC》第三主要内容是关于指针和数组的学习。 首先是指针的介绍和使用。指针C语言中一个非常重要的概念,它可以用来间接访问内存中的数据,通过指针可以实现对变量地址的操作。在学习过程中,我们了解了指针的定义和声明,以及指针与数组之间的关系。指针程序设计中的应用非常广泛,特别是在动态内存分配和函数调用等方面,有着重要的作用。 其次是数组的使用。数组是一种由相同类型的元素组成的集合,它在C语言中非常常用。在第三的学习中,我们了解了数组的定义、初始化和遍历等基本操作,还学习了一些在数组中常用的算法和技巧。通过多维数组和指针数组的学习,我们可以更灵活地处理多个数据。 除了指针和数组,第三还涉及到了C语言中的结构体(struct)和文件的输入输出操作等内容。结构体是一种可以封装多个不同类型的数据的自定义数据类型,它在实际的程序设计中经常被用于组织和管理数据。文件的输入输出操作涉及了C语言中如何读写文件以及相关的文件处理函数等知识点。 通过学习《C语言程序设计精髓MOOC》第三的内容,我们对指针和数组有了更深入的认识,并且掌握了它们的基本用法和应用技巧。这对于进一步学习和理解C语言程序设计以及其他高级编程语言都非常有帮助。此外,通过作业和练习的完成,我们可以检验和巩固所学的知识,提高我们自己的编程能力。希望通过这门课程的学习,能够让我们对C语言有更全面和深入的了解,为以后的学习和工作打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值