31、单词倒排
#include
#include
using namespace std;
int main()
{
string str;
while(getline(cin, str))
{
vector<string> word;
string temp = "";
for(int i = 0; i < str.size(); i ++)
{
if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
temp += str[i];
else
{
if(temp.size() > 0)
{
word.push_back(temp);
temp = "";
}
}
}
//最后一个单词没有读进 vector
if(temp.size() > 0)
word.push_back(temp);
//倒序输出word
for(int i = word.size() - 1; i > 0; i --)
cout << word[i] << ' ';
cout << word[0] << endl;
}
return 0;
}
32、字符串运用-密码截取
//回文串
#include
#include
using namespace std;
int main()
{
string s;
while(cin >> s)
{
int maxlen = 0;
//ABA 3 #A#B#A# 7 7/2=3
//ABBA 4 #A#B#B#A# 9 9/2=4
//扩充字符串
string temp = "";
temp.append(1, '#');
for(int i = 0; i < s.size(); i ++)
{
temp.append(1, s[i]);
temp.append(1, '#');
}
s = temp;
int n = s.size();
for(int i = 1; i < n; i ++)
{
//奇数 ABA
int low = i - 1, high = i + 1;
while(low >= 0 && high < n && s[low] == s[high])
low --, high ++;
if(high - low - 1 > maxlen)
maxlen = high - low - 1;
}
cout << maxlen / 2 << endl;
}
return 0;
}
33、整数与IP地址间的转换
#include
#include
using namespace std;
int main()
{
unsigned int a, b, c, d, shi;//10.0.3.193
char dot;
while(cin >> a >> dot >> b >> dot >> c >> dot >> d)
{
cout << (a << 24) + (b << 16) + (c << 8) + d << endl;
//输入十进制
cin >> shi;
cout << ((shi & 0xff000000) >> 24) << '.' << ((shi & 0x00ff0000) >> 16) << '.' << ((shi & 0x0000ff00) >> 8) << '.' << (shi & 0x000000ff) << endl;
}
return 0;
}
34、图片整理
#include
#include
using namespace std;
int main()
{
string str;
while(cin >> str)
{
sort(str.begin(), str.end());
cout << str << endl;
}
return 0;
}
35、蛇形矩阵
#include
using namespace std;
int main()
{
int n;
while(cin >> n)
{
int first = 1;
for(int i = 1; i <= n; i++)
{
cout << first;
int temp = first;
for(int j = i + 1; j <= n; j++)
{
temp += j;
cout << " " << temp;
}
cout << endl;
first += i;
}
}
return 0;
}
36、字符串加密
#include
using namespace std;
int main()
{
string key, s;
while(cin >> key >> s)
{
//创建加密表
string alp;
//hash表
int p[26] = {0};//对应26个字母
for(int i = 0; i < key.size(); i ++)
{
//考虑小写字母
if(key[i] >= 'a' && key[i] <= 'z')
{
if(p[key[i] - 'a'] == 0)
{
p[key[i] - 'a'] = 1;
alp += key[i] - 32;//统一大写
}
}
//考虑大写
if(key[i] >= 'A' && key[i] <= 'Z')
{
if(p[key[i] - 'A'] == 0)
{
p[key[i] - 'A'] = 1;
alp += key[i];
}
}
}
//填充字母
for(int i = 0; i < 26; i ++)
{
if(p[i] == 0)
alp += 'A' + i;
}
//查表输出
for(int i = 0; i < s.size(); i ++)
{
if(s[i] >= 'a' && s[i] <= 'z')
{
char c = alp[s[i] - 'a'] + 32;
cout << c;
}
if(s[i] >= 'A' && s[i] <= 'Z')
{
cout << alp[s[i] - 'A'];
}
}
cout << endl;
}
return 0;
}
37、统计每个月兔子的总数
#include
using namespace std;
int main()
{
int month;
while(cin >> month)
{
int first = 1, second = 0, third = 0;//初试第一二三月兔子总数
//first 年龄1个月的兔子数量
//second 年龄2个月的兔子数量
//third 年龄3个月的兔子数量
while(-- month)
{
third += second;
second = first;
first = third;
}
cout << first + second + third << endl;
}
return 0;
}
38、求小球落地五次后所经历的路程
#include
using namespace std;
int main()
{
int h;
while(cin >> h)
{
double sum = h;
double height = h;
for(int i = 1; i < 5; i ++)
{
height /= 2;
sum += height * 2;
}
cout << sum << endl << height / 2 << endl;
}
return 0;
}
39、判断两个IP是否属于一个子网
#include
using namespace std;
int main()
{
int mask1 = 0, mask2 = 0, mask3 = 0, mask4 = 0;//255.255.255.0
int ip1a = 0, ip1b = 0, ip1c = 0, ip1d = 0;
int ip2a = 0, ip2b = 0, ip2c = 0, ip2d = 0;
char ch;
while(cin>>mask1>>ch>>mask2>>ch>>mask3>>ch>>mask4)
{
cin>>ip1a>>ch>>ip1b>>ch>>ip1c>>ch>>ip1d;
cin>>ip2a>>ch>>ip2b>>ch>>ip2c>>ch>>ip2d;
if((mask1 > 255 || mask1 < 0) || (mask2 > 255 || mask2 < 0) || (mask3 > 255 || mask3 < 0) || (mask4 > 255 || mask4 < 0)
|| (ip1a > 255 || ip1a < 0) || (ip1b > 255 || ip1b < 0) || (ip1c > 255 || ip1c < 0) || (ip1d > 255 || ip1d < 0)
|| (ip2a > 255 || ip2a < 0) || (ip2b > 255 || ip2b < 0) || (ip2c > 255 || ip2c < 0) || (ip2d > 255 || ip2d < 0))
cout<<1<<endl;
else if(((mask1 & ip1a) == (mask1 & ip2a)) && ((mask2 & ip1b) == (mask2 & ip2b)) && ((mask3 & ip1c) == (mask3 & ip2c)) && ((mask4 & ip1d) == (mask4 & ip2d)))
cout<<0<<endl;
else
cout<<2<<endl;
}
return 0;
}
40、输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数
#include
using namespace std;
int main()
{
string str;
while(getline(cin, str))
{
int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
for(int i = 0; i < str.size(); i ++)
{
if((str[i] <= 'z' && str[i] >= 'a') || (str[i] <= 'Z' && str[i] >= 'A'))
cnt1 ++;
else if(str[i] == ' ')
cnt2 ++;
else if(str[i] <= '9' && str[i] >= '0')
cnt3 ++;
else
cnt4 ++;
}
cout << cnt1 << endl << cnt2 << endl << cnt3 << endl << cnt4 << endl;
}
return 0;
}