两道面试题,请大神指教

rophie 2012-09-08 10:26:24
1、有一组学生信息数据,其中学生的信息是:姓名,学号,性别,成绩等信息,设计合适的数据结构,把这些学生信息数据表示出来,(1)对该组学生信息数据按成绩高低进行排序; (2)给出一个value,输出该值在学生信息数据中的成绩排名情况(成绩相同,为并列排名,例如97分为第二高分,其中有3名97分,这3名学生排名都为第二)及成绩高于该value的学生人数。

2、现在有0.png,1.png,2.png,3.png,4.png 5张图片,大小为16*16mm,分别用0,1,2,3,4数值来表示,现在用这些张小图片拼成了一个160mm*160mm地图,拼接后的图例short[][] mapData=new short[10][10] 的数据值为:
0 1 1 2 3 1 1 2 3 4
5 3 1 2 4 3 0 1 1 2
2 2 2 2 4 1 2 1 2 1
1 1 1 4 2 4 2 2 0 2
3 1 4 1 2 2 2 4 0 0
4 4 1 1 2 0 1 4 0 1
4 1 1 0 2 4 0 0 3 0
2 0 0 2 2 4 2 2 4 4
2 0 2 2 1 0 4 3 1 0
2 0 0 4 1 1 2 3 3 1
请代码绘制出该图例,绘制图片的方法为DrawImage(image,x,y)—其中x,y值为将该image对象绘制到x,y点;创建Image对象的方法为Image image0=new Image(“0.png”)。
...全文
212 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
rophie 2012-09-08
  • 打赏
  • 举报
回复
不会……
ryxjxyx 2012-09-08
  • 打赏
  • 举报
回复
关注下面试
AndyZhang 2012-09-08
  • 打赏
  • 举报
回复
抽时间先写
rophie 2012-09-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

第一题 简单吧
[/Quote]

求完美代码参考
Ydc__ 2012-09-08
  • 打赏
  • 举报
回复
第一题 简单吧
lurenceGu 2012-09-08
  • 打赏
  • 举报
回复
1、有一组学生信息数据,其中学生的信息是:姓名,学号,性别,成绩等信息,设计合适的数据结构,把这些学生信息数据表示出来,(1)对该组学生信息数据按成绩高低进行排序; (2)给出一个value,输出该值在学生信息数据中的成绩排名情况(成绩相同,为并列排名,例如97分为第二高分,其中有3名97分,这3名学生排名都为第二)及成绩高于该value的学生人数。


struct Student_ST
{
int m_iIndex;
int m_iPoint;
int m_iID;
char m_pszName[];
char m_cSex;
int m_iCore;
}

template <typename T,int iSize>
class POOL_ST
{
enum {POOL_SIZE=Index,}
public:
POOL_ST(){}
~POOL_ST(){}
private:
T *pList;
public:
void InitPool(){pList=new T[POOL_SIZE];Assert(pList);memset(pList,0,sizeof(T)*POOL_SIZE;}
int AddItem(const T &)
{
// 用二分查找 (注意前后相同 则他们的Index也相同)
// To Do ... .. .
// 找到之后后面往后面移动m_iPoint都加1
// 设置位置信息Index
// 数据memcpy拷贝过来(也可以用存放指针的方式,需要在外面new好)
// 返回位置
// To Do ... .. .
}
T &GetItem()
{
//
}
}


2、现在有0.png,1.png,2.png,3.png,4.png 5张图片,大小为16*16mm,分别用0,1,2,3,4数值来表示,现在用这些张小图片拼成了一个160mm*160mm地图,拼接后的图例short[][] mapData=new short[10][10] 的数据值为:
0 1 1 2 3 1 1 2 3 4
5 3 1 2 4 3 0 1 1 2
2 2 2 2 4 1 2 1 2 1
1 1 1 4 2 4 2 2 0 2
3 1 4 1 2 2 2 4 0 0
4 4 1 1 2 0 1 4 0 1
4 1 1 0 2 4 0 0 3 0
2 0 0 2 2 4 2 2 4 4
2 0 2 2 1 0 4 3 1 0
2 0 0 4 1 1 2 3 3 1
请代码绘制出该图例,绘制图片的方法为DrawImage(image,x,y)—其中x,y值为将该image对象绘制到x,y点;创建Image对象的方法为Image image0=new Image(“0.png”)。

这个也简单 主要是结构算法分离 且结构清晰
stereoMatching 2012-09-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]
第二题

(用的不是 c++ 。 三行代码)
[/Quote]
请问怎么做到的?

studentData可以引入move constructor和move assignment,效率可能会比较好一些

struct studentData
{
studentData() {}

studentData(studentData &&data)
{
*this = std::move(data);
}

studentData& operator=(studentData &&data)
{
if(this != &data)
{
name_.swap(data.name_);
score_ = data.score_;
sex_.swap(data.sex_);
student_number_.swap(data.student_number_);
}

return *this;
}

studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}

std::string name_;

size_t score_;
std::string sex_;
std::string student_number_;
};
CandPointer 2012-09-08
  • 打赏
  • 举报
