2013年华为编程大赛上机题,第一二题比较简单,第三题有点难度,需要先把中缀序列转化成后缀序列,然后对后缀序列求值
题目如下,后面有答案
编程题(共3题)
注意:
1、请上机编写程序,按题目要求提交文件。[详见考试说明,点击进入考试说明]
2、本试题采用自动执行测试用例进行评分,测试用例不对考生公开
3、评卷通过在给定用例输入下,严格按照试题要求比较考生实现函数的输出与预设输出。两者相同则得分,不同则不得分
4、评卷人保证测试用例输入参数的合法性,考生不用考虑输入参数非法或异常的情况,题目中注明的例外
5、评卷人保证测试用例输入在被测函数正常合法情况下使用不会导致程序错误
6、被要求实现的函数如果包含返回参数,该返回参数涉及的空间分配和释放均在函数外完成,在被要求实现函数内部可以直接使用返回参数
7、如果考生函数异常导致程序崩溃或死循环,则自动评卷可能会被人为终止,剩余用例不被执行,无法得分
8、基于上述阅卷规则,请考生严格按照题目要求功能实现程序,尽量保证实现函数的稳健性,同时建议完成一道题并调试保证正确性后,再考虑并实现下一题目
1. 字符串处理
- 问题描述:
把一个字符串中的除大写字母、小写字母和数字字符之外的其他字符都去掉,输出新字符串。
- 要求实现函数:
void my_string(char*input, char* output)
【输入】 char* input,输入的字符串
【输出】 char* output, 输出的字符串
【返回】 无
- 示例
输入:input = “A*(BC&De+_fg/*”
输出:output = “ABCDefg”
输入:input = “aB+_9”
输出:output = “aB9”
2. 掷骰子游戏
- 问题描述:
在掷骰子游戏中,会根据所掷数字在地图中前进几步,前进完成后需要根据当前地图位置所示的障碍进行相应操作,其中障碍表示:
1) 9:无障碍
2) 1:停掷一轮,即下轮所掷数字无效;
3) 2:后退两步,如果已经到起点不再后退;
4) 3:奖励前进一步
如果在游戏过程中,已经走到地图终点,则游戏结束。根据输入的地图数组,和5个骰子数的数组,返回最终玩家前进了多少步。
- 要求实现函数:
void dice(int map_len,int* map, int* dice_val, int* output)
【输入】 int map_len,地图数组的长度
int* map,地图数组,值表示障碍
int* dice_val,5个骰子数的数组
【输出】 int *output,玩家共前进了多少步
【返回】 无
注:玩家是从起始位置开始,即地图数组的第一位,骰子数只能是1~6
- 示例
1) 输入:map_len = 15, map ={9,1,9,9,9,2,9,9,9,9,9,9,9,9,9},dice_val = {1,2,1,3,1},
返回:4
2) 输入:map_len = 16, map ={9,9,9,9,9,1,9,3,9,9,2,9,9,9,9,9},dice_val = {2,1,4,1,6},
返回:15
3. 表达式计算
- 问题描述:
在软件园开饭店的小明最近很郁闷,经常在进行账目核查时出错,每次的计算结果总是有偏差。小王知道后打算为小明解决这个问题,经过调查发现,问题出现在计算器上,当前的计算器计算方法大部分是:输入数据,输入运算符,再输入数据,立刻给出计算结果,然后不断循环。当计算结束时,如果出现偏差或者数据,无法回顾查看到底是哪一步出错,如果需要再次核查,还需要再全部输入一次,非常耽误时间。小王打算帮老板设计一种新的计算器,能够支持表达式的计算,由于是第一个版本,只需要支持整数的“+,-,*”和“( )”即可, 当然括号内的运算优先级高于括号外,“*”的优先级是高于“+,-”的。
注:输入的表达式字符串长度小于20。且表达式一定是合法的表达式。
- 要求实现函数:
void calculate(char*input, int* rel)
【输入】 char *input,待计算的表达式
【输出】 int* rel,计算结果
【返回】 无
- 示例
1) 输入:input = 5+2-10*2+5
输出:rel = -8
// 2013年 华为上机考试题
//question1
void my_string(char* input, char* output)
{
while(*input != '\0')
{
if( *input>='A' && *input<='Z' ||
*input>='a' && *input<='z' ||
*input>='0' && *input<='9')
{
*output++ = *input;
}
++input;
}
*output = '\0';
}
//question2
void dice(int map_len, int* map, int* dice_val, int* output)
{
int i=0;
int pos = 0;
for(i=0;i<5;i++)
{
switch(map[pos])
{
case 9:
break;
case 1:
i++;
break;
case 2:
pos -=2;
break;
case 3:
pos +=1;
break;
default:break;
}
if(pos < 0) pos =0;
pos += dice_val[i];
if(pos >= map_len-1) break;
}
*output = pos ;
}
// question 3
int prior(char ch)
{
if(ch=='*' || ch=='/')
return 2;
if(ch=='+' || ch=='-')
return 1;
return 0;
}
void calculate(char *input,int *output)
{
stack<char> op_stack;
vector<int> str;
while(*input != '\0')
{
if(*input>='0' && *input<='9')
{
str.push_back(atoi(input));
while(*input>='0' && *input<='9')
{
++input;
}
}
else if(*input=='(')
{
op_stack.push(*input);
++input;
}
else if(*input==')')
{
while(op_stack.top()!='(')
{
char ch=op_stack.top();
op_stack.pop();
str.push_back(ch);
}
op_stack.pop();
++input;
}
else
{
if(op_stack.empty())
{
op_stack.push(*input);
++input;
}
else if(prior(*input)>prior(op_stack.top()))
{
op_stack.push(*input);
++input;
}
else
{
while(!op_stack.empty() && prior(op_stack.top())>=prior(*input))
{
char ch=op_stack.top();
op_stack.pop();
str.push_back(ch);
}
op_stack.push(*input);
++input;
}
}
}
while(!op_stack.empty())
{
char ch=op_stack.top();
op_stack.pop();
str.push_back(ch);
}
stack<int> num_stack;
int i=0;
int a,b;
int ret=0;
for(i=0;i<str.size();i++)
{
if(str[i]=='+' || str[i]=='-'
||str[i]=='*'|| str[i] =='/')
{
a=num_stack.top();
num_stack.pop();
b=num_stack.top();
num_stack.pop();
if(str[i] == '+')
{
ret = a+b;
}
else if(str[i] == '-')
{
ret = b-a;
}
else if(str[i] == '*')
{
ret = a*b;
}
else if(str[i] == '/')
{
ret = b/a;
}
num_stack.push(ret);
}
else//str[i] 为数字 则压栈
{
num_stack.push(str[i]);
}
}
*output = ret;/**/
}
int main()
{
char input[] = "5+9-15*(2+5)+3";
int ret;
calculate(input,&ret);
cout << ret << endl;
return 0;
}