程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-05(1)

[NEFU大一下C语言 期末模拟]参考答案

发布于2021-05-30 20:10     阅读(412)     评论(0)     点赞(14)     收藏(4)


[NEFU大一下C语言 期末模拟]参考答案

模拟又又又来啦,但是感觉每次模拟都比考试难awa
有几道是做过的,就不讲了
唯一有点说头的是链表那题,建议直接拉到底

感谢潘航巨佬光速A题后保留了题目截图

题目

在这里插入图片描述

结构体基本操作

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 25
typedef struct Student{
	double a,b,c,sum,ave;
}Student;
Student stu[N];
int main(){
	int n;
	while(~scanf("%d",&n)){
		double math=0,eng=0,phy=0;
		for(int i=1;i<=n;i++){
			scanf("%lf%lf%lf",&stu[i].a,&stu[i].b,&stu[i].c);
			stu[i].sum=stu[i].a+stu[i].b+stu[i].c;
			stu[i].ave=stu[i].sum/3;
			math+=stu[i].a;eng+=stu[i].b;phy+=stu[i].c;
			printf("%.2lf %.2lf\n",stu[i].sum,stu[i].ave);
		}
		printf("%.2lf %.2lf\n",math,math/n);
		printf("%.2lf %.2lf\n",eng,eng/n);
		printf("%.2lf %.2lf\n",phy,phy/n);
	}
	return 0;
}

在这里插入图片描述

二维数组指针做函数参数

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 25
/*double (*p)[N]和double p[][N]等效,input和search用了不同写法*/
void input(double (*p)[N],int n,int m){
	for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				scanf("%lf",&p[i][j]);
}
void search(double p[][N],int n,int m){
	double max=p[0][0];int x=1,y=1;
	for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if(max<p[i][j]){
					max=p[i][j];
					x=i+1,y=j+1;
				}
	printf("%.2lf %d %d\n",max,x,y);
}
int main(){
	int n,m;
	double g[N][N];
	while(~scanf("%d%d",&n,&m)){
		input(g,n,m);
		search(g,n,m);
	}	
	return 0;
}

###

做过不讲,这种字符串分词处理的,个人觉得,一个个处理,比较舒服。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define N 1005

void solve(char *p)
{
	int cnt=0;
	bool flag=0;
	while(*p!='\0')
	{
		if(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z')
		{
			printf("%c",*p);
			flag=1;
		}
		else if(flag)//加个flag主要防止连续空格的毒瘤数据
		{
			printf(" ");
			cnt++;
			flag=0;
		}
		++p;
	}
	if(cnt)printf("\n分出了%d个单词\n",cnt);
} 
int main()
{
	int n;
	char str[N];
	char *p=str;
	while(scanf("%d ",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			gets(p);
			solve(p);
		}		
	}
	return 0;
} 

###

做过不讲

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort1(char **pp,int n)
{
    //补充完成程序
    char tmp[100];
	for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
			if(strcmp(pp[i],pp[j])>0)
			{
				strcpy(tmp,pp[i]);
				strcpy(pp[i],pp[j]);
				strcpy(pp[j],tmp);
			}
}
void output1(char *p[],int n)
{
 for(int i=0;i<n;i++)printf("%s\n",p[i]);  
}
int main()
{
    void sort1(char **pp,int n);
    void output1(char *p[],int n);
    char str[50],*cp[20];
    int n,l,i;
    while(scanf("%d\n",&n)!=EOF)
    {
          for(i=0;i<n;i++)
          {
              gets(str);
             //start
            	int len=strlen(str);
            	cp[i]=(char*)malloc(sizeof(char)*(len+1));
            	strcpy(cp[i],str);
             //end
           }
           sort1(cp,n);
           output1(cp,n);
    }
    return 0;
}

在这里插入图片描述

结构体基操

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 25
struct girl{
	int code;
	char name[30];
	int age;
	float tall;
};
struct girl g[N];
int main(){
	int n;
	while(~scanf("%d",&n)){
		
		double ave=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d %s %d %f",&g[i].code,&g[i].name,&g[i].age,&g[i].tall);
			ave+=g[i].tall;
		}
		ave/=n;
		int cnt=0;
		for(int i=0;i<n;i++)
			if(g[i].age>=20&&g[i].age<=25&&g[i].tall>ave)
			{
				++cnt;
				printf("%d %s\n",g[i].code,g[i].name);
			}
		printf("%d\n",cnt);
	}
	return 0;
}

###

大概是唯一值得讲一下的题目?
和平常自己写链表方式不太一样,刚开始挺蒙的hh,不过嘛冷静冷静
这个insert操作本质是啥嘞,是个search,既然要搜索,我们就要遍历。
题目代码的output其实已经告诉你怎么遍历了,你甚至不用看这个链表是怎么构建的hh。
直接复制output代码框架,你就得到了遍历这个链表的方式.

  Link_Node *p;
    p=head;
    while(p!=NULL && p->next!=NULL)
     {
       p=p->next;
       
     }

