中国大学MOOC(C语言程序设计精髓)作业

第十一周 练兵区编程题
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>
int Y(char str[][11]);
int main()
{
	char str[5][11];
	int i, ret;
	printf("Input five countries' names:\n");
	for(i=0;i<5;i++)
	{
		gets(str[i]);
	}
	ret = Y(str);
	printf("The minimum is:%s\n", str[ret]);
	return 0;
}
int Y(char str[][11])
{
	int i, ret=0;
	for(i=0;i<5;i++)
	{
		if(strcmp(str[ret], str[i]) > 0)
			ret = i;
	}
	return ret;
}

2、学生成绩管理系统V2.0(4分)
题目内容:

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

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

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

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

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

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

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

(7)按优秀(90100)、良好(8089)、中等(7079)、及格(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"

按优秀(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" 

用户输入的菜单项超出0-8的选择范围,输出错误提示信息:“Input error!\n”

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

时间限制:500ms内存限制:32000kb
C语言实现:

#include <stdio.h>//		一维数组和函数指针作函数参数;
#include <string.h>
void Y(void);
void Input(long a[], float b[], int n);
void Caculate(long a[], float b[], int n);
void Sort(long a[], float b[], int n, void (*order)(long a[], float b[], int ));
void Descending_score(long a[], float b[], int r);
void Ascending_score(long a[], float b[], int r);
void Ascending_number(long a[], float b[], int r);
void Search(long a[], float b[], int n);
void Statistic(long a[], float b[], int n);
void List(long a[], float b[], int n);
int main()
{
	int n, m;
	long a[30];
	float b[30];
	printf("Input student number(n<30):\n");
	scanf("%d", &n);
	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");
	while(scanf("%d", &m) && m != 0)
	{
		switch(m)
		{
		case 1:Input(a, b, n);
			break;
		case 2:Caculate(a, b, n);
			break;
		case 3:printf("Sort in descending order by score:\n");
			Sort(a, b, n, Descending_score);
			break;
		case 4:printf("Sort in ascending order by score:\n");
			Sort(a, b, n, Ascending_score);
			break;
		case 5:printf("Sort in ascending order by number:\n");
			Sort(a, b, n, Ascending_number);
			break;
		case 6:Search(a, b, n);
			break;
		case 7:Statistic(a, b, n);
			break;
		case 8:Sort(a, b, n, Ascending_number);
			break;
		default:printf("Input error!\n");
		}
		Y();
	}
	printf("End of program!\n");
	return 0;
}
void Y(void)
{
	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");
	return ;
}
void Input(long a[], float b[], int n)
{
	int i;
	printf("Input student's ID and score:\n");
	for(i=0;i<n;i++)
	{
		scanf("%ld%f", &a[i], &b[i]);
	}
	return ;
}
void Caculate(long a[], float b[], int n)
{
	float sum=0, aver=0;
	int i;
	for(i=0;i<n;i++)
	{
		sum = sum + b[i];
	}
	printf("sum=%.0f,aver=%.2f\n", sum, sum/n);
	return ;
}
void Descending_score(long a[], float b[], int r)
{
	long term1;
	float term2;
	if(b[r] < b[r+1])
	{
		term1 = a[r];
		a[r] = a[r+1];
		a[r+1] = term1;
		term2 = b[r];
		b[r] = b[r+1];
		b[r+1] = term2;
	}
}

void Ascending_score(long a[], float b[], int r)
{
	long term1;
	float term2;
	if(b[r] > b[r+1])
	{
		term1 = a[r];
		a[r] = a[r+1];
		a[r+1] = term1;
		term2 = b[r];
		b[r] = b[r+1];
		b[r+1] = term2;
	}
	
}
void Ascending_number(long a[], float b[], int r)
{
	long term1;
	float term2;
	if(a[r] > a[r+1])
	{
		term1 = a[r];
		a[r] = a[r+1];
		a[r+1] = term1;
		term2 = b[r];
		b[r] = b[r+1];
		b[r+1] = term2;
	}
}
void Search(long a[], float b[], int n)
{
	int i;
	long num;
	printf("Input the number you want to search:\n");
	scanf("%ld", &num);
	for(i=0;i<n;i++)
	{
		if(num%1000 == a[i]%1000)
		{
			printf("%ld\t%.0f\n", a[i], b[i]);
			return ;
		}
	}
	printf("Not found!\n");
	return ;
}
void Statistic(long a[], float b[], int n)
{
	int i;
	int x[6];
	memset(x, 0, sizeof(x));
	for(i=0;i<n;i++)
	{
		if(b[i]<60)
			x[0]++;
		else if(b[i]<70)
			x[1]++;
		else if(b[i]<80)
			x[2]++;
		else if(b[i]<90)
			x[3]++;
		else if(b[i]<100)
			x[4]++;
		else 
			x[5]++;
	}
	printf("<60\t%d\t%.2f%%\n", x[0], (float)x[0]*100/n);
	for(i=1;i<5;i++)
	{
		printf("%d-%d\t%d\t%.2f%%\n",i*10+50, i*10+60-1, x[i], (float)x[i]*100/n);
	}
	printf("%d\t%d\t%.2f%%\n",100, x[5], (float)x[5]*100/n);
	return ;
}
void Sort(long a[], float b[], int n, void (*order)(long a[], float b[], int n))
{
	int i, r;
	for(i=0;i<n;i++)
	{
		for(r=0;r<n-1;r++)
		{
			(*order)(a, b, r);
		}
	}
	for(i=0;i<n;i++)
	{
		printf("%ld\t%.0f\n", a[i], b[i]);
	}
	return ;
}

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>
int main()
{
	char *str[13]={"Illegal month","January","February","March","April","May","June","July","August","September","October","November","December"};
	int i;
	printf("Input month number:\n");
	scanf("%d", &i);
	if(i<1 || i>12)
	{
		printf("%s\n", str[0]);
	}
	else 
	{
		printf("month %d is %s\n", i, str[i]);
	}
	return 0;
}

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];
		 int (*p)[COURSE];
		 p = &score[0];
         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(p, sum, aver, m, n);
        Print(p, 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]);
     }
}

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[], int n);
int main()
{
  int    i, n; 
  char   *pStr[M], str[M][N];
  printf("How many countries?\n");
  scanf("%d",&n);
  getchar();        /* 读走输入缓冲区中的回车符 */
  printf("Input their names:\n");
  for (i=0; i<n; i++)  
  {
      gets(str[i]);  /* 输入n个字符串 */
  }
  for(i=0;i<n;i++)
  {
	  pStr[i] = str[i];
  }
  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;
         } 
      }   
  } 
}

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 11
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, ret, pRow, pCol;
	int a[N][N];
	printf("Input m,n:\n");
	scanf("%d,%d", &m, &n);
	printf("Input %d*%d array:\n", m, n);
	InputArray(*a, m, n);
	ret = FindMax(*a, m, n, &pRow, &pCol);
	printf("max=%d,row=%d,col=%d\n", ret, pRow, pCol);
	return 0;
}
void InputArray(int *p, int m, int n)
{
	int i, j;
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			scanf("%d", &p[i*N+j]);
		}
	}
	return ;
}
int  FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
	int i, j;
	int max = p[0];
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			if(max < p[i*N+j])
			{
				max = p[i*N+j];
				*pRow = i;
				*pCol = j;
			}
		}
	}
	return max;
}

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 n, i, r, term;
	int a[N];
	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;i++)
	{
		for(r=0;r<n-1;r++)
		{
			if(a[r] > a[r+1])
			{
				term = a[r];
				a[r] = a[r+1];
				a[r+1] = term;
			}
		}
	}
	printf("Sorting results:");
	for(i=0;i<n;i++)
	{
		printf("%4d", a[i]);
	}
	return 0;
}

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>
#include <string.h>
#define N 100
void Y(char *p, char c, int n);
int main()
{
	int n;
	char str[N], c;
	char *p;
	p = str;
	printf("Input a string:\n");
	gets(str);
	n = strlen(str);
	printf("Input a character:\n");
	c = getchar();
	Y(p, c, n);
	printf("Results:%s\n", str);
	return 0;
}
void Y(char *p, char c, int n)
{
	int i, find = 0;
	for(i=0;i<n;i++)
	{
		if(*(p+i) == c)
		{
			*(p+i) = '\0';
			find = 1;
		}
		if(find)
		{
			*(p+i) = *(p+i+1);
		}
	}
	return ;
}

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 Y(int max, int min);
int main()
{
	int a[N];
	int i, maxNum, minNum, ret;
	printf("Input 10 numbers:\n");
	for(i=0;i<N;i++)
	{
		scanf("%d", &a[i]);
	}
	minNum = maxNum = a[0];
	for(i=0;i<N;i++)
	{
		if(minNum > a[i])
		{
			minNum = a[i];
		}
		if(maxNum < a[i])
		{
			maxNum = a[i];
		}
	}
	ret = Y(maxNum, minNum);
	printf("maxNum=%d\n", maxNum);
	printf("minNum=%d\n", minNum);
	if(ret)
	{
		printf("%d", ret);
	}
	return 0;
}
int Y(int max, int min)
{
	int i, ret=1;
	if(min == 0)
		return 0;
	for(i=1;i<=max;i++)
	{
		if(min%i==0 && max%i==0)
		{
			ret = i;
		}
	}
	return ret;
}

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
void Merge(int a[], int b[], int c[], int m, int n);
int main()
{
	int a[N], b[N], c[N];
	int n, m;
	int i;
	printf("Input m,n:");
	scanf("%d,%d", &n, &m);
	printf("Input array a:");
	for(i=0;i<n;i++)
	{
		scanf("%d", &a[i]);
	}
	printf("Input array b:");
	for(i=0;i<m;i++)
	{
		scanf("%d", &b[i]);
	}
	Merge(a, b, c, m, n);
	for(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, r, term;
	for(i=0,r=0;i<n+m;i++)
	{
		if(i<n)
		{
			c[i] = a[r];
			r++;	
		}
		else 
		{
			c[i] = b[i-r];
		}
	}
	for(i=0;i<n+m;i++)
	{
		for(r=0;r<n+m-1;r++)
		{
			if(c[r] < c[r+1])
			{
				term = c[r];
				c[r] = c[r+1];
				c[r+1] = term;
			}
		}
	}
	return ;
}
  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码农12138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值