首页 > 试题广场 > 简单密码
[编程题]简单密码
  • 热度指数:125985 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

密码是我们生活中非常重要的东东,我们的那么一点不能说的秘密就全靠它了。哇哈哈. 接下来渊子要在密码之上再加一套密码,虽然简单但也安全。

 

假设渊子原来一个BBS上的密码为zvbo9441987,为了方便记忆,他通过一种算法把这个密码变换成YUANzhi1987,这个密码是他的名字和出生年份,怎么忘都忘不了,而且可以明目张胆地放在显眼的地方而不被别人知道真正的密码。

 

他是这么变换的,大家都知道手机上的字母: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,就这么简单,渊子把密码中出现的小写字母都变成对应的数字,数字和其他的符号都不做变换,

 

声明:密码中没有空格,而密码中出现的大写字母则变成小写之后往后移一位,如:X,先变成小写,再往后移一位,不就是y了嘛,简单吧。记住,z往后移是a哦。



输入描述:

输入包括多个测试数据。输入是一个明文,密码长度不超过100个字符,输入直到文件结尾



输出描述:

输出渊子真正的密文

示例1

输入

YUANzhi1987

输出

zvbo9441987
#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 回复(76)

python solution:

d={
    "abc":2,
    "def":3,
    "ghi":4,
    "jkl":5,
    "mno":6,
    "pqrs":7,
    "tuv":8,
    "wxyz":9,

}
while True:
    try:
        a,res=input(),""
        for i in a:
            if i.isupper():
                if i!="Z":
                    res+=chr(ord(i.lower())+1)
                else:
                    res+="a"
            elif i.islower():
                for j in d.keys():
                    if i in j:
                        res+=str(d[j])
                        break
            else:
                res+=i
        print(res)



    except:
        break
发表于 2017-10-04 19:58:26 回复(18)
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(password(sc.next()));
	}
	
	public static String password(String str) {
		if (str == null || "".equals(str))
			return str;
		char[] nums = str.toCharArray();
		StringBuilder sb = new StringBuilder();
		for (char c : nums) {
			if (c >= 'a' && c <= 'z') {
				if (c == 's' || c == 'v'|| c == 'y' || c == 'z')
                    sb.append((c - 'a') / 3 + 1);
				else 
					sb.append((c - 'a') / 3 + 2);
			} else if (c >= 'A' && c <= 'Z') {
				if (c == 'Z')
					sb.append('a');
				else
					sb.append((char)(c + 'a' - 'A' + 1));
			} else {
                sb.append(c);
            }
		}
		return sb.toString();
	}
	
}
编辑于 2016-08-04 17:30:47 回复(5)
#include<iostream>
#include<string>
using namespace std;
const string from = "abcdefghijklmnopqrstuvwxyz";
const string to = "22233344455566677778889999";
int main() {
	string str;
	while (getline(cin, str)) {
		for (int i = 0; i < str.size(); i++) {
			if (isdigit(str[i])) continue;
			else if (isupper(str[i])) {
				if (str[i] == 'Z') str[i] = 'a';
				else str[i] = tolower(str[i]) + 1;
			}
			else
				str[i] = to[from.find(str[i])];
		}
		cout << str << endl;
	}
	return 0;
}

发表于 2017-03-15 14:54:22 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[]args){
      Scanner scan=new Scanner(System.in);
            String str=scan.next();
            char[]s=str.toCharArray();
            for(int i=0;i<s.length;i++){
                if(str.charAt(i)>='a'&&str.charAt(i)<='c')s[i]='2';
                if(str.charAt(i)>='d'&&str.charAt(i)<='f')s[i]='3';
                if(str.charAt(i)>='g'&&str.charAt(i)<='i')s[i]='4';
                if(str.charAt(i)>='j'&&str.charAt(i)<='l')s[i]='5';
                if(str.charAt(i)>='m'&&str.charAt(i)<='o')s[i]='6';
                if(str.charAt(i)>='p'&&str.charAt(i)<='s')s[i]='7';
                if(str.charAt(i)>='t'&&str.charAt(i)<='v')s[i]='8';
                if(str.charAt(i)>='w'&&str.charAt(i)<='z')s[i]='9';
                if(str.charAt(i)>='A'&&str.charAt(i)<='Z')s[i]=(char)((s[i]+32+1-'a')%26+'a');//s[i]+=33;
               
            }
            System.out.println(String.valueOf(s));
    }
}

发表于 2016-04-20 20:59:10 回复(5)
#include<iostream>

