文章

40

粉丝

607

获赞

68

访问

400.7k

头像
1699字符串操作(bit版,复习bitset库函数)
P1699 华中科技大学2019年机试题
发布于2020年4月25日 18:23
阅读数 9.5k

#include<iostream>
#include<string>
#include<cstring>
#include<bitset>

using namespace std;

string miwen(string s) {
	for (int i = 0;i < s.size();i++) {
		if (s[i] >= 'a' && s[i] <= 'z')
		{
			s[i] = (s[i] - 97 + 2) % 26 + 97;
		}
		else if (s[i] >= 'A' && s[i] <= 'Z') {
			s[i] = (s[i] - 65 + 2) % 26 + 65;
		}
	}
	return s;
}

void f(string s) {
	for (int i = 0;i < s.size();i++) {
		bitset<8> bit = s[i];
		if (bit.count() % 2 == 0) {
			bit.set(7);
		}
		cout << s[i] << "  " << bit.to_string() << "  " << bit.to_ulong() << endl;
	}
}

int main() {
	string s;
	while (cin >> s) {
		cout << "原文:" << s << endl;
		s = miwen(s);
		cout << "密文:" << s << endl;
		f(s);
	}
	return 0;
}

点击查看部分bitset库函数具体用法

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发