回复

第二题

(用的不是 c++ 。 三行代码)

stereoMatching 2012-09-08
  • 打赏
  • 举报
回复
vc2010,希望楼主可以把自己的答案贴上来,大家一起学习,交流


#ifndef INTERVIEW_00_HPP
#define INTERVIEW_00_HPP

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

struct studentData
{
studentData() {}

studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}

std::string name_;

size_t score_;
std::string sex_;
std::string student_number_;
};

std::ostream& operator<<(std::ostream &out, studentData const &data)
{
out << "score : " << data.score_ << std::endl;
out << "name : " << data.name_ << std::endl;
out << "student number : " << data.student_number_ << std::endl;
out << "sex : " << data.sex_ << std::endl;

return out;
}

bool operator==(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ == rhs.score_;
}

bool operator<(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ < rhs.score_;
}

class studentManager
{
public:
studentManager() {}

void add_student(studentData const &data)
{
students_.push_back(data);
}

void print_data()
{
std::for_each(std::begin(students_), std::end(students_),
[&](studentData const &fir)
{
std::cout<<fir<<std::endl;
} );
}

size_t ranking(size_t value)
{
auto const result = std::equal_range(std::begin(ranking_), std::end(ranking_), value);
if(result.first != result.second)
return ranking_.size() - (result.first - std::begin(ranking_) );

return 0;
}

void sort_by_score()
{
if(!students_.empty())
{
std::sort(std::begin(students_), std::end(students_) );

ranking_.clear();
std::for_each(std::begin(students_), std::end(students_),
[&](studentData const &fir)
{
ranking_.push_back(fir.score_);
} );

auto const it = std::unique(std::begin(ranking_), std::end(ranking_));
ranking_.erase(it, std::end(ranking_));
}
}

private:
studentManager(studentManager const&);
studentManager& operator=(studentManager const&);

private:
std::vector<size_t> ranking_;
std::vector<studentData> students_;
};

inline void test_student_manager()
{
studentManager manager;
manager.add_student(studentData("one", 99, "male", "1"));
manager.add_student(studentData("two", 99, "male", "2"));
manager.add_student(studentData("three", 100, "male", "3"));
manager.add_student(studentData("four", 77, "male", "4"));
manager.add_student(studentData("five", 88, "male", "5"));
manager.add_student(studentData("six", 88, "male", "6"));

manager.sort_by_score();

manager.print_data();

std::cout<< manager.ranking(100) << std::endl;
std::cout<< manager.ranking(99) << std::endl;
std::cout<< manager.ranking(88) << std::endl;
std::cout<< manager.ranking(77) << std::endl;
}

#endif // INTERVIEW_00_HPP

rophie 2012-09-08
  • 打赏
  • 举报
回复
能不能用 MSVC啊……好多不理解啊
stereoMatching 2012-09-08
  • 打赏
  • 举报
回复
这是用mingw4.6.2写的,defaulted function这个特性vc10还不支援
你可以把它换成

studentManager() {}


请问原po是面试那家公司?
rophie 2012-09-08
  • 打赏
  • 举报
回复
studentManager() = default;这句不理解,vs报错啊
stereoMatching 2012-09-08
  • 打赏
  • 举报
回复
第二题的pseudo codes(我用size_t代替image)

#ifndef INTERVIEW_01_HPP
#define INTERVIEW_01_HPP

#include <iostream>

inline void DrawImage(size_t image, size_t x, size_t y)
{
//std::cout << "(" << x << ", " << y << ") : " << image;
std::cout << image;
}

inline void test_draw_image()
{
size_t image[6] = {0, 1, 2, 3, 4, 5};

size_t map[10][10] =
{
{0, 1, 1, 2, 3, 1, 1, 2, 3, 4},
{5, 3, 1, 2, 4, 3, 0, 1, 1, 2},
{2, 2, 2, 2, 4, 1, 2, 1, 2, 1},
{1, 1, 1, 4, 2, 4, 2, 2, 0, 2},
{3, 1, 4, 1, 2, 2, 2, 4, 0, 0},
{4, 4, 1, 1, 2, 0, 1, 4, 0, 1},
{4, 1, 1, 0, 2, 4, 0, 0, 3, 0},
{2, 0, 0, 2, 2, 4, 2, 2, 4, 4},
{2, 0, 2, 2, 1, 0, 4, 3, 1, 0},
{2, 0, 0, 4, 1, 1, 2, 3, 3, 1}
};

for(int x = 0; x != 10; ++x)
{
for(int y = 0; y != 10; ++y)
{
DrawImage(image[map[x][y]], x, y);
}
std::cout << std::endl;
}
}

#endif // INTERVIEW_01_HPP

stereoMatching 2012-09-08
  • 打赏
  • 举报
回复
抱歉,符号overload哪里写得很糟糕,修正版
贪快没想清楚就写下去了Orz

#ifndef INTERVIEW_00_HPP
#define INTERVIEW_00_HPP

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

struct studentData
{
studentData() = default;

studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}

std::string name_;

