目录
- 1.字符串最后一个单词
- 2.计算字符个数
- 3.排序与去重
- 4.字符串分割
- 5.进制转换
- 6.质数因子
- 7.取近似值
- 8.合并表记录
- 9.提取不重复的整数
- 10.字符个数统计
- 11.数字颠倒
- 12.字符串反转
- 13.句子逆序
- 14.字符串的连接最长路径查找
- 15.求int型正整数在内存中存储时1的个数
- 16.购物单
- 17.坐标移动
- 18.密码验证合格程序
- 19.删除字符串中出现次数最少的字符
- 20.字符串排序–非字母数字顺序不变
- 21.查找兄弟单词
- 22.字符串加解密
- 23.简单密码
- 24.字符串合并处理
- 25.回文字符串
- 26.字符串加密
- 27.统计每个月兔子的总数
- 28.求小球落地5此后所经历的路程和第五次反弹的高度
- 29.统计字符串中不同类别字符的个数
- 30.按字节截取字符串
- 31.数组中重复的数字
1.字符串最后一个单词
计算并输出字符串最后一个单词的长度
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[5001];
//输入字符串
gets(str); // cin不接受空格、tab键的输入
//计算并输出字符串最后一个单词的长度
cout << str << endl;
int len =0;
for(int i = strlen(str) - 1; str[i] != ' ' && i >= 0; i--)
len++;
cout << len << endl;
return 0;
}
2.计算字符个数
写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出字符串中含有该字符的个数;不区分大小写
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[100];
char ch;
// 输入一个由字母和数字组成的字符串
cin >> str;
cin >> ch;
cout << "str = " << str << '\n' << "ch = " << ch << endl;
// 输出输入字符串中含有该字符的个数
int count = 0;
// int i = 0;
// while(str[i] != '\0')
// {
// if(str[i] == ch || str[i] == ch + 32 || str[i] == ch - 32)
// count++;
// i++;
// }
for(int i = 0; str[i] != '\0'; i++)
{
if(str[i] == ch || str[i] == ch + 32 || str[i] == ch - 32)
count++;
}
cout << count << endl;
return 0;
}
3.排序与去重
用计算机生成了N个1到1000之间的随机整数(N<=1000),对于其中重复的数字,只保留一个,把其余的相同的数去掉;然后再把这些数从小到大排序,请完成以上问题中的“去重”和“排序”工作
测试样例1: 3个数字, 分别是2,2,1
测试样例2:11个数字,分别是10,20,40,32,67,40,20,89,300,400,15
#include <iostream>
using namespace std;
int main()
{
int num, n;
while(cin >> num)
{
int a[1001]={
0}; //hash表思路,复杂度为o(n)
while(num--)
{
cin >> n;
a[n] = 1;
}
for(int i = 1; i < 1001; i++)
if(a[i] == 1)
cout << i << endl;
}
return 0;
}
注意:该题采用了哈希表思路,定义的数组必须初始化,否则内存中之前存入数据将会参与运算,否运行将会出错。
4.字符串分割
连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;长度不是8整数倍的字符串请在后面补数字0,空字符串不处理
输入 abc 123456789 输出 abc00000 12345678 900000000
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
while(getline(cin, str))
{
//循环 截取前八个,
while(str.size() > 8)
{
cout << str.substr(0, 8) << endl; //获得字符串从第0位开始长度为8的字符串
str = str.substr(8); //获得字符串从第8位开始到尾的字符串
}
cout << str.append(8 - str.size(), '0') << endl;
}
return 0;
}
length()与size()没有区别,都是返回string对象中元素数量,即返回std::distance(begin(),end()) 。length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。
str.substr()函数
- 形式:str.substr(pos, n)
- 解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
- 补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
str.append()函数
append函数是向string的后面追加字符或字符串。
-
向string的后面加C-string
string s = “hello “; const char *c = “out here “;
s.append©; // 把c类型字符串s连接到当前字符串结尾
s = “hello out here”; -
向string的后面加C-string的一部分
string s=”hello “;const char *c = “out here “;
s.append(c,3); // 把c类型字符串s的前n个字符连接到当前字符串结尾
s = “hello out”;
3).向string的后面加string
string s1 = “hello “; string s2 = “wide “; string s3 = “world “;
s1.append(s2); s1 += s3; //把字符串s连接到当前字符串的结尾
s1 = “hello wide “; s1 = “hello wide world “; -
向string的后面加string的一部分
string s1 = “hello “, s2 = “wide world “;
s1.append(s2, 5, 5); 把字符串s2中从5开始的5个字符连接到当前字符串的结尾
s1 = “hello world”;
string str1 = “hello “, str2 = “wide world “;
str1.append(str2.begin()+5, str2.end()); //把s2的迭代器begin()+5和end()之间的部分连接到当前字符串的结尾
str1 = “hello world”; -
向string后面加多个字符
string s1 = “hello “;
s1.append(4,’!’); //在当前字符串结尾添加4个字符!
s1 = “hello !!!”;
————————————————
转载原文链接:https://blog.youkuaiyun.com/wxn704414736/article/details/78551886
5.进制转换
写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入)
输入0xA ,输出 10
#include <iostream>
using namespace std;
int main()
{
string str;
while(getline(cin, str))
{
if(str.size() < 0)
break;
int num = 0;
for(int i =2; i < str.size(); i++)
{
if(str[i] >= '0' && str[i] <= '9')
num = num * 16 + str[i] - '0';
else
num = num * 16 + str[i] - 'A' + 10;
}
cout << num << endl;
}
return 0;
}
6.质数因子
输入一个long型正整数,按照从小到大的顺序输出它的所有质因子(如180的质因子为2 2 3 3 5),最后一个数后面也要有空格
#include <iostream>
using namespace std;
int main()
{
long num;
while(cin >> num)
{
//for循环从小到大的质数因子
for(int i = 2; i * i < num; i++) // 使用i^2 < num 比i <= num时间复杂度低
{
while(num % i == 0)
{
cout << i << ' ';
num /= i;
}
}
//数字本身就是质数
if(num > 1)
cout << num << ' ';
}
return 0;
}
7.取近似值
写出一个程序,接受一个正浮点数值,输出该数值的近似整数值
验证:输入5.5 输出6
#include <iostream>
using namespace std;
int main()
{
double num; //一般情况下不用float,因为float强转会产生很多问题
cin >> num;
int a = (int)num; //强转
if((num - a) * 10 >= 5)
cout << a + 1 << endl;
else if((num - a) * 10 <= -5)
cout << a - 1 << endl;
else
cout << a << endl;
return 0;
}
8.合并表记录
数据表记录包含包含表索引和数值(int范围的整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出
输入描述:先输入键值对的个数,然后输入成对的index和value值,以空格隔开
输出描述:输出合并后的键值对(多行)
示例:
输入:4
0 1;0 2; 1 2;3 4
输出:0 3;1 2;3 4
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int num; //键值对数
cin >> num;
unordered_map<int, int> hash;
vector<int> key_map;
while(num--)
{
int key, value;
cin >> key >> value;
key_map.push_back(key); //有重复的
hash[key] += value;
}
//去重
sort(key_map.begin(), key_map.end());
key_map.erase(unique(key_map.begin(), key_map.end()), key_map.end());
//遍历打印
for(vector<int>::iterator iter = key_map.begin(); iter != key_map.end(); ++iter)
cout << *iter << " " << hash[*iter] << endl;
return 0;
}
unordered_map内部实现了一个哈希表(也叫散列表,通过把关键码值映射到Hash表中一个位置来访问记录,查找的时间复杂度可达到O(1),其在海量数据处理中有着广泛应用)。因此,其元素的排列顺序是无序的。
unorder_map库详细介绍
vector库详细介绍
sort函数详细介绍
unique函数详细介绍
9.提取不重复的整数
输入一个intx型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数
验证:输入 9876673 输出37689
/** 9.提取不重复的整数 */
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<int, int> hash;
int num;
int out = 0;
cin >> num;
while(num)
{
if(hash[num % 10] == 0)
{
hash[num % 10]++;
out = out * 10 + num % 10;
}
num /= 10;
}
cout << out << endl;
return 0;
}
10.字符个数统计
编写一个函数,计算字符串中含有的不同字符的个数。字符在ASCII码范围内(0~127),换行表示结束符,不在范围内的不作统计
验证:输入 abc 输出 3
采用数组方法
#include <iostream>
using namespace std;
int main()
{
char ch[500];
int arr[128] = {
0};
int count = 0;
gets(ch);
for(int i = 0; ch[i] != '\0'; i++)
{
arr[ch[i]]++;
}
for(int k = 0; k < 128; k++)
{
if(arr[k] > 0)
count++;
}
cout << count << endl;
return 0;
}
采用hash表
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
int sum = 0;
unordered_map<char, int> hash;
char ch[500];
cin >> ch;
for(int i = 0; ch[i] != '\0'; i++)
{
if(ch[i] >= 0 && ch[i] <= 127)
hash[ch[i]] = 1;
}
for(int k = 0; k < hash.size(); k++)
{
if(hash[k] ==