21、简单密码
#include
#include
using namespace std;
int main()
{
string str;
while(cin >> str)
{
//进行加密
//小写字母变成九宫格数字
// 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0
for(int i = 0; i < str.size(); i ++)
{
if(str[i] >= 'a' && str[i] <= 'c')
str[i] = '2';
if(str[i] >= 'd' && str[i] <= 'f')
str[i] = '3';
if(str[i] >= 'g' && str[i] <= 'i')
str[i] = '4';
if(str[i] >= 'j' && str[i] <= 'l')
str[i] = '5';
if(str[i] >= 'm' && str[i] <= 'o')
str[i] = '6';
if(str[i] >= 'p' && str[i] <= 's')
str[i] = '7';
if(str[i] >= 't' && str[i] <= 'v')
str[i] = '8';
if(str[i] >= 'w' && str[i] <= 'z')
str[i] = '9';
//大写字母变成小写后移一位
if(str[i] == 'Z')
str[i] = 'a';
else if(str[i] >= 'A' && str[i] <= 'Y')
{
str[i] = str[i] + 33;
}
}
cout << str << endl;
}
return 0;
}
22、汽水瓶
#include
using namespace std;
int main()
{
int empty;//空瓶子数量
int res = 0;//总共换了多少瓶
while(cin >> empty)
{
int new1, hold;
while(empty >= 2)
{
//只有2瓶
//借一瓶 还3空
if(empty == 2)
res++, empty = 0;
else
{
new1 = empty / 3;
hold = empty % 3;
res += new1;
empty = new1 + hold;
}
}
cout << res << endl;
res = 0;
}
return 0;
}
23、删除字符串中出现次数最少的字符
#include
#include
using namespace std;
//统计次数 hash表
int main()
{
string str;
int minnum;
while(cin >> str)
{
unordered_map<char, int> hash;//在while循环外面 hash表一直存在 多组输入 上一组输入还在
//初始化hash表
for(int i = 0; i <=str.size(); i ++)
{
hash[str[i]] ++;
}
minnum = hash[str[0]];
//对比找到最小的
for(int i = 1; i <=str.size(); i ++)
minnum = min(minnum, hash[str[i]]);
//符合条件的输出
for(int i = 0; i <=str.size(); i ++)
if(hash[str[i]] > minnum)
cout << str[i];
cout << endl;
}
return 0;
}
24、合唱队
#include
#include
#include
using namespace std;
void calcu(vector<int> height, vector<int> &incnum)
{
//计算以height为最高的递增队列人数
for(int i = 1; i < height.size(); i++)
{
for(int j = i - 1; j >= 0; j--)
{
if(height[j] < height[i] && incnum[i] < incnum[j] + 1)
incnum[i] = incnum[j] + 1;
}
}
}
int main()
{
int n;
while(cin >> n)
{
vector<int> height;
vector<int> incnum(n, 1);
vector<int> decnum(n, 1);
vector<int> realnum;
int h;
for(int i = 0; i < n; i++)
cin >> h, height.push_back(h);
//求递增序列数组人数
calcu(height, incnum);
//求递减序列数组人数
reverse(height.begin(), height.end());
calcu(height, decnum);
reverse(decnum.begin(), decnum.end());
int maxnum = 0;
for(int i = 0; i < n; ++i)
{
realnum.push_back(incnum[i] +decnum[i] - 1);//减掉自身的
maxnum = max(maxnum, realnum[i]);
}
cout << n - maxnum << endl;
}
return 0;
}
25、数据分类处理
#include
#include
#include
#include
using namespace std;
bool match(int r, int i)
{
string strr = to_string(r);
string stri = to_string(i);
int pos = stri.find(strr);
if(pos != -1)
return true;
else
return false;
}
int main()
{
int Inum, Rnum;
//15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123
//5 6 3 6 3 0
while(cin >> Inum)
{
vector<int> I, R;
for(int i = 0; i < Inum; i++)
{
int temp;
cin >> temp;
I.push_back(temp);
}
cin >> Rnum;
for(int i = 0; i < Rnum; i++)
{
int temp;
cin >> temp;
R.push_back(temp);
}
//R排序、去重
sort(R.begin(), R.end());
R.erase(unique(R.begin(), R.end()), R.end());
vector<int> value, cnt, index, index1;
for(int i = 0; i < R.size(); i++)
{
int cnt1= 0;//记录R[i]在I里面出现的次数 子集数量
for(int j = 0; j < I.size(); j++)
{
if(match(R[i], I[j]))
{
cnt1 ++;
index.push_back(j);
value.push_back(I[j]);
}
}
if(cnt1 != 0)
{
cnt.push_back(cnt1);
index1.push_back(R[i]);
}
}
int sum = value.size() + cnt.size() + index.size() + index1.size();
cout << sum << " ";
int j = 0;
for(int i = 0; i < cnt.size(); i++)
{
cout << index1[i] << ' ' << cnt[i] << ' ' ;
while((cnt[i]--) > 0)
{
cout << index[j] << ' ' << value[j];
if(i == cnt.size() - 1 && cnt[i] == 0)
cout << endl;
else
cout << " ";
j ++;
}
}
}
return 0;
}
26、字符串排序
#include
#include
using namespace std;
int main()
{
string str;
while(getline(cin, str))
{
vector<char> alp;//字母顺序存储
for(int i = 0; i < 26; i++)
for(int j = 0; j < str.size(); j++)
{
if(str[j] - 'a' == i || str[j] - 'A' == i)
alp.push_back(str[j]);
}
for(int m = 0, n = 0; m < str.size() && n < alp.size(); m++)
{
if((str[m] >= 'a' && str[m] <='z') || (str[m] >= 'A' && str[m] <= 'Z'))
str[m] = alp[n++];
}
cout << str << endl;
}
return 0;
}
27、查找兄弟单词
#include
#include
#include
#include
using namespace std;
int main()
{
int n;//字典中单词个数
while(cin >> n)
{
vector<string> dic;//字典
string word;
//字典初始化
for(int i = 0; i < n; i ++)
{
cin >> word;
dic.push_back(word);
}
string target;//目标单词
cin >> target;
//处理 搜索兄弟单词
vector<string> res;//记录所有兄弟单词
string temp = target;
sort(temp.begin(), temp.end());//排序
int lent = target.size();//目标单词的长度
for(int i = 0; i < n; i ++)
{
int lendic = dic[i].size();
//长度一直 单词不同
if((lendic == lent) && target != dic[i])
{
string tempdic = dic[i];
sort(tempdic.begin(), tempdic.end());
if(temp == tempdic)
res.push_back(dic[i]);
}
}
//所有兄弟单词都在res中
int index;//res[index - 1]
cin >> index;
//坑 一定要字典排序
sort(res.begin(), res.end());
cout << res.size() << endl;
//默认了index - 1 < res.size()
if(index - 1 < res.size())
cout << res[index - 1] << endl;
}
return 0;
}
28、素数伴侣
#include
#include
#include
//匈牙利算法
using namespace std;
int t[100][100] = {0};
vector<int> ji(100, -1);
vector<int> ou(100, -1);
vector<int> visit(100, 0);
bool isprime(int n)
{
int flag = 0;
for(int i = 2; i <= n / i; i ++)
if(n % i == 0)
{
flag = 1;
break;
}
if(flag == 0) return true;
return false;
}
int dfs(int i)
{
//找到 返回1 没找到返回0
for(int j = 0; j < 100; j ++)
{
if(t[i][j] == 1 && !visit[j])
{
visit[j] = 1;// ij匹配
if(ou[j] == -1 || dfs(ou[j]))
{
ji[i] = j;
ou[j] = i;
return 1;
}
}
}
return 0;
}
int main()
{
int n;
//素数 偶数+奇数
//素数伴侣 一定是 偶数 + 奇数
vector<int> cou, cji;
while(cin >> n)
{
for(int i = 0; i < n; i ++)
{
int temp;
cin >> temp;
if(temp % 2 != 0) cji.push_back(temp);//奇数
else cou.push_back(temp);//偶数
}
for(int i = 0; i < cji.size(); i ++)
for(int j = 0; j < cou.size(); j ++)
if(isprime(cji[i] + cou[j])) t[i][j] = 1;
//匈牙利算法
int res = 0;
for(int i = 0; i < cji.size(); i ++)
{
if(ji[i] == -1)
{
for(int j = 0; j < visit.size(); j ++) visit[j] = 0;
res += dfs(i);//增光路径 每多找到一组 +1
}
}
cout << res << endl;
//清空
cji.clear();
cou.clear();
for(int i = 0; i < 100; i ++)
for(int j = 0; j < 100; j ++)
t[i][j] = 0;
for(int i = 0; i < 100; i ++)
ji[i] = ou[i] = -1;
}
return 0;
}
29、字符串加解密
#include
#include
using namespace std;
string data1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string data2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
int main()
{
string str1, str2;
while(cin >> str1 >> str2)
{
for(int i = 0; i < str1.size(); i ++)
str1[i] = data2[data1.find(str1[i])];
for(int i = 0; i < str2.size(); i ++)
str2[i] = data1[data2.find(str2[i])];
cout << str1 << endl << str2 << endl;
}
return 0;
}
30、字符串合并处理
#include
#include
#include
using namespace std;
const string helper1 = "0123456789abcdefABCDEF";
const string helper2 = "084C2A6E195D3B7F5D3B7F";
int main()
{
string str1, str2, mergestr;
while(cin >> str1 >> str2)
{
mergestr = str1 + str2;
//奇数 偶数分别拆开
string strji, strou;
for(int i = 0; i < mergestr.size(); i++)
{
if( i % 2 == 0)
strou += mergestr[i];
else
strji += mergestr[i];
}
//排序后合并
sort(strji.begin(), strji.end());
sort(strou.begin(), strou.end());
int index = 0;
for(int i = 0; i < mergestr.size(); i++)
{
if(i % 2 == 0)
mergestr[i] = strou[i / 2];
else
mergestr[i] = strji[i / 2];
}
//加密
for(int i = 0; i < mergestr.size(); i++)
{
int idx = helper1.find(mergestr[i]);
if(idx != -1)
mergestr[i] = helper2[idx];
}
cout << mergestr << endl;
}
return 0;
}