华为机试题回忆

今天(2013-10-18)终于有机会机试了(上次没有收到短信)

听说和下午考题不一样(我上午的)

第一题:打印电量,其实就是用cout按照一定的规则打印“+-------------------+”和“++++++++++++++”和“============”  很简单。


第二题:求最小零钱数,钱面值有1,5,10,50,100;输入一个钱数,输出零钱张数(最少)直接除,也不难


第三题:字符串求和;数入如“1+43-2*15”输出“14”,要用递归,没有做出来,回来看了一下高手博客,发现有点难


参考:http://blog.csdn.net/sicofield/article/details/8907787

再补充一个(非原创):

#include <iostream>
#include <string>
using namespace std;


int Calc(char *exp)
{
	char op[255];
	int data[255];
	int mapop[255];
	memset(op,0,sizeof(op));
	memset(data,0,sizeof(data));
	memset(mapop,0,sizeof(mapop));
	for (int i = '0'; i < '9'; i++)
		mapop[i] = 1;
	mapop['+'] = 2;
	mapop['-'] = 2;
	mapop['*'] = 3;
	mapop['/'] = 3; 
	int len = strlen(exp);
	int tp = 0;
	int td = 0;
	int to = 0;
	int ans = 0;
	for (int i = 0; i < len + 1; i++)
	{
		if(mapop[exp[i]] == 1)
			tp = tp * 10 + exp[i] -'0';
		else
		{
			if (mapop[exp[i]] != 1 || i == len)   
				data[td++] = tp;
			if (mapop[op[to-1]] == 3)
			{
				if(op[to-1] == '*')
				{
					data[td-2] = data[td-2] * tp;
				}
				else
				{
					if (data[td-2] == 0)
						return -1;
					else
						data[td-2] = data[td-2] / tp;
				}
				to--;
				td--;
			}
		}
		if(mapop[exp[i]] == 2 || mapop[exp[i]] == 3)
		{
			op[to++] = exp[i];
			tp = 0;
		}
		
		
	}
	for(int i = td-1; i >=1; i--)
	{ 
		if (op[--to] == '+')
		{
			ans = ans + data[i];
		}
		else
		{
			ans = ans - data[i];
		}
	}
	return ans + data[0];
}




int main ()
{
	char str[128];
	gets(str);
	if (strlen(str)>128)
	{
		cout<<"error!";
	}
	cout<<Calc(str);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值