南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 -ID and password

这里

题目描述

Users prefer simple passwords that are easy to remember, but such passwords are often insecure. Some sites use random computer-generated passwords, but users have a hard time remembering them. But today I improved the website system, which greatly reduced the probability of password being cracked. So we can generate a simple password for each ID by default.
Each id has its initial password.
Do the following two jobs at the same time:
1.Turn uppercase letters to lowercase letters
2.Turn lowercase letters to uppercase letters
You can get the initial password for the id.

输入描述:

The input consists of one or more identities(ID), one per line.
The length of each id is between 1 and 100. Each id only consists of letters and digits.

输出描述:

For each id, output the corresponding initial password.

输入

JDJhadjsazA
ksfjkkdSDJ23

输出

jdjHADJSAZa
KSFJKKDsdj23

思路

题目很简单,就是字符串大写变小写,小写变大写。
但是看到一个很有意思的代码

mycode

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

int main() {
//  freopen("in.txt", "r", stdin);
    string s;
    while (cin >> s) {
        string t;
        int len = s.length();
        for (int i = 0;i < len; i++) {
            if (isdigit(s[i]))  t += s[i];
            else {
                if (s[i] >= 'A' && s[i] <= 'Z')   t += s[i] + 32;
                if (s[i] >= 'a' && s[i] <= 'z')   t += s[i] - 32;

            }
        }
        cout << t << endl;
    }
    return 0;
}

othercode

int (‘a’) = 97 1100001
int (‘A’) = 65 1000001
‘a’ - ‘z’对32异或,刚好等于 ‘A’ - ‘Z’
‘A’ - ‘Z’对32异或,结果等于‘a’ - ‘z’
可以手动模拟二进制

#include<bits/stdc++.h> 
#define N 100005
#define ll long long
using namespace std;
int main() {
    string s;
    while (cin >> s) {
        int len = s.length();
        for (int i = 0; i < len; i++) {
            if (s[i] >= 'A' && s[i] <= 'z') {
                s[i] = s[i] ^ 32;
            }
        }
        cout << s << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henuyh/article/details/80385946
今日推荐