数据结构与算法

专有名词定义

1.全排列,又称为全排序
eg:1-3的全排序
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
共321=6种。

2.回文----顺读,逆读均相同

3.素数,也叫做质数。质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。

部分代码如下:

https://blog.csdn.net/oax_knud/article/details/129419727

华科大历年机考题:

https://wenku.baidu.com/view/dbd7452ffc00bed5b9f3f90f76c66137ee064f10.html?wkts=1679476079238&bdQuery=%E8%80%83%E7%A0%94%E5%A4%8D%E8%AF%95%E6%9C%BA%E8%AF%95%E9%A2%98

题目:
输入学生信息:学号,三门课程的成绩,学号为0时结束,将其存储在链表A中,从中找出分数大于平均分的学生,并将该学生信息按平均分降序排列存入到链表B中,最后输出链表B。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
    {char xuehao[20];
	int chengji[3];
	float av;
	struct node *next;
    }stud,*UerInfo; 
int main()
    {
      UerInfo ui;
      ui=(UerInfo)malloc(sizeof(stud));
      UerInfo p=ui;
      UerInfo q=ui;
      UerInfo tempB=ui;
      printf("input students' information:\n");
      int cnt=0;
      while (1)
      { 
         printf("input 学号:");
         scanf("%s",ui->xuehao);
         if(strcmp(ui->xuehao,"0")==0)
             break;
         printf("input 成绩:");
         scanf("%d",&ui->chengji[0]);
         printf("input 成绩:");
         scanf("%d",&ui->chengji[1]);
         printf("input 成绩:");
         scanf("%d",&ui->chengji[2]);
         ui->av=((ui->chengji[0]+ui->chengji[1]+ui->chengji[2])/3);
         ui->next=(UerInfo)malloc(sizeof(stud));
         ui=ui->next;
         cnt++;
      }
      int chengji1=0;
      int chengji2=0;
      int chengji3=0;
      while (p&&strcmp(p->xuehao,"0")!=0)
      {
      chengji1+=p->chengji[0];
      chengji2+=p->chengji[1];
      chengji3+=p->chengji[2];
      p=p->next;
      }
      float chengji1av=0.0;
      float chengji2av=0.0;
      float chengji3av=0.0;
      float avfinal=0.0;
      if(cnt)
          {
          chengji1av=(float)chengji1/(float)cnt;
          chengji2av=(float)chengji2/(float)cnt;
          chengji3av=(float)chengji3/(float)cnt;
          avfinal=(chengji1av+chengji2av+chengji3av)/3;
          }
      printf("高于平均分的有:\n");
      while (q&&strcmp(q->xuehao,"0")!=0)
          {
             if(q->av>avfinal)
                 {
                 printf("%s\n",q->xuehao);
                 printf("%f\n",q->av);
                 }
             q=q->next;
          }
      printf("\n降序排列如下:\n");
      UerInfo s;
      s=(UerInfo)malloc(cnt*sizeof(stud));
      int k=0;
      UerInfo temp=tempB;
      while (tempB&&strcmp(tempB->xuehao,"0")!=0)
      {
         s[k].av=tempB->av;
         s[k].chengji[0]=tempB->chengji[0];
         s[k].chengji[1]=tempB->chengji[1];
         s[k].chengji[2]=tempB->chengji[2];
         strcpy(s[k].xuehao,tempB->xuehao);
         tempB=tempB->next;
         k++;
      }
      int l,m;
      stud temps;
      for (l=0;l<cnt-1;l++)
      {
        for (m=l+1;m<cnt;m++)
        {
            if(s[l].av<s[m].av)
                {
                    temps.chengji[0]=s[l].chengji[0];
                    temps.chengji[1]=s[l].chengji[1];
                    temps.chengji[2]=s[l].chengji[2];
                    strcpy(temps.xuehao,s[l].xuehao);
                    s[l].chengji[0]=s[m].chengji[0];
                    s[l].chengji[1]=s[m].chengji[1];
                    s[l].chengji[2]=s[m].chengji[2];
                    strcpy(s[l].xuehao,s[m].xuehao);
                    s[m].chengji[0]=temps.chengji[0];
                    s[m].chengji[1]=temps.chengji[1];
                    s[m].chengji[2]=temps.chengji[2];
                    strcpy(s[m].xuehao,temps.xuehao);
                }
        }
      }
      for (int i=0;i<cnt;i++)
      {
      printf("学号:%s\n",s[i].xuehao);
      printf("成绩:%f\n",s[i].chengji[0]);
      printf("成绩:%f\n",s[i].chengji[1]);
      printf("成绩:%f\n",s[i].chengji[2]);
      }
      return 0;
    }

题目:编写程序判断输入的任意字符,任意长度的字符串是否回文(顺读,逆读均相同),最后输出原字符串和判断结果。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
void main() 
    { 
    int i; 
    int bIsSame = 1;//是否相同的标志
    int a=0,b; 
    char f [255]; 
    scanf("%s",f); //读入字符串
    b = strlen(f)-1; //获得最后一个字符的序号(字符串总长度减)
    for(i=0;i<b/2;i++) //循环,从到字符串中间的字符
        { 
        if(f[a++]!=f[b--]) //首尾比较是否相同字符
            { 
            bIsSame = 0; //有不同的就把标志置,并跳出循环
            break; 
            } 
        } 
    if(bIsSame) //相同就输出Y 
        printf ("Y"); 
    else //不同就输出N 
        printf ("N"); 
    getchar();//按任意键退出
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值