各大计算机公司 笔试及面试 题目 - 华为(上机 7-8 答案)

本文提供了三个C++编程题目及解答:数组元素迭代、表达式求值与随机扑克牌抽取。涉及C++标准库中vector与stack的应用,以及随机数生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<vector>
#include<stack>

#include<cstdlib>
#include<ctime>

using namespace std;


/************************************************************
*
*题七
*
*************************************************************/
void array_iterate(int len, int input_array[], int m, int output_array[])
{
	vector<int> vec;
	int pos=0;

	for(int i=0;i<len;i++)
	{
		vec.push_back(input_array[i]);
	}

	for(int i=0;vec.size()>0 && i<len;i++)
	{
		pos=(pos+m)%vec.size();
		output_array[len-1-i]=vec[pos];
		m=vec[pos];
		vec.erase(vec.begin()+pos);
	}
}
 

/************************************************************
*
*题八
*
*************************************************************/
int calculate(int len,char *expStr) 
{
	stack<int> optrStack;
	stack<int> opndStack;
	int a1=0,a2=0;

	while(*expStr !='\0')
	{
		//number
		if((*expStr-'0') >=0 && (*expStr-'0')<=9)
		{
			opndStack.push((*expStr++)-'0');
		}
		else // + - * /
		{
			optrStack.push(*expStr++);

			if(optrStack.top() =='*')
			{
				a1=opndStack.top();
				a2= (*expStr++)-'0';

				opndStack.pop();
				optrStack.pop();

				opndStack.push(a1*a2);

			}
			else if(optrStack.top() =='/')
			{
				a1=opndStack.top();
				a2= (*expStr++)-'0';

				opndStack.pop();
				optrStack.pop();

				opndStack.push(a1/a2);
			}

		}
	}

	while(!optrStack.empty())
	{
		a2=opndStack.top();
		opndStack.pop();
		
		a1= opndStack.top();
		opndStack.pop();

		if(optrStack.top()=='+')
		{
			optrStack.pop();
			opndStack.push(a1+a2);

		}
		else//only -
		{
			optrStack.pop();
			opndStack.push(a1-a2);
		}
	}

	return opndStack.top();
}

/************************************************************
*
*题九
*
*************************************************************/

#define A 1
#define J 11
#define Q 12
#define K 13

void judgePork()
{
	int *arr=new int[5];
	srand(time(NULL));
	for(int i=0;i<5;i++)
	{
		arr[i]=rand()%13;
		cout<<arr[i]<<" ";
	}

	cout<<"\n";
}


int main()  
{  
	//测试 题七
	{
		//int a[]={3,1,2,4};  
		//int b[4];  
		//memset(b,0,4*sizeof(int));  
		//array_iterate(4, a, 7,b);  
		//printf("%d,%d,%d,%d\n",b[0],b[1],b[2],b[3]);  
		//printf("------------------------------------\n");
		//array_iterate_2(4, a, 7,b);  
		//printf("%d,%d,%d,%d\n",b[0],b[1],b[2],b[3]);  
	}

	//测试 题八
	{
		////char *expStr = "1+4*5-8/3";  
		//char *expStr = "8/3*3";  
		//int len=strlen(expStr);  
		////calculate(len,expStr);  

		//printf("%s = %d\n",expStr,calculate(len,expStr));
	}

	//测试 题九
	{
		judgePork();
	}

	system("pause");
}  
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值