首页 > 试题广场 >

简单密码

[编程题]简单密码
  • 热度指数:282765 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现在有一种密码变换算法。
九键手机键盘上的数字与字母的对应: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,把密码中出现的小写字母都变成九键键盘对应的数字,如:a 变成 2,x 变成 9.
而密码中出现的大写字母则变成小写之后往后移一位,如:X ,先变成小写,再往后移一位,变成了 y ,例外:Z 往后移是 a 。
数字和其它的符号都不做变换。
数据范围: 输入的字符串长度满足

输入描述:

输入一组密码,长度不超过100个字符。



输出描述:

输出密码变换后的字符串

示例1

输入

YUANzhi1987

输出

zvbo9441987
#include <iostream> 
#include <string>
using namespace std;
int main(void)
{
	string pw;
	while (cin >> pw){
		int length = pw.size();
		string newPw(pw);
		for (int i = 0; i<length; ++i){
			if (pw[i] >= '0' && pw[i] <= '9')
				newPw[i] = pw[i];
			else if (pw[i] >= 'A' && pw[i] <= 'Z'){
				if (pw[i] == 'Z')
					newPw[i] = 'a';
				else
					newPw[i] = 'a' + pw[i] - 'A' + 1;
			}
			else if (pw[i] >= 'a' && pw[i] <= 'z'){
				if (pw[i] == 'z')
					newPw[i] = '0' + 9;
				else if (pw[i] >= 'p' && pw[i] <= 's')
					newPw[i] = '0' + 7;
				else if (pw[i] >= 't' && pw[i] <= 'v')
					newPw[i] = '0' + 8;
				else if (pw[i] >= 'w' && pw[i] <= 'z')
					newPw[i] = '0' + 9;
				else {
					int num = (pw[i] - 'a') / 3 + 2;
					newPw[i] = '0' + num;
				}
			}
		}
		cout << newPw << endl;
	}

	return 0;
}

发表于 2016-06-23 08:18:45 回复(4)
#include <bits/stdc++.h>
using namespace std;
unordered_map<char, char> helper = {{'a',2},{'b',2},{'c',2},{'d',3},{'e',3},{'f',3},{'g',4},{'h',4},{'i',4},{'j',5},{'k',5},{'l',5},{'m',6},{'n',6},{'o',6},{'p',7},{'q',7},{'r',7},{'s',7},{'t',8},{'u',8},{'v',8},{'w',9},{'x',9},{'y',9},{'z',9}};
char toLower(char ch){
    char result = tolower(ch);
    if(result=='z') result = 'a';
    else result = result + 1;
    return result;
}
int main(){
    string str;
    while(cin >> str){
        for(int i=0;i < str.size();++i)
            if(isalpha(str[i]))
                if(isupper(str[i])) str[i] = toLower(str[i]);
                else str[i] = helper[str[i]] + '0';
        cout << str << endl;
    }
    return 0;
}

发表于 2020-06-23 13:34:59 回复(0)
#include<iostream>
#include<string>
using namespace std;
const string dict1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string dict2="bcdefghijklmnopqrstuvwxyza22233344455566677778889999";

char Char_Change(char a){
    for(int i=0;i<dict1.size();i++)
        if(dict1[i]==a) return dict2[i];
    return a;
}

int main(){
    //string data="YUANzhi1987";
    string data;
    while(getline(cin,data)){
    	for(int i=0;i<data.size();i++)
        	data[i] = Char_Change(data[i]);
    	cout<<data<<endl;
    }
    return 0;
}


发表于 2016-08-01 21:23:25 回复(84)
#include<iostream>
#include<string>
using namespace std;

//小写字母变换为九键键盘数字
char charToInt(const char& ch){
    if('a'<=ch&&ch<='c') return '2';
    else if('d'<=ch&&ch<='f') return '3';
    else if('g'<=ch&&ch<='i') return '4';
    else if('j'<=ch&&ch<='l') return '5';
    else if('m'<=ch&&ch<='o') return '6';
    else if('p'<=ch&&ch<='s') return '7';
    else if('t'<=ch&&ch<='v') return '8';
    else  return '9';
}
//大写字母变为小写字母后往后移位
char charMove(const char& ch){
    if(ch=='Z') return 'a';
    else return tolower(ch)+1;
}
int main(){
    string str;
    while(getline(cin,str)){
        string outStr;
        for(auto ch:str){
            if('a'<=ch&&ch<='z') outStr.push_back(charToInt(ch));
            else if('A'<=ch&&ch<='Z') outStr.push_back(charMove(ch));
            else outStr.push_back(ch);
        }
        cout<<outStr<<endl;
    }
    
}

