文章

166

粉丝

68

获赞

855

访问

61.7k

头像
字符串的操作 题解:中文冒号,两个空格
P1699 华中科技大学机试题
发布于2025年3月14日 10:26
阅读数 163

#include <bits/stdc++.h>
using namespace std;

char kaiSaChange(char c){
    int k;
    char ans;
    if(c>='a'&&c<='z'){
        k=(c-'a'+2)%26;
        ans='a'+k;
    }else if(c>='A'&&c<='Z'){
        k=(c-'A'+2)%26;
        ans='A'+k;
    }
    return ans;
}

int main() {
	string s;
	while(cin>>s){
	    cout<<"原文:"<<s<<endl;
	    for(int i=0;i<s.size();i++){
	        s[i]=kaiSaChange(s[i]);
	    }
	    cout<<"密文:"<<s<<endl;
	    for(auto x:s){
	        cout<<x<<"  ";
	        int k=x;
	        string t=bitset<8>(k).to_string();
	        int ct=0;
	        for(auto y:t){
	            if(y=='1')ct++;
	        }
	        if(ct%2==0)t[0]='1';
	        cout<<t<<"  ";
	        int sum=0;
	        for(auto y:t){
	            sum*=2;
	            sum+=y-'0';
	        }
	        cout<<sum<<endl;
	    }
	}

}

其他就是用bitset处...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发