然后根据题目要求,先找到最大素数,然后记录下编号
吐槽一下这个su函数的返回值居然是素数返回0,不是素数返回1反人类啊啊啊
然后再遍历一波找到那个编号的位置p以及它的前驱位置pre
然后欢乐插入就可以啦!

当然代码实现部分可能有些冗余,见谅
蒟蒻鱼竿的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sb
{
int no;//编号 
struct sb *next;
};
typedef struct sb Link_Node;
void input(Link_Node *p)
{
  scanf("%d",&p->no);
  p->next=NULL;
}
Link_Node * create_list(int n)
{ Link_Node *head,* p,* q;
  int i;
   head=q=(Link_Node *)malloc(sizeof(Link_Node));
   if(head!=NULL)
    {
     head->next=NULL;
     for(i=0;i<n;i++)
      { p=(Link_Node *)malloc(sizeof(Link_Node));
        input(p);
        q->next=p;
        q=q->next;
      }
    }
   return head;
}
void output(Link_Node *head)
{  int j;
    Link_Node *p;
    p=head;
    while(p!=NULL && p->next!=NULL)
     {
       p=p->next;
       printf("%d ",p->no);
     }
     printf("\n");
}
int su(int n) //素数
{int i,flag=0;
  for(i=2;i<=n/2;i++)
    if (n%i==0) {flag=1;break;}
  if (n==1) flag=1;
  return flag;
}
void insert(Link_Node *head,int m)
{ 
//start
	Link_Node *p,* idx,* pre;
    p=head;
    int id=-1;
    idx=(Link_Node*)malloc(sizeof(Link_Node));
    idx->no=m;
    while(p!=NULL && p->next!=NULL)//找no
     {
       p=p->next;
       if(!su(p->no)&&(p->no)>id)id=p->no;
     }
    p=head,pre=head;//p别忘了初始化head
    while(p!=NULL && p->next!=NULL&&p->no!=id)
     {
       pre=p;
       p=p->next;
     }
     idx->next=p;
     pre->next=idx;
 //end 
}
int main()
{
  Link_Node *head;
  int n,m;
  while(scanf("%d%d",&n,&m)!=EOF)
   {
     head=create_list(n);
     insert(head,m);
     output(head);
     free(head);
   }
    return 0;
}

潘航巨佬的代码%%%orz

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sb
{
int no;//编号 
struct sb *next;
};
typedef struct sb Link_Node;
void input(Link_Node *p)
{
  scanf("%d",&p->no);
  p->next=NULL;
}
Link_Node * create_list(int n)
{ Link_Node *head,* p,* q;
  int i;
   head=q=(Link_Node *)malloc(sizeof(Link_Node));
   if(head!=NULL)
    {
     head->next=NULL;
     for(i=0;i<n;i++)
      { p=(Link_Node *)malloc(sizeof(Link_Node));
        input(p);
        q->next=p;
        q=q->next;
      }
    }
   return head;
}
void output(Link_Node *head)
{  int j;
    Link_Node *p;
    p=head;
    while(p!=NULL && p->next!=NULL)
     {
       p=p->next;
       printf("%d ",p->no);
     }
     printf("\n");
}
int su(int n) //素数
{int i,flag=0;
  for(i=2;i<=n/2;i++)
    if (n%i==0) {flag=1;break;}
  if (n==1) flag=1;
  return flag;
}
void insert(Link_Node *head,int m)
{ 
//start
    Link_Node ** FINAL = &(head -> next);
    int maxx = -1;
    while(head -> next != NULL){
        Link_Node * tmp = head -> next;
        if(!su(tmp -> no)){
            if(tmp -> no > maxx){
                FINAL = &(head -> next);
                maxx = tmp -> no;
            }
        }
        head = head -> next;
    }
    Link_Node * tmp = *FINAL;
    *FINAL = (Link_Node *)malloc(sizeof(Link_Node));
    (*FINAL) -> next = tmp;
    (*FINAL) -> no = m;
 //end 
}
int main()
{
  Link_Node *head;
  int n,m;
  while(scanf("%d%d",&n,&m)!=EOF)
   {
     head=create_list(n);
     insert(head,m);
     output(head);
     free(head);
   }
    return 0;
}

原文链接:https://blog.csdn.net/qq_39354847/article/details/117372132



所属网站分类: 技术文章 > 博客

作者:来呀来呀开心吧

链接:http://www.phpheidong.com/blog/article/86853/5b680ec9a6115d150b18/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

14 0
收藏该文
已收藏

评论内容:(最多支持255个字符)