发表于 2022-08-04 10:26:25 回复(0)
string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
string2 = "bcdefghijklmnopqrstuvwxyza222333444555666777788899990123456789"
s = input()
for i in s:
    print(string2[string.index(i)],end='')
发表于 2022-07-24 21:46:33 回复(0)
def str_tran(get_str):
    lst_str = []
    for i in get_str:
        lst_str.append(i)
    for j in range(len(lst_str)):
        if ord(lst_str[j]) == ord('Z'):
            lst_str[j] = 'a'
        elif ord(lst_str[j]) >= 65 and ord(lst_str[j]) < 90:
            lst_str[j] = chr(ord(lst_str[j])+32+1)
        elif lst_str[j] in 'abc':
            lst_str[j] = '2'
        elif lst_str[j] in 'def':
            lst_str[j] = '3'
        elif lst_str[j] in 'ghi':
            lst_str[j] = '4'
        elif lst_str[j] in 'jkl':
            lst_str[j] = '5'
        elif lst_str[j] in 'mno':
            lst_str[j] = '6'
        elif lst_str[j] in 'pqrs':
            lst_str[j] = '7'
        elif lst_str[j] in 'tuv':
            lst_str[j] = '8'
        elif lst_str[j] in 'wxyz':
            lst_str[j] = '9'
    return ''.join(lst_str)
get_str = input()
print(str_tran(get_str))

发表于 2022-07-20 22:42:39 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String[] str =  sc.nextLine().split("");
        String result = "";
        for(int i=0;i<str.length;i++){
            if("abc".contains(str[i])){
                result = result+"2";
            }else if("def".contains(str[i])){
                result = result+"3";
            }else if("ghi".contains(str[i])){
                result = result+"4";
            }else if("jkl".contains(str[i])){
                result = result+"5";
            }else if("mno".contains(str[i])){
                result = result+"6";
            }else if("pqrs".contains(str[i])){
                result = result+"7";
            }else if("tuv".contains(str[i])){
                result = result+"8";
            }else if("wxyz".contains(str[i])){
                result = result+"9";
            }else{
                if("0123456789".contains(str[i])){
                    result = result+str[i];
                }else{
                  char[] num =   str[i].toCharArray();
                if(num[0]==90){
                     char ch = (char) (97);
                    result = result+ch;
                }else{
                    char ch = (char) (num[0] + 33);
                    result = result+ch;
                }
              }
            }
        }
        System.out.println(result);
    }
}
发表于 2022-05-24 15:04:14 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char c[] = str.toCharArray();
        for(int i = 0; i<str.length() ; i++){
            if(c[i]>='A'&&c[i]<='Z'){
                if(c[i]+32 != 'z')
                    c[i] += 33;
                else
                    c[i] = 'a';
            }
            else if(c[i]=='a'||c[i]=='b'||c[i]=='c')
                c[i]='2';
            else if(c[i]=='d'||c[i]=='e'||c[i]=='f')
                c[i]='3';
            else if(c[i]=='g'||c[i]=='h'||c[i]=='i')
                c[i]='4';
            else if(c[i]=='j'||c[i]=='k'||c[i]=='l')
                c[i]='5';
            else if(c[i]=='m'||c[i]=='n'||c[i]=='o')
                c[i]='6';
            else if(c[i]=='p'||c[i]=='q'||c[i]=='r'||c[i]=='s')
                c[i]='7';
            else if(c[i]=='t'||c[i]=='u'||c[i]=='v')
                c[i]='8';
            else if(c[i]=='w'||c[i]=='x'||c[i]=='y'||c[i]=='z')
                c[i]='9';
            System.out.print(c[i]);
        }
        System.out.println();
    }
}
发表于 2022-03-22 15:15:21 回复(0)
str1 = input()
list1 = ['abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']
for i in str1:
    if i.islower():
        for a in list1:
            if i in a:
                n=list1.index(a)+2
                print(n,end='')
    elif i.isupper():
        print(chr(ord(i.lower())+1),end='')
    else:
        print(i,end='')
发表于 2022-01-19 23:22:58 回复(0)
俺太笨了,只会枚举:
#include<stdio.h>
int main(){
    char c;
    while(~scanf("%c",&c)){
        if(c=='Z')
            printf("a");
        else if('A'<=c&&c<'Z')
            printf("%c",c+33);
        else if('a'<=c&&c<='z')
        {
            if('a'<=c&&c<='c') printf("2");
            if('d'<=c&&c<='f') printf("3");
            if('g'<=c&&c<='i') printf("4");
            if('j'<=c&&c<='l') printf("5");
            if('m'<=c&&c<='o') printf("6");
            if('p'<=c&&c<='s') printf("7");
            if('t'<=c&&c<='v') printf("8");
            if('w'<=c&&c<='z') printf("9");
        }
        else printf("%c",c);
    } printf("\n");
}
发表于 2021-12-28 22:05:35 回复(0)
number = {"2":"abc", "3":"def", "4":"ghi", "5":"jkl", "6":"mno", "7":"pqrs", "8":"tuv", "9":"wxyz"}
pw = list(input())
for i in range(len(pw)):
  if pw[i].islower():
    for key, value in number.items():
      if pw[i] in value:
        pw[i] = key
        break
  elif pw[i].isupper():
    if pw[i] == "Z":
      pw[i] = "a"
    else:
      pw[i] = chr(ord(pw[i].lower()) + 1)
