编程查找出长的最高和最矮的人的姓名和身高,并打印出来。

来源:百度知道 编辑:UC知道 时间:2024/05/31 12:51:57
从键盘输入20个人的姓名和身高,并存在结构数组中,编程查找出长的最高和最矮的人的姓名和身高,并打印出来。使用VC编辑

二分法需先排序,对于如此小的问题规模,应用穷举搜索
#include<iostream.h>
#include<string.h>

int j, k, b = 0, c = 10;

class person {
char *name;
int stature;
public:
void storage(char *s, int d) {
stature = d;
name = new char[sizeof(s)];
strcpy(name, s);
}
void print() {
cout<<"姓名:"<<name<<" 身高:"<<stature<<endl;
}
friend res(person a[]);
};

res(person a[]) {
for(int i = 0; i < 20; i++) {
if(a[i].stature > b) { b = a[i].stature; j = i;}
if(a[i].stature < c) { c = a[i].stature; k = i;}
}
}

void main() {
person a[20];
char s[20];
int d;
cout<<"输入20个人的姓名和身高:\n";
for(int i = 0; i < 20; i++) {
cin>>s>>d;
a[i].storage(s,d);
}
res(a);
cout<<"最高者:"; a[j].