腾讯2017暑假实习生编程题----字符换位
来源:互联网 发布:搜客云优化 编辑:程序博客网 时间:2024/05/22 05:02
题目:小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个空间字符的相对位置不变,且不能申请额外的空间。你能帮帮小Q吗?
例如:OkhaoPingCeilXu 转换后 khaoingeiluOPCX
看到这个题目时,就会想到使用冒泡排序,考虑到一些同学之前没有了解过什么是冒泡排序,那就借此机会来说明一下。
冒泡排序(bubble sort)这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端。
由于本人上课时,老师使用英文授课,所以下面就用英文也来解释吧!
The bubble sort simply compares adjacent elements and exchange them if they are out of order. To do this, you need to make several passes over the data.
During the first pass, you compare the first two elements in the list.
(1) If they are out of order, you exchange them.
(2) Then you compare the next pair of elements (position 2 and 3).
(3) If they are out of order, you exchange them.
(4) This algorithm continues comparing and exchanging pairs of elements until you reach the end of the list.
我看到这个题目时,就考虑到使用C++中的头文件里一个“cctype”, 大概是我的老师经常让我们使用这个函数头文件来进行做实验和做作业,所以就很自然的想到了。这个头文件里包含了一个“isupper”的函数,所以可以直接使用这个函数来判断这个字母是否是大写字母,这样比较方便快捷。
下面是代码的展示,代码在Linux环境下能运行。
#include<iostream>
#include<cctype>
using namespace std;
void Sort_array(char arr[],int len);
int main()
{
cout<<"How many data do you want to add: ";
int len;
cin>>len;
char arrCha[len];
for(int i=0; i<len; i++)
{
cout<<"Enter the data into the array: ";
cin>>arrCha[i];
}
cout<<endl;
cout<<"This is the prior data in the array: ";
for(int k=0; k<len; k++)
{
cout<<arrCha[k];
}
cout<<endl;
Sort_array(arrCha,len);
int j;
cout<<"After sorting: ";
for(j=0;j<len;j++)
{
cout<<arrCha[j]<<" ";
}
cout<<endl;
return 0;
}
void Sort_array(char ar[],int len)
{
int i,j;
char temp;
for(i=0;i<len;i++)
{
for(j=0;j<len-1;j++)
{
if((isupper(arr[j])) && (islower(arr[j+1])))
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
这个程序可以让用户(小Q)随便输入任何长度的字符串,都会满足用户的要求。
By the way,在linux环境中,要编译和运行一个程序,就得先打g++ <filename> (where the filename ends with ".cpp" or the ".h" file) 进行编译,然后再打./a.out 来进行运行。
下面是运行的结果,为了测试此程序是否能运行输出的结果跟题目的例子一样,不知道为什么我从Linux环境里将运行结果进行截图,粘贴到这里就无法显示,所以只能一个一个的把结果写出来,但是是能运行的。
How many data do you want to add: 15
Enter the data into the array: O
Enter the data into the array: k
Enter the data into the array: h
Enter the data into the array: a
Enter the data into the array: o
Enter the data into the array: P
Enter the data into the array: i
Enter the data into the array: n
Enter the data into the array: g
Enter the data into the array: C
Enter the data into the array: e
Enter the data into the array: i
Enter the data into the array: l
Enter the data into the array: X
Enter the data into the array: u
This is the prior data in the array: OkhaoPingCeilXu
After sorting: k h a o i n g e i l u O P C X
Enter the data into the array: O
Enter the data into the array: k
Enter the data into the array: h
Enter the data into the array: a
Enter the data into the array: o
Enter the data into the array: P
Enter the data into the array: i
Enter the data into the array: n
Enter the data into the array: g
Enter the data into the array: C
Enter the data into the array: e
Enter the data into the array: i
Enter the data into the array: l
Enter the data into the array: X
Enter the data into the array: u
This is the prior data in the array: OkhaoPingCeilXu
After sorting: k h a o i n g e i l u O P C X
很显然,这个程序输出的结果跟例题的结果一模一样。因为这程序里使用了cout<<arrCha[j]<<" ";所以在排序完之后,每一个之间都会有一个空格,这样便于用户的查看。
然后再测一下,如果用户随便输入一个字符串,还会不会满足这个题目要求呢?
How many data do you want to add: 5
Enter the data into the array: e
Enter the data into the array: R
Enter the data into the array: e
Enter the data into the array: V
Enter the data into the array: h
This is the prior data in the array: eReVh
After sorting: e e h R V
Enter the data into the array: e
Enter the data into the array: R
Enter the data into the array: e
Enter the data into the array: V
Enter the data into the array: h
This is the prior data in the array: eReVh
After sorting: e e h R V
很显然,结果满足题目的要求,所以这个程序是正确的。
希望这篇文章对各位有所帮助!!
2 0
- 腾讯2017暑假实习生编程题----字符换位
- 【真题】腾讯2017暑假实习生编程题 算法基础-字符移位
- 腾讯2017暑假实习生编程题有趣的数字
- 腾讯2017暑假实习生编程题构造回文
- 腾讯2017 暑假实习生编程题---【3】 有趣的数字
- 腾讯2017暑假实习生招聘笔试编程题
- 腾讯2017暑假实习生招聘笔试编程题(1)
- 腾讯2017暑假实习生招聘笔试编程题(2)
- 腾讯2017暑期实习生编程题-字符移位
- 算法基础-字符移位----腾讯2017暑期实习生编程题
- 腾讯2017暑期实习生编程题-字符移位
- 腾讯2017暑期实习生编程题--算法基础-字符移位
- 腾讯2017暑假实习生编程题字符移位
- 腾讯 2017 暑假实习生编程题(一):给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢? 输出需要删除的字符个数。
- 腾讯2017实习生编程之算法基础-字符移位
- 2016腾讯实习生笔试编程题:字符移位
- 腾讯2017暑期实习生编程题
- 腾讯2017暑期实习生编程题
- 几种常用的排序算法源代码以及效率比较
- 航空公司VIP客户查询
- 快速上手使用的基础脚手架webpack2-react-tools
- RecycleView添加下滑到底部的监听
- pyspark notebook的使用
- 腾讯2017暑假实习生编程题----字符换位
- 在tomcat里面配置 maven项目
- HBase性能优化方法总结(一):表的设计
- Android之动画(二)
- 字符串匹配的KMP算法
- 使用Mock.js生成前端测试数据
- This National Puppy Day
- eclipse集成tomcat、maven、git环境搭建
- Activity学习笔记