早先年考研的主考科目正是【算法与数据结构】,复习得还算可以。也在当时[百度知道]上回答了许多相关问题,现把他们一起汇总整理一下,供读者参考。


【1】

原题目地址:https://zhidao.baidu.com/question/689185362502150884.html?entry=qb_uhome_tag

题目:

**那位大神能帮我讲解一下这个程序么,详细一点,谢谢 **

#include<iostream>
#include<string>
using namespace std; class person {
public:
void set_name(string s) {
name=s;
}//定义了一个名为set_name的返回类型为void的函数
void set_height(float a) {
height=a;
}
void set_age(int a) {
age=a;
}
string show_name() {
return name;
}
float show_height() {
return height;
}
int show_age() {
return age;
}
private:
string name;
float height;
int age;
};
person* join(int&num, person*p1) {
int n1=num, n2;
int i;
string name;
float height;
int age;
cout<<"要增加的人数是:"<<endl;
cin>>n2;//输入要添加的人数
system("cls");
num+=n2;
person *p2=new person[num];
for (i=0; i<n1; i++) {
*(p2+i)=*(p1+i);
}
for (i=n1; i<num; i++) {
system("cls");
person p;
cout<<"本次添加的第"<<i+1<<"个人的名字是:"<<endl;
cin>>name;
p.set_name(name);
cout<<"身高是:"<<endl;
cin>>height;
p.set_height(height);
cout<<"年龄是:"<<endl;
cin>>age;
p.set_age(age);
*(p2+i)=p;
}
system("cls");
delete[]p1;
return p2;
}
void sort_height(int num, person*p1) {
system("cls");
person p;
int i;
for (i=0; i<num; i++)
for (int j=i+1; j<num; j++)
if ((p1+i)->show_height()>(p1+j)->show_height()) {
p=*(p1+i);
*(p1+i)=*(p1+j);
*(p1+j)=p;
}
for (i=0; i<num; i++)
cout<<(p1+i)->show_name()<<"\t身高是:\t"<<(p1+i)->show_height()<<"\t年龄是:"<<(p1+i)->show_age()<<endl;
}
void find_name(int num, person*p1) {
system("cls");
string name;
int i;
cout<<"要查询的人的名字是:"<<endl;
cin>>name;
system("cls");
for (i=0; i<num; i++) {
if ((p1+i)->show_name()==name)
break;
}
if (i<num)
cout<<(p1+i)->show_name()<<"\t身高是:\t"<<(p1+i)->show_height()<<"\t年龄是:"<<(p1+i)->show_age()<<endl;
else
cout<<"查无此人!"<<endl;
}
int main() {
char c;
int num=0;
person*p=new person[0]; bool end=false;
cout<<"1.添加人员资料,2.按身高升序显示所有人资料,3.按姓名查询个人资料,4.退出"<<endl;
while (cin>>c) {
switch(c) {
case'1':
p=join(num, p);
break;
case'2':
sort_height(num, p);
break;
case'3':
find_name(num, p);
break;
case'4':
end=true;
break;
default:
cout<<"输入有误:\n";
}
if (end)
break;
cout<<"1.添加人员资料,2.按身高升序显示所有人资料,3.按姓名查询个人资料,4.退出"<<endl;
}
return 0;
}

答:

