华为上机题汇总(十一)
注:编译环境为Visual Studio 2012,答案仅供参考。
目录
- 华为上机题汇总十一
- 目录
- 第五十一题
- 第五十二题
- 第五十三题
- 第五十四题
- 第五十五题
第五十一题
51.子串分离
题目描述:
通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一
个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个’,’并将子
串存储。
如果输入“abc def gh i d”,结果将是abc,def,gh,i,d,
要求实现函数:
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
#include <iostream>
using namespace std;
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr){
while (*pInputStr != ‘\0‘)
{
if (*pInputStr == ‘ ‘)
{
pInputStr++;
continue;
}
while (*pInputStr != ‘\0‘ && *pInputStr != ‘ ‘)
{
*pOutputStr++ = *pInputStr++;
}
*pOutputStr++ = ‘,‘;
}
*pOutputStr = ‘\0‘;
}
int main()
{
char input[100],output[100];
cin.getline(input,100);
DivideString(input,strlen(input),output);
cout << output << endl;
return 0;
}
第五十二题
52.链表翻转。
给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现
思想:采用遍历链表,分成length/k组,对每组进行逆转,逆转的同时要将逆转后的尾和头连接起来
#include <iostream>
using namespace std;
bool invalidInput = false;
struct LNode{
int value;
LNode* next;
};
LNode* reverKNodes(LNode *begin, int k){
LNode *head = begin->next, *front = head, *p = front->next;
for (int i = 0; i < k-1; i++)
{
LNode *after = p->next;
p->next = front;
front = p;
p = after;
}
begin->next = front;
head->next = p;
return head;
}
LNode* reverseList(LNode *phead, int k){
if (!phead || k < 0)
{
invalidInput = true;
return phead;
}
LNode *p = phead;
bool end = false;
while (p->next != NULL)
{
LNode *pAhead = p;
for (int i = 0; i < k; i++)
{
if (pAhead->next == NULL){
end = true;
break;
}
pAhead = pAhead->next;
}
if (end) break;
p = reverKNodes(p,k);
}
return phead;
}
int main()
{
LNode node6;
node6.value = 6;
node6.next = NULL;
LNode node5;
node5.value = 5;
node5.next = &node6;
LNode node4;
node4.value = 4;
node4.next = &node5;
LNode node3;
node3.value = 3;
node3.next = &node4;
LNode node2;
node2.value = 2;
node2.next = &node3;
LNode node1;
node1.value = 1;
node1.next = &node2;
LNode headNode;
LNode *phead = &headNode;
phead->next = &node1;
int k;
cin >> k;
reverseList(phead,k);
while (phead->next != NULL)
{
phead = phead->next;
cout << phead->value << " ";
}
cout << endl;
return 0;
}
第五十三题
53.链表相邻元素翻转
如a->b->c->d->e->f->g,翻转后变为:b->a->d->c->f->e->g
#include <iostream>
using namespace std;
bool invalidInput = false;
struct LNode{
char value;
LNode* next;
};
LNode* reverKNodes(LNode *begin){
LNode *head = begin->next, *front = head, *current = front->next, *after = current->next;
current->next = front;
begin->next = current;
head->next = after;
return head;
}
LNode* reverseList(LNode *phead){
if (!phead)
{
invalidInput = true;
return phead;
}
LNode *p = phead;
bool end = false;
while (p->next != NULL)
{
LNode *pAhead = p;
for (int i = 0; i < 2; i++)
{
if (pAhead->next == NULL){
end = true;
break;
}
pAhead = pAhead->next;
}
if (end) break;
p = reverKNodes(p);
}
return phead;
}
int main()
{
LNode node7;
node7.value = ‘g‘;
node7.next = NULL;
LNode node6;
node6.value = ‘f‘;
node6.next = &node7;
LNode node5;
node5.value = ‘e‘;
node5.next = &node6;
LNode node4;
node4.value = ‘d‘;
node4.next = &node5;
LNode node3;
node3.value = ‘c‘;
node3.next = &node4;
LNode node2;
node2.value = ‘b‘;
node2.next = &node3;
LNode node1;
node1.value = ‘a‘;
node1.next = &node2;
LNode headNode;
LNode *phead = &headNode;
phead->next = &node1;
reverseList(phead);
while (phead->next != NULL)
{
phead = phead->next;
cout << phead->value << " ";
}
cout << endl;
return 0;
}
第五十四题
54.求最长连续子串
#include <iostream>
#include <string>
using namespace std;
void maxLengthSub(const string &s1, string &s2){
auto begin = s1.begin();
string maxStr;
while (begin != s1.end())
{
if (*begin == ‘ ‘)
{
begin++;
continue;
}
auto ahead = begin + 1;
while (ahead != s1.end() && *ahead != ‘ ‘)
{
ahead++;
}
if (ahead - begin > maxStr.size())
{
maxStr = string(begin,ahead);
}
begin = ahead;
}
s2 = maxStr;
}
int main()
{
string s1, s2;
getline(cin,s1);
maxLengthSub(s1,s2);
cout << s2 << endl;
return 0;
}
第五十五题
55.描述: 自从有了智能手机,时刻都要关心手机的电量。你的任务很简单,用程序打印符号来表示当前手机的电量。
用10行和10列来表示电池的电量,同时在外围加上边框,每一行表示10%的电量。
假设还有60%的电量,则显示如下:
+----------+
|----------|
|----------|
|----------|
|----------|
|++++++++++|
|++++++++++|
|++++++++++|
|++++++++++|
|++++++++++|
|++++++++++|
+----------+
运行时间限制: 无限制
内存限制: 无限制
输入: 多组测试数据,第一行为测试数据组数N(N<10),紧接着是N行,每行一个数,表示电量,这个数值可能是0,10,20 ,30,40,50,60,70,80,90,100
输出: 每组数据输出一个电池的电量,每组数据之间用15个“=”隔开
样例输入: 2
50
0
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s[] = {"+----------+","|----------|","|++++++++++|","==============="};
void display(int n){
if (n < 0 || n > 10)
{
return;
}
cout << s[0] << endl;
for (int i = 0; i < 10-n; i++)
{
cout << s[1] << endl;
}
for (int i = 0; i < n; i++)
{
cout << s[2] << endl;
}
cout << s[0] << endl;
}
int main()
{
vector<int> v;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int buttery;
cin >> buttery;
v.push_back(buttery/10);
}
for (unsigned i = 0; i < v.size(); i++)
{
display(v[i]);
if (i != v.size()-1)
{
cout << s[3] << endl;
}
}
cout << endl;
return 0;
}