using namespace std;
int table[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
int main(){
    string str;
    while(cin>>str){
        for(int i = 0; i < str.size(); ++i){
            if(str[i] >= 'A' && str[i] < 'Z')
                cout<<(char)(str[i]-'A'+'a'+1);
            else if(str[i] == 'Z')
                cout<<'a';
            else if(str[i] >= 'a' && str[i] <= 'z')
                cout<<table[str[i]-'a'];
            else cout<<str[i];
        }
        cout<<endl;
    } 
    return 0;
}

发表于 2017-06-18 14:21:49 回复(3)

突然发现Python自带的字符串函数,试了下,挺好用

inttab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
outtab = "bcdefghijklmnopqrstuvwxyza22233344455566677778889999"
while True:
    try:
        pwd = input()
        print(pwd.translate(pwd.maketrans(inttab, outtab)))
    except:
        break
发表于 2018-12-20 09:27:41 回复(5)
import java.util.Scanner;

public class Main {
	
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = 0;
        while (sc.hasNext()) {//注意while处理多个case
            String str = sc.nextLine();
            StringBuilder sb = new StringBuilder();
            for(i = 0; i < str.length(); i++){
                if(str.charAt(i) == 'Z'){
                    sb.append("a");
                    continue;
                }
                if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
                    sb.append(String.valueOf(str.charAt(i)));
                    continue;
                }
                if(str.charAt(i)>='A' && str.charAt(i) < 'Z'){
                    sb.append(String.valueOf((char)(str.charAt(i) + 1)).toLowerCase());
                    continue;
                }   
                else{
                    switch(str.charAt(i)){
                        case('1'):
                        sb.append("1");
                        break;
                        
                        case('a'):
                        case('b'):
                        case('c'):
                        sb.append("2");
                        break;
                        
                        case('d'):
                        case('e'):
                        case('f'):
                        sb.append("3");
                        break;
                        
                        case('g'):
                        case('h'):
                        case('i'):
                        sb.append("4");
                        break;
                        
                        case('j'):
                        case('k'):
                        case('l'):
                        sb.append("5");
                        break;
                        
                        case('m'):
                        case('n'):
                        case('o'):
                        sb.append("6");
                        break;
                        
                        case('p'):
                        case('q'):
                        case('r'):
                        case('s'):
                        sb.append("7");
                        break;
                        
                        case('t'):
                        case('u'):
                        case('v'):
                        sb.append("8");
                        break;
                        
                        case('w'):
                        case('x'):
                        case('y'):
                        case('z'):
                        sb.append("9");
                        break;
                    }
                }
            }
            System.out.println(sb.toString());
        }
    }
}
写的极其麻烦
>_<
发表于 2016-09-21 16:22:56 回复(4)
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str;
	cin >> str;

	int size = str.length();
	for (int i = 0; i < size; i++)
	{
        if (str[i] == 'Z')
		{
			str[i] = 'a'; continue;
		}
		if (str[i] < 'Z'&&str[i] >= 'A')
		{
			str[i] += '!'; continue;
		}
		else if (str[i] <= 'c'&&str[i] >= 'a')
		{
			str[i] = '2';  continue;
		}
		if (str[i] <= 'f'&&str[i] >= 'd')
		{
			str[i] = '3';  continue;
		}
		if (str[i] <= 'i'&&str[i] >= 'g')
		{
			str[i] = '4';  continue;
		}
		if (str[i] <= 'l'&&str[i] >= 'j')
		{
			str[i] = '5';  continue;
		}
		if (str[i] <= 'o'&&str[i] >= 'm')
		{
			str[i] = '6';  continue;
		}
		if (str[i] <= 's'&&str[i] >= 'p')
		{
			str[i] = '7';  continue;
		}
		if (str[i] <= 'v'&&str[i] >= 't')
		{
			str[i] = '8';  continue;
		}
		if (str[i] <= 'z'&&str[i] >= 'w')
		{
			str[i] = '9';
		}
	}
	for (int i = 0; i<size; i++)
	{
		cout << str[i];
	}
}

发表于 2019-09-15 18:57:27 回复(0)
#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)

纯C

开始的时候手欠把小写的”o"输成了数字“0”,看代码没看出来,对比输出结果才发现的。
#include <stdlib.h>
#include <stdio.h>

int main()
{
    char *str = (char *)malloc(sizeof(char) * 101);
    while(gets(str))
    {
        char *pstr = str;
        while(*pstr != '\0')
        {
            if(*pstr >= 'A' && *pstr <= 'Z')
            {
                if(*pstr == 'Z') *pstr = 'a';
                else *pstr += ('a' - 'A' + 1);
            }
            else if(*pstr >= 'a' && *pstr <= 'z')
            {
                if(*pstr <= 'c')
                    *pstr = '2';
                else if(*pstr <= 'f')
                    *pstr = '3';
                else if(*pstr <= 'i')
                    *pstr = '4';
                else if(*pstr <= 'l')
                    *pstr = '5';
                else if(*pstr <= 'o')
                    *pstr = '6';
                else if(*pstr <= 's')
                    *pstr = '7';
                else if(*pstr <= 'v')
                    *pstr = '8';
                else
                    *pstr = '9';
            }
            pstr++;
        }
        printf("%s", str);
    }
    free(str);
    return 0;
}


发表于 2020-08-24 15:11:07 回复(0)
#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)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
       
        while(in.hasNextLine()) {
            
            StringBuilder sBuilder = new StringBuilder();
            
            String password = in.nextLine();
            for (int i = 0; i < password.length(); i++) {
                if (password.charAt(i) >= 'A' && password.charAt(i) < 'Z') {
                    sBuilder.append(Character.toLowerCase((char)(password.charAt(i)+1)));
                } else if (password.charAt(i) == 'Z') {
                    sBuilder.append('a');
                } else if (password.charAt(i) >= 'a' && password.charAt(i) <= 'c') {
                    sBuilder.append(2);
                } else if (password.charAt(i) >= 'd' && password.charAt(i) <= 'f') {
                    sBuilder.append(3);
                } else if (password.charAt(i) >= 'g' && password.charAt(i) <= 'i') {
                    sBuilder.append(4);
                } else if (password.charAt(i) >= 'j' && password.charAt(i) <= 'l') {
                    sBuilder.append(5);
                } else if (password.charAt(i) >= 'm' && password.charAt(i) <= 'o') {
                    sBuilder.append(6);
                } else if (password.charAt(i) >= 'p' && password.charAt(i) <= 's') {
                    sBuilder.append(7);
                } else if (password.charAt(i) >= 't' && password.charAt(i) <= 'v') {
                    sBuilder.append(8);
                } else if (password.charAt(i) >= 'w' && password.charAt(i) <= 'z') {
                    sBuilder.append(9);
                } else {
                    sBuilder.append(password.charAt(i));
                }
            }
            
            System.out.println(sBuilder);
            
        }
    }
}


发表于 2016-08-24 15:41:33 回复(1)
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();
            char[] a = s.toCharArray();
            String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            String s2 = "bcdefghijklmnopqrstuvwxyza22233344455566677778889999";
            for(int i=0;i<a.length;i++) {
                if((a[i]<='Z'&&a[i]>='A') || (a[i]<='z'&&a[i]>='a')) {
                    a[i] = s2.charAt(s1.indexOf(a[i]));
                }
            }
            for(int i=0;i<a.length;i++) {
                System.out.print(a[i]);
            }
            System.out.println();
        }
    }
}

编辑于 2018-07-25 15:34:20 回复(3)
#需求:将接受进的字符串,一一遍历,如果是大写,先变小写,后延一位,如果是小写,变数字,其余不变
while True:
    try:
        new_str = ''
        password = input()
        for i in password:
            if i.isupper():
                if i is not 'Z':
                    new_str += chr(ord(i.lower())+1)
                else:
                     new_str += 'a'
            elif i.islower():
                if i in 'abc':
                     new_str += '2'
                elif i in 'def':
                     new_str += '3'
                elif i in 'ghi':
                     new_str += '4'
                elif i in 'jkl':
                     new_str += '5'
                elif i in 'mno':
                      new_str += '6'
                elif i in 'pgrs':
                      new_str += '7'
                elif i in 'tuv':
                      new_str += '8'
                elif i in 'wxyz':
                      new_str += '9'
            else:
                new_str += i
        print(new_str)
    except:
        break
因为题目中有‘输入包括多个测试数据’,所以还是用了while True
发表于 2020-03-22 20:02:30 回复(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)

python,结合讨论区核心观点,使用对照字符串,利用字符串索引,代码很简洁

inttab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
outtab = "bcdefghijklmnopqrstuvwxyza22233344455566677778889999"
while True:
    try:
        s = input()
        res = ''
        for i in s:
            if i.isalpha():
                res += outtab[inttab.find(i)]
            else:
                res += i 
        print(res)
    except:
        break


发表于 2020-10-12 20:21:48 回复(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)