程序填空题(51-64)

第五十一题

题目

给定程序BLANK1. C中,函数fun的作用是:参数xx的前10个元素已经按升序排好序,将参

数num按升序插入到数组xx中。

#include <stdio.h>

void func(int xx[], int num)
{
	int n1,n2,pos,i,j;
	pos=xx[9];
	if (num > pos)
/**********found**********/
		xx[10] = __(1)___;
	else {
		for(i=0;i<10;i++)
		{
			if(xx[i]>num){
				n1=xx[i];
				xx[i]=num;
				for(j=i+1;j<11;j++){
					n2=xx[j];
					xx[j] = n1;
/**********found**********/
					_____(2)____;
				}
/**********found**********/
				_____(3)_____;
			}
		}
	}
}

int main()
{
	int xx[11] = {2,5,7,10,17,51,63,73,85,99};
	int i,num;
	printf("original array is:\n");
	for(i=0;i<10;i++) printf("%5d",xx[i]);
	printf("\n");
	printf("insert a new number:");
	scanf("%d", &num);
	func(xx, num);
	for(i=0;i<11;i++) printf("%5d", xx[i]);
	printf("\n");
	return 0;
}

解析

pos n1=n2 break

分析

第五十二题

题目

给定程序BLANK1.C中,函数fun的功能是在数组中找出两科成绩之和最高的学生并返回其

在数组中的下标。对所给函数int fun(STU *d, int n),主函数传给形参d的是学生数组名,而

传给形参n的是该数组中学生的个数。

例如:若学生数组数据为:2016500301李清水83 92

2016500336刘世才85 94

2016500371王子晨88 88

则调用该函数后,程序输出为: 2016500336 刘世才85 94

#include<stdio.h>
typedef struct stu
{
	char ID[30];
	char name[20];
	int score[2];
} STU;
int fun(STU *d,int n)
{
	int i,m;
	/******found******/
	______(1)______;
	for(i=1;i<n;i++)
	/******found******/
		if(d[i].score[0]+d[i].score[1]>________(2)________)
			m=i;
	/******found******/
	______(3)______;
}

void main()
{
	STU a[10]={ "2016500301","李清水",83,92,"2016500336","刘世才",85,94,"2016500371","王子晨",88,88};
	int i,n=3;
	i=fun(a,n);
	printf("%30s%20s%4d%4d",a[i].ID,a[i].name,a[i].score[0],a[i].score[1]);
	printf("\n");
}

解析

m=0 d[m].score[0]+d[m].score[1] return m

分析

第五十三题

题目

#include  <stdio.h>
#include  <string.h>
#define   N   5
#define   M   10
int fun(char  (*ss)[M], int  k)
{ int  i,j=0,len;
/**********found**********/
  for(i=0; i< __1__ ; i++)
  {  len=strlen(ss[i]);
/**********found**********/
     if(len<= __2__)
/**********found**********/
        strcpy(ss[j++],__3__);
  }
  return  j;
}
main()
{ char  x[N][M]={"Beijing","Shanghai","Tianjing","Nanjing","Wuhan"};
  int  i,f;
  printf("\nThe original string\n\n");
  for(i=0;i<N;i++)puts(x[i]);  printf("\n");
  f=fun(x,7);
  printf("The string witch length is less than or equal to 7 :\n");
  for(i=0; i<f; i++)  puts(x[i]);printf("\n");
  getchar();
}

解析

N k *(ss+i)

分析

第五十四题

题目

给定程序BLANK1. C中,函数fun的功能是:根据所给的一组学生的成绩,计算出平均成

绩,并计算低于平均成绩的学生的平均成绩作为函数值返回。

例如:如有成绩为: 50, 60, 70, 80, 90, 100, 55, 65, 75, 85, 95, 99,

则运行结果应为:62.500000

