1. 程式人生 > 其它 >有關演算法與資料結構的考題解答參考彙總 [C++] [連結串列] · 第三篇

有關演算法與資料結構的考題解答參考彙總 [C++] [連結串列] · 第三篇

早先年考研的主考科目正是【演算法與資料結構】,複習得還算可以。也在當時[百度知道]上回答了許多相關問題,現把他們一起彙總整理一下,供讀者參考。


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

我明白您的意思了!樓上的朋友是對的!你也可以看我的這種寫法!也是行得通的!