2013华为上机题C++编程

1. 字符串处理 

把一个字符串中的除大写字母、小写字母和数字字符之外的其他字符都去掉,输出新字符串。 

要求实现函数:  

void my_string(char* input, char* output) 

【输入】  char* input,输入的字符串 

【输出】  char* output, 输出的字符串

 【返回】  无 

示例  

输入:input = “A*(BC&De+_fg/*” 输出:output = “ABCDefg” 输入:input = “aB+_9” 

输出:output = “aB9”

#include "stdafx.h"
#include <iostream>
using namespace std;
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';
}
int _tmain(int argc, _TCHAR* argv[])
{
	char src[1000];
	cin>>src;
	char dst[1000];
	my_string(src,dst);
	cout<<dst;
	system("pause");
	return 0;
}


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 

 

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
void dice(int map_len,int* map1,int* dice_val,int* output)
{
	int temp=0,i;
	for(i=0;i<5;i++)
	{
		if(temp==map_len-1)
			break;
		temp+=*dice_val;
		if(map1[temp]==1)
		{
			dice_val++;
			i++;
		}
		else if (map1[temp]==2)
		{
			temp-=2;
			if(temp<=0)
			temp=0;
		}
		else if(map1[temp]==3)
			temp++;
		else
			;
		dice_val++;
	}
	*output=temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
	//int map_len=15,m_map[]={9,1,9,9,9,2,9,9,9,9,9,9,9,9,9},dice_val[]={1,2,1,3,1};
	int map_len=16,m_map[]={9,9,9,9,9,1,9,3,9,9,2,9,9,9,9,9},dice_val[]={2,1,4,1,6};
	int step=0;
	dice(map_len,m_map,dice_val,&step);
	cout<<step;
	system("pause");
	return 0;
}

 

 

3题目描述:  

输入一个字符串,将其中大写字母转换为对应小写字母之后的第五个字母, 若原始大写字母为V~Z, 则转换为对应小写字母的值减21。 其他字符不变,输出转换后的字符串。 

例如,对于字母A,则转换为小写字母f;若形参是字母W,则转换为小写字母b 要求实现函数:  

void TransferString(const char * pInputStr, long lInputLen, char * pOutputStr); 

【输入】 pInputStr:  输入字符串 

         lInputLen:  输入字符串长度          

【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 示例  

输入:“Axs3mWss” 输出:“fxs3mbss”

 

 

 

 

4. 将一个字符串的元音字母复制到另一个字符串,并排序(30分)

问题描述:有一字符串,里面可能包含英文字母(大写、小写)、数字、特殊字符,现在需要实现一函数,将此字符串中的元音字母挑选出来,存入另一个字符串中,并对字符串中的字母进行从小到大的排序(小写的元音字母在前,大写的元音字母在后,依次有序)。 说明:1、  元音字母是a,e,i,o,u,A,E,I,O,U。 

2、  筛选出来的元音字母,不需要剔重;最终输出的字符串,小写元音字母排在前面,大写元音

字母排在后面,依次有序。 

要求实现函数:void sortVowel (char* input, char* output); 

【输入】  char* input,表示输入的字符串 

【输出】  char* output,排好序之后的元音字符串。 

【返回】  无 

示例 :          输入:char *input = “Abort!May Be Some Errors In Out System.“ 

  输出:char *output =“aeeeooAEIO “

 

#include "stdafx.h"
#include <iostream>
using namespace std;
void sortVowel(char* input,char* output)
{
	int len=strlen(input);
	int i,j;
	char lchar[100];
	char rchar[100];
	int x=0,y=0;
	for(i=0;i<len;i++)
	{
		if(input[i]=='a'||input[i]=='e'||input[i]=='i'||input[i]=='o'||input[i]=='u')
			{
				lchar[x++]=input[i];
		    }
		else if(input[i]=='A'||input[i]=='E'||input[i]=='I'||input[i]=='O'||input[i]=='U')
		{
			    rchar[y++]=input[i];
		}		
	}
	char temp;
	//rchar[y]='\0';
	//cout<<rchar<<endl;
    for(i=0;i<x;i++)
	{
		for(j=i+1;j<x;j++)
		{
			if(lchar[j]<lchar[j-1])
			{
				temp=lchar[j-1];
				lchar[j-1]=lchar[j];
				lchar[j]=temp;
			}	
		}
		*output++=lchar[i];
	}
		for(i=0;i<y;i++)
		{
			for(j=i+1;j<y;j++)
			{
				if(rchar[j]<rchar[j-1])
				{
					temp=rchar[j-1];
					rchar[j-1]=rchar[j];
					rchar[j]=temp;
				}
			}
			*output++=rchar[i];
		}
		*output='\0';
}
int _tmain(int argc, _TCHAR* argv[])
{  
	char stringin[50]="Abort!May Be Some Errors In Out System.";
	char stringout[50];//空字符串初始化时必须赋初值
	sortVowel(stringin,stringout);
	cout<<stringout;
	system("pause");
	return 0;
}

5. 从两个数组的最后一个元素比较两个数组中不同元素的个数 

如有array1[5]={77,21,1,3,5}, array2[3]={1,3,5},从array1[4]与array2[2]比较开始,到array1[2]与array[0]比较结束。这样得出它们不同的元素个数为0,若array1[6]={77,21,1,3,5,7},那么他们不同的元素为3。   

函数原型为 int compare_array( int len1, int array1[], int len2, int array2[] ); 

  其中,len1与len2分别为数组array1[]和array2[]的长度,函数返回值为两个数组不同元素的个数。

 

 

6. 单词统计

题目描述:  

输入一段英文文本,用程序统计出现频率最高和最低的两个单词; 

英文文本中仅出现这四类字符:空格( )、英文逗号(,)、英文句号(.)、英文大小写字母(a-z、A-Z) 单词之间的分隔符仅考虑这三种:空格( )、英文逗号(,)、英文句号(.); 仅大小写不同的单词算同一个单词; 

如果两个单词出现次数相同,则在文本中首次出现的单词优先返回。 返回的单词统一用小写字母返回 例如: 

输入字符串“Hello world, i said hello world to the world”,返回“world”,“i” 输入字符串“Somebody like somebody,i do not like it”,返回“somebody”,“i” 要求实现函数:  

void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord); 

【输入】 pInputStr:  输入字符串,指向一段英文文本 

【输出】 pOutputHotWord: 输出字符串,返回出现次数最多的单词,该指针所指存储空间已经分配好,且足够大 

         pOutputColdWord:输出字符串,返回出现次数最少的单词,该指针所指存储空间已经分配好,且足够大 

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 示例  

输入:“Hello world, i said hello world to the world”

输入:"world "

             " i"

#include <iostream>
#include <cstring>
using namespace std;
void WordStat(const char *pInputStr, char *pOutputHotWord, char *pOutputColdWord); 
void WordStat(const char *pInputStr, char *pOutputHotWord, char *pOutputColdWord)
{
	///先将字符串分成一个一个单词赋给一个二维数组
	int len=strlen(pInputStr);
	int i,j=0,k=0,num_space=0;
	char temp[100][1000];
	for(i=0;i<len;i++)
	{
		temp[j][k]=pInputStr[i];
		if(temp[j][k]>='A'&&temp[j][k]<='Z')
			temp[j][k]+=32;//大写转成小写
		if(temp[j][k]==' '||temp[j][k]==','||temp[j][k]=='.')
		{
			temp[j][k]='\0';
			j++;
			k=0;
		}
		else
		k++;
	}
	temp[j][k]='\0';///最后一个要加个结束符
	//查找不同单词的类型
	char diffect_word[100][1000];
	int m,n,x=1;
	strcpy(diffect_word[0],temp[0]);
	bool flag=true;
	for(m=1;m<=j;m++)
	{
		flag=true;
		for(n=0;n<m;n++)
		{
			if(!strcmp(temp[m],temp[n]))
			{
				flag=false;
				break;
			}
		}
		if(flag)	
		strcpy(diffect_word[x++],temp[m]);

	}
	//计算每个不同的单词出现的次数
	int num_word[100]={0};
	for(i=0;i<x;i++)
	{
		for(k=0;k<=j;k++)
		{
			
			if(!strcmp(diffect_word[i],temp[k]))
				num_word[i]++;
		}
		cout<<num_word[i]<<" "<<diffect_word[i]<<endl;
	}
//查找num_word的第一个最大值与第一个最小值,对应在temp中的位置
  int num_max=0;
  int num_min=0;
  for(i=1;i<x;i++)
  {
	  if(num_word[num_max]<num_word[i])
		  num_max=i;
	  if(num_word[num_min]>num_word[i])
		  num_min=i;
  }
  pOutputHotWord=diffect_word[num_max];
  pOutputColdWord=diffect_word[num_min];
  cout<<pOutputHotWord<<endl;
  cout<<pOutputColdWord;
}
void main()
{
	const char *input="Hello world,i said hello world to the world ";//注意,豆号后面不要有空格
	//const char *input="Somebody like somebody i do not like it";
	char * pOutputHotWord=NULL;
	char * pOutputColdWord=NULL;
    WordStat(input,pOutputHotWord, pOutputColdWord);
	system("pause");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值