#include <stdio.h>
double fun(double x[], int n)
{
	int i,k=0;
	double avg = 0.0,sum=0.0;
	for(i=0;i<n;i++)
/***********found**********/
		avg+=___(1)___;
/***********found**********/
	___(2)___/=n;
	for(i=0;i<n;i++)
		if(x[i]<avg)
		{
/***********found**********/
			___(3)___+=x[i];
			k++;
		}
	return sum/k;
}
main()
{
	double score[12]={50,60,70,80,90,100,55,65,75,85,95,99};
	double aa;
	aa=fun(score,12);
	printf("%f\n",aa);
}

解析

x[i] avg sum

分析

第五十五题

题目

给定程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组中大于平均值的数据移至数组的前部,小于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数中输出平均值和移动后的数据。

例如,有10个正数:46 30 3240 6 17 45 15 48 26,平均值为:30.500000

移动后的输出为:46 32 40 45 48 30 6 17 15 26

#include  <stdlib.h>
#include  <stdio.h>
#define   N   10
double fun(double  *x)
{ int  i, j;    double  s, av, y[N];
  s=0;
  for(i=0; i<N; i++)  s=s+x[i];
/**********found**********/
  av=__1__;
  for(i=j=0; i<N; i++)
    if( x[i]>av ){
/**********found**********/
      y[__2__]=x[i]; x[i]=-1;}
  for(i=0; i<N; i++)
/**********found**********/
    if( x[i]!= __3__) y[j++]=x[i];
  for(i=0; i<N; i++)x[i] = y[i];
  return  av;
}
main()
{ int  i;     double  x[N]= {46,30,32,40,6,17,45,15,48,26};
  for(i=0; i<N; i++) printf("%4.0f ",x[i]);
  printf("\n");
  printf("\nThe average is: %f\n",fun(x));
  printf("\nThe result :\n",fun(x));
  for(i=0; i<N; i++)  printf("%5.0f ",x[i]);
  printf("\n");
  getchar();
}

解析

s/N j++ -1

分析

第五十六题

题目

函数fun的功能是:在有n个元素的结构体数组std中,查找有一门或两门不及格科目的学生,找到后输出学生的学号;函数的返回值是有不及格科目的学生人数。例如,主函数中给出了4名学生的数据,则程序运行的结果为;

学号:N1002 学号:N1006

共有2位学生有不及格科目

#include  <stdio.h>
typedef struct
{ char num[8];     /* 学号 */
  double score[2]; /* 两门课成绩 */
/**********found**********/
} __(1)__ ;
int  fun(STU  std[ ], int  n)
{  int  i, k=0;
   for(i=0; i<n; i++)
/**********found**********/
     if( (std[i].score[0]<60) __(2)__ (std[i].score[1]<60) ) 
     { k++;     printf("学号:%s ",std[i].num);   }
/**********found**********/
   return __(3)__ ;
}
main()
{  STU  std[4]={ "N1001", 76.5,82.0 ,"N1002", 53.5,73.0, 
                "N1005", 80.5,66.0,"N1006", 81.0,56.0 };
  printf( "\n共有%d位学生有不及格科目\n" , fun(std,4) );
  getchar();
}

解析

STU || k

分析

第五十七题

题目

函数fun的功能是:输出a所指数组中的前n个数据,要求每行输出5个数。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。

#include  <stdio.h>
#include  <stdlib.h>
void fun( int *a,  int n )
{  int  i;
   for(i=0; i<n; i++)
   {
/**********found**********/
     if( ___1___==0 )
/**********found**********/
           printf("___2___");
/**********found**********/
      printf("%d  ",___3___);
   }
}
main()
{  int  a[100]={0}, i,n;
   n=22;
   for(i=0; i<n;i++) a[i]=rand()%21;
   fun( a, n);
   printf("\n");
  getchar();
}

解析

a[i] \n a[i]

分析

第五十八题

题目

给定程序中,函数fun的功能是:将形参s所指字符串中的数字字符转换成对应的数值计算出这些数值的累加和作为函数值返回。

例如,形参s所指的字符串为: abs5def126jkm8,程序执行后的输出结果为:22.