#include<iostream>
#include<string>
using namespace std; class person
{
public:
void set_name(string s){name=s;}//定义了一个名为set_name的返回类型为void的函数
void set_height(float a){height=a;}//把a值赋给height(因为类,一个封装概念,private里的变量不能再外部直接赋值)
void set_age(int a){age=a;} //同上!
string show_name(){return name;}//输出private里的name值
float show_height(){return height;}//同上!
int show_age(){return age;} //同上!
private: //私有成员
string name;
float height;
int age;
};
person * join(int &num,person *p1)//形参,当前人员资料数目和一个总地址,返回一个新的总地址
{
int n1=num,n2;//ni是存放当前人员资料数目,n2存放要添加的数目
int i;
string name;
float height;
int age;
cout<<"要增加的人数是:"<<endl;
cin>>n2;//输入要添加的人数
system("cls");//清屏(windows API 函数,用法和DOS里一样 -cls清屏) num+=n2; //存放添加后的数目
person *p2=new person[num];//开辟num个人员资料内存量
for(i=0;i<n1;i++)//把原有的放到新开辟的空间中
{
*(p2+i)=*(p1+i);//运用指针来赋值,相当于copy对象了
}
for(i=n1;i<num;i++) //添加新的人员资料
{
system("cls"); person p;//临时用的对象
cout<<"本次添加的第"<<i+1<<"个人的名字是:"<<endl;
cin>>name;
p.set_name(name);
cout<<"身高是:"<<endl;
cin>>height;
p.set_height(height);
cout<<"年龄是:"<<endl;
cin>>age;
p.set_age(age);
*(p2+i)=p;//相当于复制,赋给新的人员资料里
}
system("cls");
delete[]p1; //原来的总地址无效了,释放! return p2; //返回新地址!
}
void sort_height(int num,person*p1) //排序(形参 num,*p1 同上! )
{
system("cls");
person p;
int i;
for(i=0;i<num;i++) //冒泡排序!(经典,必须掌握!)--共反复执行num次
for(int j=i+1;j<num;j++)//将接下来的值逐个和上一个值进行比较
if((p1+i)->show_height()>(p1+j)->show_height()) //倘若满足上一个比这个大
{
p=*(p1+i); //交换!结果变成升序排列了!
*(p1+i)=*(p1+j);
*(p1+j)=p;
}
for(i=0;i<num;i++) //显示所有
cout<<(p1+i)->show_name()<<"\t身高是:\t"<<(p1+i)->show_height()<<"\t年龄是:"<<(p1+i)->show_age()<<endl;
}
void find_name(int num,person*p1)//寻找name(形参 同上!)
{
system("cls");
string name;//临时一用
int i;
cout<<"要查询的人的名字是:"<<endl;
cin>>name;
system("cls");
for(i=0;i<num;i++)
{
if((p1+i)->show_name()==name)//如果找到一样的名字,则退出循环,倒置i的值比num小,如果没找到,导致i=num
break;
}
if(i<num) //比较i,num值,如果成立,说明找到对应的,输出!
cout<<(p1+i)->show_name()<<"\t身高是:\t"<<(p1+i)->show_height()<<"\t年龄是:"<<(p1+i)->show_age()<<endl;
else //找不到,显示。。。
cout<<"查无此人!"<<endl;
}
int main()
{
char c; //作为待输入字符的变量
int num = 0;//目前人员资料数
person *p = new person[0];//开辟了person类的内存空间(暂且没有)并新建一个相应指针指向它,作为以后的总地址
bool end = false;//一个开关变量(false-不关 true-关闭)
cout<<"1.添加人员资料,2.按身高升序显示所有人资料,3.按姓名查询个人资料,4.退出"<<endl;
while(cin>>c) //待用户输入值
{
switch(c) //检测
{
case'1':p=join(num,p);break;//如果是输入1。。。
case'2':sort_height(num,p);break;//如果是输入2。。。
case'3':find_name(num,p);break; //如果是输入3。。。
case'4':end=true;break; //如果是输入4。。。
default:cout<<"输入有误:\n";
}
if(end) //检测是否关闭,如果没有按4,则不退出循环,继续检测输入值!
break; //退出循环,倒置return 0 ,返回,终结程序!
cout<<"1.添加人员资料,2.按身高升序显示所有人资料,3.按姓名查询个人资料,4.退出"<<endl;
}
return 0;
}

【2】

原题目地址:https://zhidao.baidu.com/question/1367581282473175579.html?entry=qb_uhome_tag

题目:

大一C语言问题,有关结构体。。。

定义结构体teleno,包含两个成员:姓名,电话

定义一个查找某人电话号码的函数。

编写程序,输入若干人员的姓名和电话,以“#”结束,并查找某人的电话号。