print(*pw, sep="")

发表于 2021-11-04 03:42:40 回复(0)
a="abcdefghijklmnopqrstuvwxyza"
w=input().strip()
p=""
for i in w:
    if i.islower():
        if i in "abc":
            p+="2"
        elif i in "def":
            p+="3"
        elif i in "ghi":
            p+="4"
        elif i in "jkl":
            p+="5"
        elif i in "mno":
            p+="6"
        elif i in "pqrs":
            p+="7"
        elif i in "tuv":
            p+="8"
        elif i in "wxyz":
            p+="9"
        else:
            pass
    elif i.isupper():
        inx=a.index(i.lower(),0,26)
        p+=a[inx+1]
    else:
        p+=i
print(p)

发表于 2021-09-16 02:27:22 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s = sc.nextLine();
            StringBuilder sb = new StringBuilder();
            for(int i=0; i<s.length(); i++){
                sb.append(get(s.charAt(i)));
            }
            System.out.println(sb.toString());
        }
    }
    
    private static char get(char c){
        if(c>='A' && c<='Z'){//大写字母,变小写后移一位
            if(c=='Z') return 'a';
            return (char)(c+1+('a'-'A'));
        }else if(c>='a' && c<='z'){//小写字母
//             switch(c){
//                 case 'a':case'b':case'c': return '2';
//                 case 'd':case'e':case'f': return '3';
//                 case 'g':case'h':case'i': return '4';
//                 case 'j':case'k':case'l': return '5';
//                 case 'm':case'n':case'o': return '6';
//                 case 'p':case'q':case'r':case's': return '7';
//                 case 't':case'u':case'v': return '8';
//                 case 'w':case'x':case'y':case 'z': return '9';
//                 default: return ' ';
//             }
            String s = String.valueOf(c);
            if("abc".contains(s)) return '2';
            if("def".contains(s)) return '3';
            if("ghi".contains(s)) return '4';
            if("jkl".contains(s)) return '5';
            if("mno".contains(s)) return '6';
            if("pqrs".contains(s)) return '7';
            if("tuv".contains(s)) return '8';
            if("wxyz".contains(s)) return '9';
            return ' ';
        }else{//其他
            return c;
        }
    }
}

发表于 2021-08-03 15:08:08 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Decrypt {
    public static final Map<Integer, String> MAP = new HashMap<>();

    static {
        MAP.put(2, "abc");
        MAP.put(3, "def");
        MAP.put(4, "ghi");
        MAP.put(5, "jkl");
        MAP.put(6, "mno");
        MAP.put(7, "pqrs");
        MAP.put(8, "tuv");
        MAP.put(9, "wxyz");
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] pwds = sc.next().toCharArray();
        String decyptedPwd = "";
        int step = 32;
        for (Character pwd : pwds) {
            //大写字母范围
            if (pwd >= 65 && pwd <= 90) {
                if (pwd + step == 122) {
                    decyptedPwd += "a";
                } else {
                    decyptedPwd += (char) ((pwd + step) + 1);
                }
            }else {
                for (Map.Entry<Integer, String> map : MAP.entrySet()) {
                    if (map.getValue().contains(String.valueOf(pwd))) {
                        decyptedPwd += map.getKey();
                    }
                }
                if (!(pwd >= 97 && pwd <= 122)){
                    decyptedPwd+=pwd;
                }
            }
        }
        System.out.println(decyptedPwd);
    }
}
发表于 2021-07-15 17:17:20 回复(0)
初始化一个九键映射表,然后依照题意按条件进行转化即可
mp = dict()
mp['0'] = '0'
mp['1'] = '1'
mp['a'] = '2'
mp['b'] = '2'
mp['c'] = '2'
mp['d'] = '3'
mp['e'] = '3'
mp['f'] = '3'
mp['g'] = '4'
mp['h'] = '4'
mp['i'] = '4'
mp['j'] = '5'
mp['k'] = '5'
mp['l'] = '5'
mp['m'] = '6'
mp['n'] = '6'
mp['o'] = '6'
mp['p'] = '7'
mp['q'] = '7'
mp['r'] = '7'
mp['s'] = '7'
mp['t'] = '8'
mp['u'] = '8'
mp['v'] = '8'
mp['w'] = '9'
mp['x'] = '9'
mp['y'] = '9'
mp['z'] = '9'

