一,华为13年校招
试题1:
题目描述:
通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。
请编写一个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个’,’并将子串存储。
如果输入“abc def gh i d”,结果将是abc,def,gh,i,d,
要求实现函数:
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“abc def gh i d” 输出:“abc,def,gh,i,d,”
<pre name="code" class="html">// ConsoleAppDivideString.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr);
using namespace std;
#define MAX 32
int _tmain(int argc, _TCHAR* argv[])
{
char srcStr[MAX]="abc def gh i d";
char dstStr[MAX]={0};
int len=strlen(srcStr);
DivideString(srcStr,len,dstStr);
cout<<"处理后的结果为:"<<dstStr<<endl;
system("pause");
return 0;
}
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr)
{
int len=lInputLen;
int i=0;
while (i<len)
{
if (pInputStr[i]==' ')
{
pOutputStr[i]=',';
}
else
{
pOutputStr[i]=pInputStr[i];
}
i++;
}
int j=0;
while( j<len )
{
if ((pInputStr[j]==' ') && (pInputStr[j+1]==' '))
{
for (int k=j;k<len;k++)
{
pOutputStr[k+1]=pOutputStr[k+2];
}
}
j++;
}
int n=0;
while (true)
{
n++;
if (pOutputStr[n]=='\0')
{
pOutputStr[n]=',';
pOutputStr[n+1]='\0';
break;
}
}
}
另一份可参考答案:
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr)
{
long j = 0;
for(long i=0;i<lInputLen;i++)
{
if(pInputStr[i]!=' ')
{
pOutputStr[j]= pInputStr[i];
j++;
}
else
{
if(pOutputStr[j-1]!= ',')
{
pOutputStr[j]=',';
j++;
}
}
}
int n=0;
while (true)
{
n++;
if (pOutputStr[n]=='\0')
{
pOutputStr[n]=',';
pOutputStr[n+1]='\0';
break;
}
}
}
试题2:
题目描述:将输入的一个单向链表,逆序后输出链表中的值。链表定义如下:
<span style="font-size:18px;">typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode; </span>
要求实现函数:
void converse(ListNode **head);
【输入】head: 链表头节点,空间已经开辟好
【输出】head: 逆序后的链表头节点 【返回】无
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出示例
输入:链表 1->2->3->4->5 的头节点head 输出:链表 5->4->3->2->1 的头节点head
<pre name="code" class="html">// ConsoleAppConverseList.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cassert>
using namespace std;
typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode; //节点
void Reverse(ListNode **phead) //反序链表元素
{
if ((*phead)==NULL)
{
cout<<"No Data"<<endl;
return;
}
ListNode *preNode=NULL;
ListNode *curNode=*phead;
while(curNode->next!=NULL)
{
ListNode *temp=curNode->next;
curNode->next=preNode;
preNode=curNode;
curNode=temp;
}
//对最后个节点处理
curNode->next = preNode;
*phead=curNode;
}
void Insert(ListNode **phead,int nData) //插入链表元素
{
ListNode *newNode=new ListNode;
assert(newNode);
newNode->value=nData;
newNode->next=NULL;
if ((*phead)==NULL)
{
*phead=newNode;
}
else
{
newNode->next=*phead;
*phead=newNode;
}
}
void PrintList(ListNode **phead) //打印链表元素
{
ListNode *p=*phead;
if (p==NULL)
{
cout<<"No Data!!!"<<endl;
return;
}
for (;p!=NULL;p=p->next)
{
cout<<p->value<<"->";
}
cout<<endl;
}
int LengthList(ListNode **phead) //打印链表元素
{
int count=0;
ListNode *p=*phead;
if (p==NULL)
{
cout<<"No Data!!!"<<endl;
return 0;
}
for (;p!=NULL;p=p->next)
{
count++;
}
return count;
}
void DelNode(ListNode **phead,int nDate)
{
ListNode *preNode=NULL;
ListNode *curNode=*phead;
if (curNode->value==nDate)
{
*phead=curNode->next;//为什么不能用curNode=curNode->next;?????????
}
else
{
while(curNode->value!=nDate)
{
preNode=curNode;
curNode=curNode->next;
if (curNode->value==nDate)
{
preNode->next=curNode->next;
delete curNode;
break;
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
ListNode *head=new ListNode;
head=NULL;
Insert(&head,5);
Insert(&head,1);
Insert(&head,3);
Insert(&head,4);
PrintList(&head);
int len=LengthList(&head);
cout<<"Length of List: "<<len<<endl;
DelNode(&head,4);
Reverse(&head);
PrintList(&head);
getchar();
return 0;
}
二,华为14年校招
试题1:
输入1--50个数字,求出最小数和最大数的和,输入以逗号隔开
- <pre name="code" class="html">// ConsoleAppMaxMinSum.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "iostream"
- using namespace std;
- #define MAX 50
- void BubbleSort(int pArr[],int n) ;
- int _tmain(int argc, _TCHAR* argv[])
- {
- int n=0;
- int a[MAX]={0};
- char str=',';
- cout<<"你想输入多少个整数?(注:请不要超过50个数字)"<<endl;
- cin>>n;
- if (n>=50)
- {
- cout<<"请不要超过50个数字,请重新输入:"<<endl;
- cin>>n;
- }
- cout<<"请输入具体数字,并以逗号隔开:"<<endl;
- for (int i=0;i<n;i++)
- {
- if (i==n-1)
- cin>>a[i];
- else
- cin>>a[i]>>str;
- }
- BubbleSort(a,n);
- cout<<"排序后的数组为: "<<endl;
- for (int i=0;i<n;i++)
- {
- cout<<a[i]<<" ";
- }
- cout<<endl;
- cout<<"最小值与最大值的和为:"<<a[0]+a[n-1];
- cout<<endl;
- system("pause");
- return 0;
- }
- void BubbleSort(int *pArr,int n)
- {
- for (int i=0;i<n;i++)
- {
- for (int j=i+1;j<n;j++)
- {
- if (pArr[j]>pArr[i])
- {
- int temp=0;
- temp=pArr[j];
- pArr[j]=pArr[i];
- pArr[i]=temp;
- }
- }
- }
- }
另一份我认为更有价值的答案:
- #include<stdio.h>
- #define N 50
- void Sort(int a[],int n);
- int main(void)
- {
- char str[100];
- int a[N]={0};
- gets_s(str); //要点1:动态的输入1--50个整数,不能确定个数,只能用字符串输入,然后分离出来
- int i=0,j=0;
- int sign=1;
- while(str[i]!='\0') //是末尾则终止
- {
- if(str[i]!=',') //输入时要在半角输入,首先判断是否是逗号
- {
- if(str[i] == '-') //要点:2:有负整数的输入 ,在判断是否是负号
- {
- // i++; //易错点1
- sign=-1;
- }
- else if(str[i]!='\0'||str[i]!=',') //不用else的话,负号也会减去‘0’
- {
- a[j]=a[j]*10 + str[i]-'0'; //要点3:输入的可以是多位数
- }
- }
- i++;
- if(str[i]==',' || str[i]=='\0') //这个判断是在i自加以后
- {
- a[j]=a[j]*sign; //易错点2
- sign=1; 易错点3
- j++; //j就是a数组的个数 范围0到j-1
- }
- }
- Sort(a,j); //排序
- printf("Max number + Min number = %d",a[0]+a[j-1]);
- getchar();
- return 0;
- }
- void Sort(int a[],int n) //选择排序
- {
- int i,j;
- int k;
- int temp;
- for(i=0;i<n-1;i++)
- {
- k=i;
- for(j=i+1;j<n;j++)
- {
- if(a[k]>a[j])
- k=j;
- }
- if(i!=k)
- {
- temp = a[k];
- a[k] = a[i];
- a[i] = temp;
- }
- }
- for(i=0;i<n;i++)
- printf("%-5d",a[i]);
- puts("");
- }
试题2:
通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。
补充说明:
1、操作数为正整数,不需要考虑计算结果溢出的情况。
2、若输入算式格式错误,输出结果为“0”。
要求实现函数:
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“4 + 7” 输出:“11”
输入:“4 - 7” 输出:“-3”
输入:“9 ++ 7” 输出:“0” 注:格式错误
- #include "stdafx.h"
- #include "iostream"
- #include <stdlib.h>
- #define MAX 10
- using namespace std;
- void help()
- {
- cout<<"注意:"<<endl;
- cout<<" 1,操作数”与“运算符”之间以一个空格隔开。"<<endl;
- cout<<" 2,操作数为正整数,不需要考虑计算结果溢出的情况。"<<endl;
- cout<<" 3,若输入算式格式错误,输出结果为“0”。"<<endl;
- cout<<endl;
- cout<<"请输入格式为:“操作数1 运算符 操作数2”的字符串(不包括引号)"<<endl;
- }
- void Arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
- int _tmain(int argc, _TCHAR* argv[])
- {
- help();
- char srcStr[MAX]="71 + 4";
- char outStr[MAX]={0};
- int len=0;
- gets_s(srcStr);
- len=strlen(srcStr);
- Arithmetic(srcStr,len,outStr);
- cout<<outStr;
- getchar();
- return 0;
- }
- void Arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
- {
- const char *input = pInputStr;
- char *output = pOutputStr;
- char oper[MAX]={0};
- int i=0,j=0,k=0;
- int num[MAX]={0};
- while (input[i]!='\0')
- {
- while (input[i]!=' ')
- {
- num[j]=num[j]*10+input[i]-'0';
- i++;
- }
- for (;('0'>= input[i])||('9'<=input[i]);i++,k++)
- {
- oper[k]=input[i];
- }
- j++;
- while(input[i]!='\0')
- {
- num[j]=num[j]*10+input[i]-'0';
- i++;
- }
- }
- int sum=0;
- for (int m=0;m<=k;m++)
- {
- sum=sum+oper[m];
- }
- switch (sum)
- {
- case 107:
- _itoa_s(num[0]+num[1],pOutputStr,10,10);
- break;
- case 109:
- _itoa_s(num[0]-num[1],pOutputStr,10,10);
- //*pOutputStr=char(num[0]-num[1]);
- break;
- default:
- output[0] = '0';
- return;
- }
- }
另一份我认为比较有价值的答案:
- #include <iostream>
- using namespace std;
- void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
- {
- const char *input = pInputStr;
- char *output = pOutputStr;
- int sum = 0;
- int operator1 = 0;
- int operator2 = 0;
- char *temp = new char[5];
- char *ope = temp;
- while(*input != ' ') //获得操作数1
- {
- sum = sum*10 + (*input++ - '0');
- }
- input++;
- operator1 = sum;
- sum = 0;
- while(*input != ' ')
- {
- *temp++ = *input++;
- }
- input++;
- *temp = '\0';
- if (strlen(ope) > 1 )
- {
- *output++ = '0';
- *output = '\0';
- return;
- }
- while(*input != '\0') //获得操作数2
- {
- sum = sum*10 + (*input++ - '0');
- }
- operator2 = sum;
- sum = 0;
- switch (*ope)
- {
- case '+':itoa(operator1+operator2,pOutputStr,10);
- break;
- case '-':itoa(operator1-operator2,pOutputStr,10);
- break;
- default:
- *output++ = '0';
- *output = '\0';
- return;
- }
- }
- int main()
- {
- char input[] = "4 - 7";
- char output[] = " ";
- arithmetic(input,strlen(input),output);
- cout<<output<<endl;
- return 0;
- }
试题3:
1.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:void stringFilter(constchar *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“deefd” 输出:“def”
输入:“afafafaf” 输出:“af”
输入:“pppppppp” 输出:“p”
main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出
当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。
一定要保证编译运行不受影响
- #include "stdafx.h"
- #include <iostream>
- #include <cassert>
- using namespace std;
- bool g_flag[26];
- void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
- int _tmain(int argc, _TCHAR* argv[])
- {
- memset(g_flag,0,sizeof(g_flag));
- char input[] = "abacacde";
- char *output = new char[strlen(input) + 1];
- stringFilter(input,strlen(input),output);
- cout<<output<<endl;
- delete output;
- return 0;
- }
- <pre name="code" class="html">void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
- {
- const char *input = pInputStr;
- char *output = pOutputStr;
- int len=lInputLen;
- int i=0,j=0;
- for (;i<len;i++)
- { for (j=len-1;j>i;j--)
- {
- if (input[i]==input[j])
- g_flag[j]=true;
- }
- }
- int m=0,n=0;
- do
- {
- if (g_flag[m]==false)
- {
- pOutputStr[n]=input[m];
- n++;
- }
- m++;
- } while (m<len);
- pOutputStr[len-n+1]='\0';
- /*
- for (int k=len-n+1;k<len;k++)
- {
- pOutputStr[k]='\0';
- }
- */
- }
另一份比较有价值的答案:
- #include <iostream>
- #include <cassert>
- using namespace std;
- bool g_flag[26];
- void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
- {
- assert(pInputStr != NULL);
- int i = 0;
- if (pInputStr == NULL || lInputLen <= 1)
- {
- return;
- }
- const char *p = pInputStr;
- while(*p != '\0')
- {
- if (g_flag[(*p - 'a')])
- {
- p++;
- }else{
- pOutputStr[i++] = *p;
- g_flag[*p - 'a'] = 1;
- p++;
- }
- }
- pOutputStr[i] = '\0';
- }
- int main()
- {
- memset(g_flag,0,sizeof(g_flag));
- char input[] = "abacacde";
- char *output = new char[strlen(input) + 1];
- stringFilter(input,strlen(input),output);
- cout<<output<<endl;
- delete output;
- return 0;
- }
试题4:
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1、仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc"。
2、压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"。
要求实现函数:
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“cccddecc” 输出:“3c2de2c”
输入:“adef” 输出:“adef”
输入:“pppppppp” 输出:“8p”
(此代码不能统计超过两位数相邻的,比如有11个a相邻,只能得到1a,而不是11a)
- // ConsoleAppStringZip.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "iostream"
- using namespace std;
- void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
- int _tmain(int argc, _TCHAR* argv[])
- {
- char input[] = "aaaaaaaaacce";
- char *output = new char[strlen(input) + 1];
- stringZip(input,strlen(input),output);
- cout<<output<<endl;
- getchar();
- return 0;
- }
- void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
- {
- const char *input = pInputStr;
- char *output = pOutputStr;
- long len=lInputLen;
- int k=0,step=0,flag=0;
- int j=0;
- for (int i=0;i<len;i++)
- {
- step=1;
- j=i+flag;
- while (input[j]==input[j+1])
- {
- step++;
- flag++;
- j++;
- }
- output[k]='0'+step%10;
- k++;
- output[k]=input[j];
- k++;
- if (input[j+1]=='\0')
- {
- output[k]='\0';
- break;
- }
- }
- }
另一份比较有价值的答案:
- #include <iostream>
- #include <cassert>
- using namespace std;
- void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
- {
- const char *p = pInputStr;
- int num = 1;
- int i = 0;
- p++;
- while(*p != NULL)
- {
- while(*p == *(p-1)&& *p != NULL)
- {
- num++;
- p++;
- }
- if (num > 1)
- {
- int size = 0;
- int temp = num;
- while(num) //计算位数
- {
- size++;
- num /= 10;
- }
- num = 1;
- for (int j = size; j > 0; j--)
- {
- pOutputStr[i+j-1] = '0'+ temp%10;
- temp /= 10;
- }
- i +=size;
- pOutputStr[i++] = *(p-1);
- p++;
- }else{
- pOutputStr[i++] = *(p-1);
- p++;
- }
- }
- pOutputStr[i] = '\0';
- }
- int main()
- {
- char input[] = "cccddecc";
- char *output = new char[strlen(input) + 1];
- stringZip(input,strlen(input),output);
- cout<<output<<endl;
- return 0;
- }