答:

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <windows.h> struct teleno
{
char name[10];
int number;
}; struct teleno * Init(struct teleno *);
void Find_num(struct teleno *);
bool Is_same(char *,char *); struct teleno * Init(struct teleno *a)
{
int i;
struct teleno *p;
for(i=0; i<5; i++)
{
p = (struct teleno *)malloc(sizeof(struct teleno));
printf("\n----%d----\n",i+1);
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入号码:");
scanf("%d",&p->number); a[i] = *p;
}
return a;
} void Find_num(struct teleno *a)
{
int i,t = 0;
char Name[10];
char *p = Name;
char word;
printf("\n请输入查找人姓名:");
while((word = getchar()) != '#')
*p++ = word;
*p = '\0';
/*while(1)
{
if((word = getchar()) == '#')
break;
*p++ = word;
}
for(i=0; i<5; i++)
{
if(Is_same(Name,a->name))
{
printf("%s:",a->name);
printf("%d\n",a->number);
t = 1;
}
*a++;
}
if(t == 0)
printf("Sorry!未找到相应号码!\n");
return;
} bool Is_same(char *p1,char *p2)
{
int i = 0,j,k;
p1++;
j = strlen(p2);
k = strlen(p1);
if(j != k)
return false; while((*p1 != '\0')||(*p2 != '\0'))
{
if(*p1 == *p2)
i++;
p1++;
p2++;
}
if(i == j)
return true;
else
return false;
} int main()
{
teleno a[5];
struct teleno *p = a;
p = Init(p);
Find_num(a); system("pause");
return 0;
}

【PS】根据您说的,我做了一下,可能不符合您的意思,我没太读清题意:

        1.是不是再导入姓名和号码时是#结束,然后输入姓名查号码?

        2.是不是在查询时输入参考姓名是以#结束?

【3】

原题目地址:https://zhidao.baidu.com/question/177059621291198124.html?entry=qb_uhome_tag

题目:

C语言编程第9题求解大神

答:

#include <stdio.h>
#include <windows.h> void Convert(int a[][3])
{
int i,j,t;
for(i=0; i<3; i++)
{
for(j=i+1; j<3 ;j++)
{
t = a[i][j];
a[i][j] = a[j][i];
a[j][i] = t;
}
}
return;
} int main()
{
int i,j;
int a[3][3];
for(i=0; i<3; i++)
for(j=0; j<3; j++)
scanf("%d",&a[i][j]); for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",a[i][j]);
}
putchar(10);
} printf("convert...\n");
Convert(a); for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",a[i][j]);
}
putchar(10);
} system("pause");
return 0;
}

追问:

答:

void Convert(int (*p)[3])
{
int i,j,t;
for(i=0; i<3; i++)
{
for(j=i+1; j<3 ;j++)
{
t = *(*(p+i)+j);
*(*(p+i)+j) = *(*(p+j)+i);
*(*(p+j)+i) = t;
}
}
return;
}

我明白您的意思了!楼上的朋友是对的!你也可以看我的这种写法!也是行得通的!

有关算法与数据结构的考题解答参考汇总 [C++] [链表] · 第三篇的更多相关文章

  1. 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)

    温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...

  2. Linux内核中的算法和数据结构

    算法和数据结构纷繁复杂,但是对于Linux Kernel开发人员来说重点了解Linux内核中使用到的算法和数据结构很有必要. 在一个国外问答平台stackexchange.com的Theoretica ...

  3. 程序员代码面试指南 IT名企算法与数据结构题目最优解

    原文链接 这是一本程序员面试宝典!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现.针对当前程序员面试缺乏权威题目汇总这一痛点,本书选取将近200道真实出现过的经典代码面试题,帮 ...

  4. 算法、数据结构、与设计模式等在游戏开发中的运用 (一):单例设计(Singleton Design)

    算法.数据结构.与设计模式等在游戏开发中的运用 (一):单例设计(Singleton Design) 作者: Compasslg 李涵威 1. 什么是单例设计(Singleton Design) 在学 ...

  5. 浅谈算法和数据结构: 七 二叉查找树 八 平衡查找树之2-3树 九 平衡查找树之红黑树 十 平衡查找树之B树

    http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html 前文介绍了符号表的两种实现,无序链表和有序数组,无序链表在插入的 ...

  6. python 下的数据结构与算法---2:大O符号与常用算法和数据结构的复杂度速查表

    目录: 一:大O记法 二:各函数高阶比较 三:常用算法和数据结构的复杂度速查表 四:常见的logn是怎么来的 一:大O记法 算法复杂度记法有很多种,其中最常用的就是Big O notation(大O记 ...

  7. LeetCode_算法及数据结构覆盖统计

    [输入]共计151道题的算法&数据结构基础数据 (见附录A) [输出-算法]其中有算法记录的共计 97道 ,统计后 结果如下  top3(递归,动态规划,回溯) 递归 动态规划 回溯 BFS ...

  8. JavaScript算法与数据结构知识点记录

    JavaScript算法与数据结构知识点记录 zhanweifu

  9. JS中的算法与数据结构——排序(Sort)(转)

    排序算法(Sort) 引言 我们平时对计算机中存储的数据执行的两种最常见的操作就是排序和查找,对于计算机的排序和查找的研究,自计算机诞生以来就没有停止过.如今又是大数据,云计算的时代,对数据的排序和查 ...

  10. Python算法与数据结构--求所有子数组的和的最大值

    Python算法与数据结构--求所有子数组的和的最大值 玄魂工作室-玄魂 玄魂工作室秘书 玄魂工作室 昨天 题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个 ...

随机推荐

  1. 【Amadeus原创】Docker安装wikijs wiki系统

    拉取mysql8的镜像并运行 docker pull mysql docker run -d -v /data/mysql/data:/var/lib/mysql -v /data/mysql/con ...

  2. 【Amadeus原创】查找docker里程序源代码路径 上传本地文件

    1. 先找到container id ,诸如,我要进wikijs. id为3f6d2092f6ba docker ps 2. docker exec 进入container里面 docker exec ...

  3. R数据分析:临床预测模型实操,校准曲线和DCA曲线做法示例

    之前给大家写过好几篇很详细的临床预测模型的原理解析,本文接着之前的文章,继续写做法,首先依然是找到一篇参照论文,今天我们的示例文章是一篇来自美国心脏学会杂志的文章: Zhang X, Yuan K, ...

  4. 中电金信:亚洲TOP1 霸榜15年

    近日,国际权威语言服务研究机构CSA Research公布了<2022年全球语言服务提供商100强>和<亚太地区TOP 30语言服务商>排名报告. 中电金信凭借卓越的品质管控. ...

  5. Springboot使用mongodb遇到问题及解决

    网上看到使用mongodb好像很简单,没有什么问题,可我一用就怎么都连不上,先看看我的配置 在pom.xml中添加依赖 1234 <dependency>    <groupId&g ...

  6. Qt编写的项目作品31-PDF阅读器(雨田哥作品)

    一.功能特点 仿WPS界面. 预览PDF文件. 支持PDF预览放大.缩小. 支持目录预览查看. 支持目录点击跳转页查看. 支持页数指定跳转. 支持上一页.下一页.首页.尾页跳转. 支持鼠标拖拽滑动预览 ...

  7. Kernel Memory 让 SK 记住更多内容

    Kernel Memory (KM) 是一种多模态 AI 服务,专注于通过自定义的连续数据混合管道高效索引数据集.它支持检索增强生成(RAG).合成记忆.提示工程以及自定义语义记忆处理.KM 支持自然 ...

  8. 《SpringBoot》自动装配原理(简单易懂)

    引入 先看SpringBoot的主配置类 @SpringBootApplication public class DemoApplication{ public static void main(St ...

  9. 2025春秋杯DAY2DAY3部分wp

    2025春秋杯DAY2DAY3部分wp DAY2 WEB easy_ser 源码如下 <?php //error_reporting(0); function PassWAF1($data){ ...

  10. 【异步编程实战】如何实现超时功能(以CompletableFuture为例)

    [异步编程实战]如何实现超时功能(以CompletableFuture为例) 由于网络波动或者连接节点下线等种种问题,对于大多数网络异步任务的执行通常会进行超时限制,在异步编程中是一个常见的问题.本文 ...