Last active
December 20, 2015 14:49
-
-
Save linzhi/6149673 to your computer and use it in GitHub Desktop.
华为2014年校园招聘杭州站机试题
一共三道题,第一道理我给了两种解法
测试案例不多,华为的这几个题目不需要很多测试例子,题目的要求已经把很多坑给填了
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> | |
#include <cstdlib> | |
#include <cstring> | |
using namespace std; | |
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr) | |
{ | |
long j = 0; | |
char alpha[26] = {0}; | |
long len = lInputLen; | |
for (long i = 0; i < len; i++) { | |
int cur = pInputStr[i] - 'a'; | |
if (alpha[cur] == 0) { | |
alpha[cur]++; | |
pOutputStr[j++] = pInputStr[i]; | |
} | |
} | |
} | |
void stringFilter_1(const char *pInputStr, long lInputLen, char *pOutputStr) | |
{ | |
int i = 0; | |
while (*pInputStr != '\0') { | |
char tmp = *pInputStr; | |
char *p = pOutputStr; | |
while (*p != '\0') { | |
if (tmp != *p) | |
p++; | |
else | |
break; | |
} | |
if (*p == '\0') { | |
pOutputStr[i] = tmp; | |
i++; | |
} | |
pInputStr++; | |
} | |
} | |
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr) | |
{ | |
int i = 0; | |
const char *p = pInputStr; | |
const char *q = pInputStr; | |
while (p <= q && *p != '\0' && *q != '\0') { | |
while (*p == *q) | |
q++; | |
int num = q - p; | |
if (num > 1) { | |
int len = sprintf(&pOutputStr[i], "%d", num); | |
pOutputStr[i + len] = *p; | |
i += len + 1; | |
p = q; | |
} | |
else { | |
pOutputStr[i] = *p; | |
i++; | |
p = q; | |
} | |
} | |
} | |
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr) | |
{ | |
const char *op = NULL; | |
const char *p = pInputStr; | |
const char *q = pInputStr + lInputLen - 1; | |
while (*pInputStr != '\0') { | |
if (*pInputStr == '+' || *pInputStr == '-') { | |
op = pInputStr; | |
break; | |
} | |
pInputStr++; | |
} | |
if (*(op + 1) != ' ') { | |
pOutputStr[0] ='0'; | |
return ; | |
} | |
while (*(q - 1) != ' ') | |
q--; | |
int a = atoi(p); | |
int b = atoi(q); | |
if (*op == '+') { | |
int c = a + b; | |
sprintf(pOutputStr, "%d", c); | |
} | |
if (*op == '-') { | |
int c = a - b; | |
sprintf(pOutputStr, "%d", c); | |
} | |
} | |
int main(void) | |
{ | |
char pInputStr1[] = {"abcbadeffeffa"}; | |
char pInputStr2[] = {"acccccccgddeff"}; | |
char pInputStr3[] = {"4 + 7"}; | |
char pOutputStr1[128] = {'\0'}; | |
char pOutputStr1_1[128] = {'\0'}; | |
char pOutputStr2[128] = {'\0'}; | |
char pOutputStr3[128] = {'\0'}; | |
stringFilter(pInputStr1, strlen(pInputStr1), pOutputStr1); | |
printf(">Q1: input: %s output: %s\n", pInputStr1, pOutputStr1); | |
stringFilter_1(pInputStr1, strlen(pInputStr1), pOutputStr1_1); | |
printf(">Q1: input: %s output: %s\n", pInputStr1, pOutputStr1_1); | |
stringZip(pInputStr2, strlen(pInputStr2), pOutputStr2); | |
printf(">Q2: input: %s output: %s\n", pInputStr2, pOutputStr2); | |
arithmetic(pInputStr3, strlen(pInputStr3), pOutputStr3); | |
printf(">Q3: input: %s output: %s\n", pInputStr3, pOutputStr3); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment