⬆︎
×

[PAT-A] 1035 Password

Hyplus目录

Java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    static class Account {
        String id;
        String password;

        Account(String id, String password) {
            this.id = id;
            this.password = password;
        }
    }

    public static boolean modify(StringBuilder pswd) {
        boolean modified = false;
        for (int i = 0; i < pswd.length(); ++i) {
            char ch = pswd.charAt(i);
            if (ch == '1') {
                pswd.setCharAt(i, '@');
                modified = true;
            } else if (ch == '0') {
                pswd.setCharAt(i, '%');
                modified = true;
            } else if (ch == 'l') {
                pswd.setCharAt(i, 'L');
                modified = true;
            } else if (ch == 'O') {
                pswd.setCharAt(i, 'o');
                modified = true;
            }
        }
        return modified;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine(); // consume the newline character

        List<Account> accounts = new ArrayList<>();
        for (int i = 0; i < n; ++i) {
            String id = scanner.next();
            StringBuilder password = new StringBuilder(scanner.next());
            if (modify(password)) {
                accounts.add(new Account(id, password.toString()));
            }
        }
        scanner.close();

        if (accounts.isEmpty()) {
            if (n == 1) {
                System.out.println("There is 1 account and no account is modified");
            } else {
                System.out.printf("There are %d accounts and no account is modified\n", n);
            }
        } else {
            System.out.println(accounts.size());
            for (Account account : accounts) {
                System.out.println(account.id + " " + account.password);
            }
        }
    }
}

C++

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 10010, INF = 1e7 + 10;

int n;
vector <pair<string, string>> accounts;

bool modify(string &pswd) {
    string res;

    for (int i = 0; i < pswd.size(); ++i) {
        if (pswd[i] == '1') res += '@';
        else if (pswd[i] == '0') res += '%';
        else if (pswd[i] == 'l') res += 'L';
        else if (pswd[i] == 'O') res += 'o';
        else res += pswd[i];
    }

    if (pswd == res) return false;
    pswd = res;
    return true;
}

int main() {
    scanf("%d", &n);

    string id, pswd;
    for (int i = 0; i < n; ++i) {
        cin >> id >> pswd;
        if (modify(pswd)) accounts.push_back({id, pswd});
    }

    if (accounts.empty()) {
        if (n == 1) printf("There is 1 account and no account is modified\n");
        else printf("There are %d accounts and no account is modified\n", n);
    } else {
        printf("%d\n", accounts.size());
        for (auto it: accounts)
            cout << it.first << " " << it.second << endl;
    }
    return 0;
}

发表评论