博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2013华为上机试题
阅读量:2231 次
发布时间:2019-05-09

本文共 6412 字,大约阅读时间需要 21 分钟。

字串转换
问题描述:
将输入的字符串(字符串仅包含小写字母‘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 
#include
#include
#include
#include
using namespace std;//第一题void convert(char *input,char* output){ char *ptrin = input; int repeat_count = 0; char temp; while(*ptrin != '\0') { 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 = '\0';}//第二题void my_word(char input[], char output[]){ vector
index; vector
words; vector
count; char *ptrinput = input; string temp; //获得单词 int i = 0; while(*ptrinput != '\0') { 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 == '\0') { 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 = '\0';}//第三题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 != '\0' ) { rightlen1++; ptrinput1++; } //第一个数据有小数点 if (*ptrinput1 == '.') { ptrinput1++; while (*ptrinput1 != '\0' ) { leftlen1++; ptrinput1++; } } while (*ptrinput2 != '.' && *ptrinput2 != '\0') { rightlen2++; ptrinput2++; } //第二个数据有小数点 if (*ptrinput2 == '.') { ptrinput2++; while (*ptrinput2 != '\0' ) { 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] == '\0') { ; } 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)) = '\0'; } 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<
 

转载地址:http://njybb.baihongyu.com/

你可能感兴趣的文章
分布式系统中的幂等性的理解
查看>>
spring的注解开发中的常用注解(一)------@bean @Configuration @ComponentScan @Import @Scope @Lazy
查看>>
(五)alin’s mysql学习笔记----索引性能分析
查看>>
Spring中使用@Transactional注解进行事务管理的时候只有应用到 public 方法才有效
查看>>
springboot整合rabbitmq及rabbitmq的简单入门
查看>>
mysql事务和隔离级别笔记
查看>>
事务的传播属性(有坑点)自调用失效学习笔记
查看>>
REDIS缓存穿透,缓存击穿,缓存雪崩原因+解决方案
查看>>
动态代理实现AOP
查看>>
23种常见的java设计模式
查看>>
关于被final修饰的基本数据类型一些注意事项
查看>>
java Thread中,run方法和start方法的区别
查看>>
在 XML 中有 5 个预定义的实体引用
查看>>
XML 元素是可扩展的
查看>>
避免 XML 属性?针对元数据的 XML 属性
查看>>
XML DOM nodeType 属性值代表的意思
查看>>
JSP相关知识
查看>>
JDBC的基本知识
查看>>
《Head first设计模式》学习笔记 - 适配器模式
查看>>
《Head first设计模式》学习笔记 - 单件模式
查看>>