#include  <stdio.h>
#include  <string.h>
#include  <ctype.h>
int fun(char  *s)
{ int  sum=0;
  while(*s) {
/**********found**********/
    if( isdigit(*s) )  sum+= *s- __1__ ;
/**********found**********/
    __2__;
  }
/**********found**********/
  return  __3__ ;
}
main()
{ char  s[81];    int  n;
  printf("\nEnter a string:\n\n");  gets(s);
  n=fun(s);
  printf("\nThe result is:  %d\n\n",n);
  getchar();
}

解析

'0' *s++ sum

分析

第五十九题

题目

给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为: 2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2。

#include  <stdio.h>
#include  <stdlib.h>
#define    N    5
typedef struct node {
  int  data;
  struct node  *next;
} NODE;
void fun(NODE  *h)
{ NODE  *p, *q, *r;
/**********found**********/
  p = h->__1__;
/**********found**********/
  if (p==__2__)  return;
  q = p->next;
  p->next = NULL;
  while (q)
  {  r = q->next;    q->next = p;
/**********found**********/
     p = q;          q = __3__;
  }
  h->next = p;
}
NODE *creatlist(int  a[])
{  NODE  *h,*p,*q;        int  i;
  h = (NODE *)malloc(sizeof(NODE));
  h->next = NULL;
  for(i=0; i<N; i++)
  {  q=(NODE *)malloc(sizeof(NODE));
     q->data=a[i];
     q->next = NULL;
     if (h->next == NULL)  h->next = p = q;
     else    {  p->next = q;  p = q;   }
  }
   return  h;
}
void outlist(NODE  *h)
{ NODE  *p;
  p = h->next;
  if (p==NULL)  printf("The list is NULL!\n");
  else
  {  printf("\nHead  ");
     do
     {  printf("->%d", p->data); p=p->next;  }
     while(p!=NULL);
     printf("->End\n");
  }
}
main()
{  NODE  *head;
   int  a[N]={2,4,6,8,10};
   head=creatlist(a);
   printf("\nThe original list:\n");
   outlist(head);
   fun(head);
   printf("\nThe list after inverting :\n");
   outlist(head);
  getchar();
}

解析

next NULL r

分析

第六十题

题目

程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩。函数fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到大)排序。

#include  <stdio.h>
#include  <string.h>
struct student {
  long  sno;
  char  name[10];
  float  score[3];
};
void fun(struct student  a[], int  n)
{
/**********found**********/
 __1__ t;
  int  i, j;
/**********found**********/
  for (i=0; i<__2__; i++)
    for (j=i+1; j<n; j++)
/**********found**********/
      if (strcmp(__3__) > 0)
      {  t = a[i];   a[i] = a[j];  a[j] = t;  }
}
main()
{ struct student  s[4]={{10001,"ZhangSan", 95, 80, 88},{10002,"LiSi", 85, 70, 78},
                    {10003,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87}};
  int  i, j;
  printf("\n\nThe original data :\n\n");
  for (j=0; j<4; j++)
  {  printf("\nNo: %ld  Name: %-8s      Scores:  ",s[j].sno, s[j].name);
     for (i=0; i<3; i++)  printf("%6.2f ", s[j].score[i]);
     printf("\n");
  }
  fun(s, 4);
  printf("\n\nThe data after sorting :\n\n");
  for (j=0; j<4; j++)
  {  printf("\nNo: %ld  Name: %-8s      Scores:  ",s[j].sno, s[j].name);
     for (i=0; i<3; i++)  printf("%6.2f ", s[j].score[i]);
     printf("\n");
  }
  getchar();
}

解析

struct student n a[i].name,a[j].name

分析

第六十一题

题目

以下程序的功能是:统计字符串(长度不超过1000个字符)中每个字母的个数(不区分大小写),若字符不是字母,则统一归为其他字符。

#include<stdio.h>
#include<string.h>
void countstr(char *st,int cutor[]);
main()
{
	char s[1000];
/**********************found***********************/
	int counters[27]= ___(1)___,i;
	printf("Input a string\n");
	gets(s);
	countstr(s,counters);
/**********************found***********************/
	for(i=0; i<=___(2)___;i++)
		printf("%c : %d\n",i+65,counters[i]);
	printf("others : %d\n",counters[i]);
}