while True:
    try:
        trans = input().strip()
        res = []
        for i in range(len(trans)):
            s = trans[i]
            ascII = ord(s)
            if ascII >= 65 and ascII <= 90:
                if s == 'Z':
                    res.append('a')
                else:
                    res.append(chr(ascII + 33))
            elif ascII >= 97 and ascII <= 122:
                res.append(mp[s])
            else:
                res.append(s)
        print(''.join(res))
    except:
        break


发表于 2021-03-20 16:34:46 回复(0)
//穷举法 时间复杂度 0(n)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    //大写变成n+1的小写, 小写变为数字,数字还是数字
    public static String value = "bcdefghijklmnopqrstuvwxyza222333444555666777788899990123456789";
    public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    public static void main(String[] args) {
//          System.out.println(key.length()+"-"+value.length());
        Map<String,String> map = new HashMap();
          for(int i = 0; i<key.length(); i++){
             map.put(key.charAt(i)+"",value.charAt(i)+""); 
            }
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        StringBuffer sb = new StringBuffer();
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.next();
            for(int i = 0; i<a.length(); i++){
               sb.append(map.get(a.charAt(i)+""));
            }
            System.out.println(sb);
        }
    }
}

编辑于 2021-02-28 12:41:56 回复(0)
最笨,最不优美的方法
#include<iostream>
#include <string>
using namespace std;

int main() {
    string str;
    while(cin >> str){
        int len = str.size();
        for(int i = 0; i<len ;i++){
            if(str[i]>='A' && str[i]<'Z'){
                str[i] = str[i] + 33;
                continue; // continue必须加,否则后面继续转码
            }
            if(str[i] == 'Z'){
                str[i] = 'a';
                continue;
            }
            
            if(str[i]>='a' && str[i]<='c')
                str[i] = '2';
            if(str[i]>='d' && str[i]<='f')
                str[i] = '3';
            if(str[i]>='g' && str[i]<='i')
                str[i] = '4';
            if(str[i]>='j' && str[i]<='l')
                str[i] = '5';
            if(str[i]>='m' && str[i]<='o')
                str[i] = '6';
            if(str[i]>='p' && str[i]<='s')
                str[i] = '7';
            if(str[i]>='t' && str[i]<='v')
                str[i] = '8';
            if(str[i]>='w' && str[i]<='z')
                str[i] = '9';
        }
        cout << str<< endl;
    }
    return 0;
}



发表于 2021-01-22 21:21:35 回复(0)
import sys


a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
b = "bcdefghijklmnopqrstuvwxyza222333444555666777788899990123456789"
output = ""

for s in sys.stdin:
    s = s.strip()
    for c in s:
        output += b[a.index(c)]
        
print(output)

发表于 2020-12-14 20:17:21 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String string = sc.nextLine();
            
            char[] chs = string.toCharArray();
            String res = "";
            for (int i = 0; i < chs.length ; i++) {
                if(chs[i]>='a' && chs[i]<='z'){
                    res+=lowCase2digital(chs[i]);//小写变数字
                }else if(chs[i]>='A' && chs[i]<='Z'){
                    res += UpCase2lowCase(chs[i]);
                }else{
                    res += chs[i];
                }
            }
            System.out.println(res);
        }
    }
    public static char UpCase2lowCase(char c){
        char tmp = (char) (c ^ ' ');
        return (char)('a'+(tmp - 'a' + 1)%26);
        
    }
    public static char lowCase2digital(char c){
        if(c>='a' && c<= 'c'){
            return '2';
        }else if(c>='d' && c<= 'f'){
            return '3';
        }else if(c>='g' && c<= 'i'){
            return '4';
        }else if(c>='j' && c<= 'l'){
            return '5';
        }else if(c>='m' && c<= 'o'){
            return '6';
        }else if(c>='p' && c<= 's'){
            return '7';
        }else if(c>='t' && c<= 'v'){
            return '8';
        }else{
            return '9';
        }
    }
}

发表于 2020-08-22 14:45:03 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    string a;
    string b[8]={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    while(cin>>a){
        for(int i=0;i<a.size();i++){
            if(a[i]>='A'&&a[i]<='Z')
                if(a[i]=='Z')
                    a[i]='a';
                else
                    a[i]=tolower(a[i])+1;
            else if(a[i]>='a'&&a[i]<='z'){
                for(int j=0;j<8;j++){
                    if(b[j].find(a[i])!=b[j].npos)
                        a[i]=j+'2';
                }
            }
        }
        cout<<a<<endl;
    }
    return 0;
}

发表于 2020-08-16 16:49:30 回复(0)