华为笔试|2015/08/22
//通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
//输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){
int i = 0;
int a = 0, b = 0;
char operater = '+';
while (pInputStr[i]>'0' && pInputStr[i]<'9'){
a = a * 10 + pInputStr[i] - '0';
++i;
}
operater = pInputStr[++i];
++(++i);
while(i<lInputLen){
if (pInputStr[i]>'0' && pInputStr[i] < '9'){
b = b * 10 + pInputStr[i] - '0';
++i;
}
else{ pOutputStr = "0\0"; return; }
}
int res = 0;
switch (operater)
{
case '+':
res = a + b; break;
case '-':
res = a - b; break;
case '*':
res = a * b; break;
case '/':
res = a / b; break;
}
int2chars(res, pOutputStr);
}
void int2chars(int x, char *str){
int i = 0;
if (x<0){
x = -x;
str[i] = '-';
}
++i;
int a[5] = { 0, 0, 0, 0, 0 };
int j = 0;
while (x>0){
a[j++] = x % 10;
x = x / 10;
}
for (int k = 4; k >=0; --k)
{
if (a[k] > 0){
str[i] = '0' + a[k];
++i;
}
}
str[i] = '\0';
}
2015年8月23日,去参加华为机试。
题目1:交叉排序
输入一串用空格隔开的数字串,对于数字串的奇数位按升序排序,偶数位按降序排序。
示例输入:4 6 2 3 6 7 8 1
处理过程:
奇数位:4 2 6 8 升序排序结果: 2 4 6 8
偶数位:6 3 7 1 降序排序结果: 7 6 3 1
结果输出:2 7 4 6 6 3 8 1
void diasplay(std::vector<int> &list){
for (int i = 0; i < list.size(); ++i)
{ cout << list[i] << " "; }
cout << endl;
}
bool compare(int a, int b) {
if (a > b){ return true; }
else{ return false; }
}
std::vector<int> f(std::vector<int> list){
std::vector<int> evens,odds,res;
for (int i = 0; i < list.size(); ++i)
{
if ((i + 1) % 2 != 0){
odds.push_back(list[i]);
}
else{
evens.push_back(list[i]);
}
}
sort(odds.begin(), odds.end());
sort(evens.begin(), evens.end(), compare);
int j = 0, k = 0;
for (int i = 0; i < list.size(); ++i)
{
if ((i + 1) % 2 != 0){
res.push_back(odds[j++]);
}
else{
res.push_back(evens[k++]);
}
}
return res;
}
int main(){
int a;
std::vector<int> list;
while (cin >> a){ list.push_back(a); }
diasplay(list);
std::vector<int> r = f(list);
diasplay(r);
return 0;
}
题目2:成绩排名
题目总共包含如下两种格式的字符串命令:
-
LOD GRADE命令,其格式:
LOD GRADE:NAME=XiaoMing,MATH=80,LANG=90;
(1) 此命令用于导入学生成绩
(2) NAME字段表示学生姓名
(3) MATH字段表示学生数学成绩
(4) LANG字段表示语文成绩
(5) MATH字段和LANG字段顺序不一定MATH在前,LANG在后
(6) 相同的分数,名次相同,后面的名次空缺;例如100,99,99,99,98,98,名次:1,2,2,2,5,5
(7) 此命令会连续执行,直到遇到第一个LST GRADE -
LST GRADE命令,其格式:
LST GRADE:NAME=XiaoMing;
(1) 此命令用于查询学生成绩
(2) NAME字段表示学生姓名
(3) 查询结果格式:姓名 数学 语文 总分 数学排名 语文排名 总排名
(5) 每组用例,此命令仅执行一次
连续多组LOD GRADE后跟一个LST GRADE查询命令
输出查询格式为:姓名 数学 语文 总分 数学排名 语文排名 总排名
样例输入:
LOD GRADE:NAME=XiaoMing,MATH=80,LANG=90;
LOD GRADE:NAME=XiaoHong,LANG=60,MATH=100;
LOD GRADE:NAME=XiaoMei,MATH=70,LANG=90;
LST GRADE:NAME=XiaoHong;
样例输出:
XiaoHong 100 60 160 1 3 2
class Student
{
public:
Student(){ name = ""; math = 0; lang = 0; sum_score = 0; };
~Student(){};
bool load(string &s)
{
if (s.substr(0, 3) == "LOD"){
int b = 15, e = 15;
while (e < s.size() && s[e] != ','){ ++e; }
name = s.substr(b, e - b);
b = e + 1; e = b;
if (s.substr(b, 4) == "MATH"){
b = b + 5; e = b;
while (e < s.size() && s[e ] != ','){ ++e; }
math = atoi(s.substr(b, e - b).c_str());
b = e + 6; e = b;
while (e < s.size() && s[e ] != ';'){ ++e; }
lang = atoi(s.substr(b, e - b).c_str());
}
else if (s.substr(b, 4) == "LANG"){
b = b + 5; e = b;
while (e < s.size() && s[e] != ','){ ++e; }
lang = atoi(s.substr(b, e - b).c_str());
b = e + 6; e = b;
while (e < s.size() && s[e] != ';'){ ++e; }
math = atoi(s.substr(b, e - b).c_str());
}
sum_score = math + lang;
}
else{
return false;
}
return true;
}
void display(){
cout << name << " " << math << " " << lang << " " << sum_score
<< " " << math_p << " " << lang_p << " " << sum_p;
}
string name;
int math;//数学成绩
int lang;//语文成绩
int sum_score;//总成绩
int math_p;//数学排名
int lang_p;//语文排名
int sum_p;//总排名
};
bool compare1(Student a, Student b) {
if (a.math > b.math){ return true; }
else{ return false; }
}
bool compare2(Student a, Student b) {
if (a.lang > b.lang){ return true; }
else{ return false; }
}
bool compare3(Student a, Student b) {
if (a.sum_score > b.sum_score){ return true; }
else{ return false; }
}
void find(string &s, vector<Student> &stds){
int b = 15, e = 15;
while (e < s.size() && s[e + 1] != ';'){ ++e; }
string name = s.substr(b, e - b + 1);
// 按math成绩排序
sort(stds.begin(), stds.end(), compare1);
stds[0].math_p = 1;
int j = 1;
for (int i = 1; i < stds.size(); ++i){
if (stds[i].math == stds[i - 1].math){
stds[i].math_p = j;
}
else{
stds[i].math_p = i+1;
j = i + 1;
}
}
// 按lang成绩排序
sort(stds.begin(), stds.end(), compare2);
stds[0].lang_p = 1;
j = 1;
for (int i = 1; i < stds.size(); ++i){
if (stds[i].lang == stds[i - 1].lang){
stds[i].lang_p = j;
}
else{
stds[i].lang_p = i+1;
j = i + 1;
}
}
// 按sum_score成绩排序
sort(stds.begin(), stds.end(), compare3);
stds[0].sum_p = 1;
j = 1;
for (int i = 1; i < stds.size(); ++i){
if (stds[i].sum_score == stds[i - 1].sum_score){
stds[i].sum_p = j;
}
else{
stds[i].sum_p = i+1;
j = i + 1;
}
}
for (int i = 0; i < stds.size(); ++i){
if (stds[i].name == name){
stds[i].display();
}
}
}
int main(){
vector<Student> stds;
string input_str;
while (getline(cin, input_str))
{
if (input_str.substr(0, 3) == "LOD"){
Student s1;
s1.load(input_str);
stds.push_back(s1);
}
else if (input_str.substr(0, 3) == "LST") {
find(input_str, stds);
break;
}
else{
break;
}
}
return 0;
}
题目3:最大凸多边形
没做出来,只写了部分(读取整行,并转化为数字)
class point{
public:
point(int a,int b):x(a),y(b){}
int x;
int y;
};
bool compare(point a,point b) {
if(a.x<b.y){return true;}
else{return false;}
}
vector<point> get_points(string &s){
vector<point> points;
int b=0,e=0;
int n=0;
while(e<s.size() && s[e] != ';' ){++e;}
n=atoi(s.substr(b,e-b).c_str());
b=e+1;e=b;
while(e<s.size())
{
while(e<s.size() && s[e] != ',' ){++e;}
int x=atoi(s.substr(b,e-b).c_str());
++e;b=e;
while(e<s.size() && s[e] != ';' ){++e;}
int y=atoi(s.substr(b,e-b).c_str());
++e;b=e;
points.push_back(point(x,y));
}
return points;
}
void f(vector<point> &ps){
sort(ps.begin(),ps.end(),compare);
point p0=ps[0];
}
int main()
{
string ss;
getline(cin, ss);
cout<<ss<<endl;
vector<point> points=get_points(ss);
cout<<points.size();
for(int i=0;i<points.size();++i){
cout<<points[i].x<<" "<<points[i].y<<endl;
}
return 0;
}