void countstr(char *st,int cutor[])
{
	int i,j;
	char t;
	i=strlen(st);
	for(j=0;j<i;j++)
	{
		t=st[j];
		if(t>='a'&&t<='z')
		   t=t-'a'+'A';
		if(t>='A'&&t<='Z')
/**********************found***********************/
           cutor[t- ___(3)___]++;
		else
			cutor[26]++;
	}
}

解析

{0} 25 'A'

分析

第六十二题

题目

在给定程序BLANK1.C中,函数fun的功能是:根据形参c中存储的整数序列,分别统计偶数元素和奇数元素的个数,将统计结果以结构变量返回主函数,结构变量的类型由程序中定义的struct pair给出,它包含两个整数变量成员,依次对应偶数个数与奇数个数之和、偶数个数与奇数个数之差。形参d中存储的是序列的长度。

例如:若c中存储的数值依次为 3,7,12,17,2,d为5,则函数返回的结构变量中,第一个成员的值为5,第二个成员的值为-1。主函数输出n1=5,n2=-1。

#include  <stdio.h>
struct pair {
  int n1, n2;
};

struct pair fun(int* c, int d) { 
  int i;

/**********************found***********************/
  ___(1)____;

  p.n1 = p.n2 = 0;

/**********************found***********************/
  for (i=0; i<d; ____(2)____) {
    if (c[i] % 2 == 0)
      p.n1++;
    else
/**********************found***********************/
      ____(3)____;
  }

  p.n1 = p.n1 + p.n2;
  p.n2 = p.n1 - p.n2 - p.n2;

  return p;
}

int main( ) { 
  int i, c[100], d;
  struct pair p;

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

  p = fun(c, d);
  printf("n1=%d,n2=%d", p.n1, p.n2);
  return 0;
}

解析

struct pair p; i++ p.n2++

分析

第六十三题

题目

在给定程序BLANK1.C中,函数fun的功能是实现凯撒密码加密,即通过将字母表中的字母向后移动一定位置而实现加密,其中26个字母需循环使用,z后面可以看成是a,其他字符则保持不变。

例如:当密钥为k=3,即向后移动3位。若明文为:Go ahead!,则密文为:Jr dkhdg!

#include <stdio.h>
#include <string.h>
void fun(char *s, char *c, int k)
{
    int i;
/**********found**********/
    for(i=0; s[i]!=___(1)___; i++)
    {
        if(s[i]>='a' && s[i]<='z')
            c[i]=(s[i]+k)>'z'?s[i]+k-26:s[i]+k;
	else if (s[i]>='A' && s[i]<='Z')
           c[i]=(s[i]+k)>'Z'?s[i]+k-26:s[i]+k;
/**********found**********/
        else c[i]=___(2)___;
    }
/**********found**********/
    c[i]= ___(3)___;
}

void main()
{   char s[100]; /* 定义明文长度 */
    char c[100]; /* 定义密文长度 */
    int k;
    printf("Please input Secret Key:\n");
    scanf("%d", &k);getchar();
    printf("Please input Plaintext:\n");
    gets(s);
    fun(s,c,k);
    printf("The Ciphertext is : \n%s\n", c);
    getchar();
}

解析

'\0' s[i] '\0'

分析

第六十四题

题目

在给定程序BLANK1.C中,函数fun的功能是删除形参s所指的字符串中末尾的所有空格请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。

#include<stdio.h>
#include <string.h>
void fun(char *s)
{
	int n;
 	char *p;
	n=strlen(s);
 	if(n>0)
 	{
/**********found**********/
	 	p=____(1)____;
	 	while(*p==' ')
/**********found**********/
			____(2)____;
/**********found**********/
		*(p+1)= ____(3)____;
	}
 } 
int main()
{ 
	char s[100];
	gets(s);
	fun(s);
	printf("%s#\n",s);
	return 0;
}

解析

s+n-1 p--  '\0'

分析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值