size_t score_;
std::string sex_;
std::string student_number_;
};

std::ostream& operator<<(std::ostream &out, studentData const &data)
{
out << "score : " << data.score_ << std::endl;
out << "name : " << data.name_ << std::endl;
out << "student number : " << data.student_number_ << std::endl;
out << "sex : " << data.sex_ << std::endl;

return out;
}

bool operator==(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ == rhs.score_;
}

bool operator<(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ < rhs.score_;
}

class studentManager
{
public:
studentManager() = default;
studentManager(studentManager const&) = delete;
studentManager& operator=(studentManager const&) = delete;

void add_student(studentData const &data)
{
students_.push_back(data);
}

void print_data()
{
for(studentData const &data : students_)
{
std::cout<< data << std::endl;
}
}

size_t ranking(size_t value)
{
auto const result = std::equal_range(std::begin(ranking_), std::end(ranking_), value);
if(result.first != result.second)
return ranking_.size() - (result.first - std::begin(ranking_) );

return 0;
}

void sort_by_score()
{
std::sort(std::begin(students_), std::end(students_) );

ranking_.clear();
for(studentData const &data : students_)
ranking_.push_back(data.score_);

auto it = std::unique(std::begin(ranking_), std::end(ranking_));
ranking_.erase(it, std::end(ranking_));

//for(size_t data : ranking_) std::cout << data << std::endl;
}

private:
std::vector<size_t> ranking_;
std::vector<studentData> students_;
};

inline void test_student_manager()
{
studentManager manager;
manager.add_student(studentData("one", 99, "male", "1"));
manager.add_student(studentData("two", 99, "male", "2"));
manager.add_student(studentData("three", 100, "male", "3"));
manager.add_student(studentData("four", 77, "male", "4"));
manager.add_student(studentData("five", 88, "male", "5"));
manager.add_student(studentData("six", 88, "male", "6"));

manager.sort_by_score();

manager.print_data();

std::cout<< manager.ranking(100) << std::endl;
std::cout<< manager.ranking(99) << std::endl;
std::cout<< manager.ranking(88) << std::endl;
std::cout<< manager.ranking(77) << std::endl;
}

#endif // INTERVIEW_00_HPP



第二题我稍后再补上
stereoMatching 2012-09-08
  • 打赏
  • 举报
回复
第一题,非完美代码,应该符合要求
全部宣告成inline单纯是因为懒,非良好习惯,莫学
这题的解法满多的,更懒一点的话也可以用std::map配合tuple进行封装

#ifndef INTERVIEW_00_HPP
#define INTERVIEW_00_HPP

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

struct studentData
{
studentData() = default;

studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}

std::string name_;

size_t score_;
std::string sex_;
std::string student_number_;
};

std::ostream& operator<<(std::ostream &out, studentData const &data)
{
out << "score : " << data.score_ << std::endl;
out << "name : " << data.name_ << std::endl;
out << "student number : " << data.student_number_ << std::endl;
out << "sex : " << data.sex_ << std::endl;

return out;
}

bool operator==(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ == rhs.score_;
}

bool operator<(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ > rhs.score_;
}

class studentManager
{
public:
studentManager() = default;
studentManager(studentManager const&) = delete;
studentManager& operator=(studentManager const&) = delete;

void add_student(studentData const &data)
{
students_.push_back(data);
}

void print_data()
{
for(studentData const &data : students_)
{
std::cout<< data << std::endl;
}
}

size_t ranking(size_t value)
{
auto const result = std::equal_range(std::begin(ranking_), std::end(ranking_),
value, std::greater<size_t>() );
if(result.first != result.second)
return result.first - std::begin(ranking_) + 1;

return 0;
}

void sort_by_score()
{
std::sort(std::begin(students_), std::end(students_) );

ranking_.clear();
for(studentData const &data : students_)
ranking_.push_back(data.score_);

auto it = std::unique(std::begin(ranking_), std::end(ranking_));
ranking_.erase(it, std::end(ranking_));

//for(size_t data : ranking_) std::cout << data << std::endl;
}

private:
std::vector<size_t> ranking_;
std::vector<studentData> students_;
};

inline void test_student_manager()
{
studentManager manager;
manager.add_student(studentData("one", 99, "male", "1"));
manager.add_student(studentData("two", 99, "male", "2"));
manager.add_student(studentData("three", 100, "male", "3"));
manager.add_student(studentData("four", 77, "male", "4"));
manager.add_student(studentData("five", 88, "male", "5"));
manager.add_student(studentData("six", 88, "male", "6"));

manager.sort_by_score();

manager.print_data();

std::cout<< manager.ranking(100) << std::endl;
std::cout<< manager.ranking(99) << std::endl;
std::cout<< manager.ranking(88) << std::endl;
std::cout<< manager.ranking(77) << std::endl;
}

#endif // INTERVIEW_00_HPP



第二题的阵列宣告方法不太像是C++的?
我看不出那个阵列的排列规则
只想得出本方法解决
caodongfang126 2012-09-08
  • 打赏
  • 举报
回复
不会,盼答案。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