标签:
注:编译环境为Visual Studio 2012,答案仅供参考。
46.在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
要求实现函数:
void my_word(charinput[], char output[])
【输入】 char input[], 输入的字符串
【输出】 char output[],输出的字符串
【返回】 无
#include <iostream>
#include <string>
#include <list>
using namespace std;
bool isLetter(char c){
return (c >= ‘a‘ && c <= ‘z‘);
}
void my_word(char *input,char* output){
list<string> v;
while (*input != ‘\0‘)
{
if (!isLetter(*input))
{
input++;
continue;
}
char *ahead = input;
while (*ahead != ‘\0‘ && isLetter(*ahead))
{
ahead++;
}
string tmp(input,ahead);
auto b = v.begin();
bool flag = true;
while (b != v.end())
{
if ((*b).size() < tmp.size())
{
break;
}
if ((*b).size() == tmp.size())
{
if (*b == tmp)
{
flag = false;
break;
}
}
b++;
}
if (flag) v.insert(b, tmp);
input = ahead;
}
auto b = v.begin();
while ( b != v.end())
{
for (unsigned j = 0; j < (*b).size(); j++)
{
*output++ = (*b)[j];
}
b++;
if (b != v.end())
{
*output++ = ‘ ‘;
}
}
*output = ‘\0‘;
}
int main()
{
char input[100],output[100];
cin.getline(input,100);
my_word(input, output);
cout << output << endl;
return 0;
}
47.数组中数字都两两相同,只有一个不同,找出该数字:
#include <iostream>
using namespace std;
int findDiffence(int a[], int n){
int sum = 0;
for (int i = 0; i < n; i++)
{
sum ^= a[i];
}
return sum;
}
int main()
{
int a[] = {2,8,5,4,5,2,8};
cout << findDiffence(a,7) << endl;
return 0;
}
48.数组中数字两两相同,有两个不同,找出这两个
#include <iostream>
using namespace std;
int findDiffence(int a[], int n){
int sum = 0;
for (int i = 0; i < n; i++)
{
sum ^= a[i];
}
return sum;
}
void findTwoDiffence(int a[], int n, int &result1, int &result2){
int *tmp1 = new int[n], n1 = 0;
int *tmp2 = new int[n], n2 = 0;
int sum = 0;
for (int i = 0; i < n; i++)
{
sum ^= a[i];
}
int cmp = sum & a[0];
for (int i = 0; i < n; i++)
{
if ((sum & a[i]) == cmp)
{
tmp1[n1++] = a[i];
}
else
{
tmp2[n2++] = a[i];
}
}
result1 = findDiffence(tmp1,n1);
result2 = findDiffence(tmp2,n2);
delete []tmp1;
delete []tmp2;
}
int main()
{
int a[] = {21,18,35,4,35,21,18,6};
int result1, result2;
findTwoDiffence(a,8,result1,result2);
cout << result1 << " " << result2 << endl;
return 0;
}
49.统计数字出现的次数,最大次数的统计出来
举例:
输入:323324423343
输出:3,6
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s;
int a[10];
for (int i = 0; i < 10; i++)
{
a[i] = 0;
}
bool flag = true;
for (unsigned i = 0; i < s.size(); i++)
{
if (s[i] < ‘0‘ || s[i] > ‘9‘)
{
flag = false;
cout << "ERROR" << endl;
break;
}
a[s[i] - ‘0‘]++;
}
if (!flag) return 0;
int max = a[0];
for (int i = 1; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
for (int i = 0; i < 10; i++)
{
if (a[i] == max)
{
cout << i << "," << a[i] << endl;
}
}
return 0;
}
50.字符串首字母转换成大写
举例:
输入:this is a book
返回:This Is A Book
bool isLetter(char c){
return (c >= ‘a‘ && c <= ‘z‘);
}
void convert(char *input, char *output){
while (*input != ‘\0‘)
{
if (!isLetter(*input))
{
*output++ = *input++;
continue;
}
*output++ = toupper(*input++);
while (*input != ‘\0‘ && isLetter(*input))
{
*output++ = *input++;
}
}
*output = ‘\0‘;
}
int main()
{
char input[100],output[100];
cin.getline(input,100);
convert(input,output);
cout << output << endl;
return 0;
}
标签:
原文地址:http://blog.csdn.net/sps900608/article/details/52199862