zoukankan      html  css  js  c++  java
  • 2013华为上机试题

    字串转换

    问题描述:

    将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:aa 转换为 bc,zz 转换为 ab;当连续相同字母超过两个时,第三个出现的字母按第一次出现算。

    要求实现函数:

    int convert(char *input,char* output)
    【输入】  char *input , 输入的字符串
    【输出】  char *output ,输出的字符串
    【返回】 

    示例

    输入:char*input="abcd" 
    输出:char*output="bcde"
    输入:char*input="abbbcd" 
    输出:char*output="bcdcde"

    字符串处理转换

    问题描述:    

    在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。

    要求实现函数:

    void my_word(charinput[], char output[])
    【输入】  char input[], 输入的字符串
    【输出】  char output[],输出的字符串
    【返回】 

    示例

    输入:charinput[]="some local buses, some1234123drivers" ,
    输出:charoutput[]="drivers local buses some"
    输入:charinput[]="%A^123 t 3453i*()" ,
    输出:charoutput[]=""

    正数减法

    问题描述:    

    两个任意长度的正数相减,这两个正数可以带小数点,也可以是整数,请输出结果。 输入的字符串中,不会出现除了数字与小数点以外的其它字符,不会出现多个小数点以及小数点在第一个字符的位置等非法情况,所以考生的程序中无须考虑输入的数值字符串非法的情况。 
    详细要求以及约束:
    1.输入均为正数,但输出可能为负数; 
    2.输入输出均为字符串形式;
    3.如果输出是正数则不需要带符号,如果为负数,则输出的结果字符串需要带负号
    例如:2.2-1.1 直接输出为“1.1”,1.1-2.2 则需要输出为“-1.1”
     4.输出的结果字符串需要过滤掉整数位前以及小数位后无效的0,小数位为全0的,直接输出整数位
    例如相减结果为11.345,此数值前后均不可以带0,“011.345”或者“0011.34500”等等前后带无效0的均视为错误 输出。例如1.1-1.1结果为0.0,则直接输出0

    要求实现函数:

    void Decrease(char *input1, char*input2, char *output)

    【输入】 char *iinput1 被减数
    char*nput2 减数 
    【输出】 char *output 减法结果
    【返回】 

    示例

    输入:char *input1="2.2" 
    char *input2="1.1"
    输出:char*output="1.1"
    输入:char *input1="1.1" 
    char *input2="2.2"

    输出:char *output="-1.1"
     
    解答代码:环境 WIN7 + VS2010
    代码整的有些复杂,第三题挺复杂,大数的减法,利用字符串来做,有小数点比较麻烦,第三题中,将两个数据相减,如果借位大于0,说明第一个数据小于第二个数据,这是可以交换两个数据,递归调用函数Decrease,输出的output加上‘-’。反之,则直接输出。那个清除无效的0,也挺搞人的。
     
    #include <iostream>
    #include <string>
    #include <vector>
    #include <time.h>  
    #include <algorithm>
    using namespace std;
    
    //第一题
    void convert(char *input,char* output)
    {
    	char *ptrin = input;
    	int repeat_count = 0;
    	char temp;
    	while(*ptrin != '')
    	{
    		if (ptrin != input)
    		{
    			if (*ptrin == *(ptrin - 1))
    			{
    				repeat_count++;
    
    				if (repeat_count == 2)
    				{
    					repeat_count = 0;
    				}
    			}
    			else
    			{
    				repeat_count = 0;
    			}
    		}
    
    		temp = (char)(*ptrin + repeat_count + 1); 
    		if (temp >  'z')
    		{
    			*output++ = (char)(temp - 'z' + 'a' - 1);
    		}
    		else
    		{
    			*output++ = temp;
    		}
    
    		ptrin++;
    	}
    	*output = '';
    }
    
    //第二题
    void my_word(char input[], char output[])
    {
          vector <int> index;
    	  vector <string> words;
    	  vector <int> count;
    	  char *ptrinput = input;
    	  string temp;
    	  //获得单词
    	  int i = 0;
    	  while(*ptrinput != '')
    	  {
    		  if (isalpha(*ptrinput))
    		  {
    			  temp += *ptrinput++;
    		  }
    		  else
    		  {
    			  if (temp.size() > 1)
    			  {
    				  words.push_back(temp);
    				  count.push_back(temp.size());
    				  index.push_back(i++);
    			  }
    			  temp.clear();
    			  ptrinput++;
    		  }
    	  }
    
    	  if (*ptrinput == '')
    	  {
    		  if (temp.size() > 1)
    		  {
    			  words.push_back(temp);
    			  count.push_back(temp.size());
    			  index.push_back(i++);
    		  }
    		  temp.clear();
    	  }
    
    	  //单词降序排列
    	  int j = 0;
    	  int tmp;
    	  int len = index.size();
    	  for (i = 0; i < len; i++)
    	  {
    		  for (j = 0; j < len - 1; j++)
    		  {
    			  if (count[j+1] > count[j])
    			  {
    				  tmp = index[j+1];
    				  index[j+1] = index[j];
    				  index[j] = tmp;
    				  tmp = count[j+1];
    				  count[j+1] = count[j];
    				  count[j] = tmp;
    			  }
    		  }
    	  }
    
    	  //根据index输出单词
    	  string outstr;
          int k = 0; 
    	  while (k < index.size())
    	  {
    		  while (k > 0 && (words[index[k]] == words[index[k -1]]))
    		  {
    			  k++;
    			  if (k == index.size())
    			  {
    				  break;
    			  }
    		  }
    		  if (k < index.size())
    		  {
    			  outstr += words[index[k]];
    			  outstr += ' ';
    			  k++;
    		  }
    	  }
    	  
    	  for (int m = 0; m < outstr.size(); m++)
    	  {
    		  *output++ = outstr[m];
    	  }
    	  *output = '';
    }
    
    //第三题
    void Decrease(char *input1, char*input2, char *output)
    {
    	int leftlen1 = 0;;
    	int rightlen1 = 0;
    	int leftlen2 = 0;
    	int rightlen2 = 0;
    	char *ptrinput1 = input1;
    	char *ptrinput2 = input2;
    
    	while (*ptrinput1 != '.' && *ptrinput1 != '' )
    	{
    		rightlen1++;
    		ptrinput1++;
    	}
    
    	//第一个数据有小数点
    	if (*ptrinput1 == '.')
    	{
    		ptrinput1++;
    		while (*ptrinput1 != '' )
    		{
    			leftlen1++;
    			ptrinput1++;
    		}
    	}
    
    	while (*ptrinput2 != '.' && *ptrinput2 != '')
    	{
    		rightlen2++;
    		ptrinput2++;
    	}
    	//第二个数据有小数点
    	if (*ptrinput2 == '.')
    	{
    		ptrinput2++;
    		while (*ptrinput2 != '' )
    		{
    			leftlen2++;
    			ptrinput2++;
    		}
    	}
    
    
    	//数据对齐
    	int calen = max(rightlen1,rightlen2) + max(leftlen1,leftlen2);
    	char *temp1 = new char[calen+1];
    	char *temp2 = new char[calen+1];
    	char *outemp = new char[calen+1];
    	int i = 0;
    	int index1 = 0;
    	int index2 = 0;
    	ptrinput1 = input1;
    	ptrinput2 = input2;
    
    	//右边数据对齐
    	if (rightlen1 < rightlen2)
    	{
    		for (i = 0; i < rightlen2 - rightlen1; i++)
    		{
    			temp1[index1++] = '0';
    		}
    	}
    	else if (rightlen1 > rightlen2)
    	{
    		for (i = 0; i < rightlen1 - rightlen2; i++)
    		{
    			temp2[index2++] = '0';
    		}
    	}
    
    	for (i = 0; i < rightlen1; i++)
    	{
    		temp1[index1++] = *ptrinput1++;
    	}
    	for (i = 0; i < rightlen2; i++)
    	{
    		temp2[index2++] = *ptrinput2++;
    	}
    
    	//补齐小数点
    	if (leftlen1 == 0 && leftlen2 != 0)
    	{
    		temp1[index1++] = '.';
    		temp2[index2++] = *ptrinput2++;
    	}
    	else if (leftlen1 != 0 && leftlen2 == 0)
    	{
    		temp1[index1++] = *ptrinput1++;
    		temp2[index2++] = '.';
    	}
    	else
    	{
    		temp1[index1++] = *ptrinput1++;
    		temp2[index2++] = *ptrinput2++;
    	}
    
    	
    	//左边数据对齐
    	for (i = 0; i < leftlen1; i++)
    	{
    		temp1[index1++]  = *ptrinput1++;
    	}
    	for (i = 0; i < leftlen2; i++)
    	{
    		temp2[index2++] = *ptrinput2++;
    	}
    
    	if (leftlen1 < leftlen2)
    	{
    		for (i = 0; i < leftlen2 - leftlen1; i++)
    		{
    			temp1[index1++]  = '0';
    		}
    	}
    	else 	if (leftlen1 > leftlen2)
    	{
    		for (i = 0; i < leftlen1 - leftlen2; i++)
    		{
    			temp2[index2++] = '0';
    		}
    	}
    
    	//开始做减法
    	int flagbit = 0;
    	for (i = calen; i > -1; i--)
    	{
    		if (temp1[i] == '.')
    		{
    			outemp[i] = '.';
    		}
    		else if (temp1[i] == '')
    		{
    			;
    		}
    		else
    		{
    			if ((temp1[i] - flagbit) < temp2[i])
    			{
    				outemp[i] = (char)(temp1[i]- flagbit + 10 - temp2[i] + '0');
    				flagbit = 1;
    			}
    			else
    			{
    				outemp[i] = (char)(temp1[i]- flagbit - temp2[i] + '0');
    				flagbit = 0;
    			}
    		}
    	}
    
    	//清除无效的0
    	char *ptrpre = outemp;
    	char *ptrend = outemp + calen;
    	while(*ptrpre == '0')
    	{
    		ptrpre++;
    	}
    	while(*ptrend == '0')
    	{
    		ptrend--;
    	}
    
    	//通过借位标志判断两个数的大小关系
    	if (flagbit == 0)
    	{
    		if (*ptrpre != '.' && *ptrend != '.')
    		{
    			memcpy(output,ptrpre,sizeof(char)*(ptrend - ptrpre + 1));
    		}
    		else if (*ptrpre == '.' && *ptrend != '.')
    		{
    			*output++ = '0'; 
    			memcpy(output,ptrpre,sizeof(char)*(ptrend - ptrpre + 1));
    		}
    		else if(*ptrpre != '.' && *ptrend == '.' )
    		{
    			memcpy(output,ptrpre,sizeof(char)*(ptrend - ptrpre));
    		}
    		else
    		{
    			*output = '0';
    		}
    		*(output+ (ptrend - ptrpre  + 1)) = '';
    	}
    	else
    	{
    		*output++ = '-';
    		Decrease(input2,input1,output);
    	}
    	delete[] temp1;
    	delete[] temp2;
    	delete[] outemp;
    	temp1 = NULL;
    	temp2 = NULL;
    	outemp = NULL;
    }
    
    void  main()
    {
    // 	//第一题测试
    // 	char input[100];
    // 	char output[100];
    // 	while(1)
    // 	{
    // 		cout << "input:" ;
    // 		gets(input);
    // 		convert(input,output);
    // 		cout << "output:";
    // 		cout << output << endl<<endl;
    // 		memset(output,0,sizeof(char)*100);
    // 	}
    
    	//第二题测试
    // 	char input[100];
    // 	char output[100];
    // 	while(1)
    // 	{
    // 		cout << "input:" ;
    // 		gets(input);
    // 		my_word(input,output);
    // 		cout << "output:";
    // 		cout << output << endl<<endl;
    // 	   memset(output,0,sizeof(char)*100);
    // 	}
    
    
    	//第三题测试
    	char input1[100];
    	char input2[100];
    	char output[100];
    	while(1)
    	{
    		cout << "input1:" ;
    		gets(input1);
    		cout << "input2:" ;
    		gets(input2);
    		Decrease(input1,input2,output);
    		cout << "result:";
    		cout << output << endl<<endl;
    	   memset(output,0,sizeof(char)*100);
    	}
    }
    
    

     
  • 相关阅读:
    【基础算法】- 全排列
    【基础算法】- 2分查找
    区块链培训
    Static Binding (Early Binding) vs Dynamic Binding (Late Binding)
    test
    No data is deployed on the contract address!
    "throw" is deprecated in favour of "revert()", "require()" and "assert()".
    Variable is declared as a storage pointer. Use an explicit "storage" keyword to silence this warning.
    京都行
    Failed to write genesis block: database already contains an incompatible
  • 原文地址:https://www.cnblogs.com/adong7639/p/3959539.html
Copyright © 2011-2022 走看看