您的位置:首页 > 理论基础 > 数据结构算法

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

2021-09-29 18:05 836 查看

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

 

【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;
}

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: