首页 > 试题广场 >

WERTYU

[编程题]WERTYU
  • 热度指数:5138 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    A common typing error is to place the hands on the keyboard one row to the right of the correct position. So "Q" is typed as "W" and "J" is typed as "K" and so on. You are to decode a message typed in this manner.

输入描述:
    Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input.


输出描述:
    You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.
示例1

输入

O S, GOMR YPFSU/

输出

I AM FINE TODAY.
这题主要就是很麻烦,没别的,但看了大家用string自带的find函数可以减少很多坑,学习到了,一个一个找真的累死了QAQ
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
    string str;
    char table1[10]={'9','`','1','2','3','4','5','6','7','8'};
    char table2[26]={'a','V','X','S','W','D','F','G','U','H','J','K','N',
                     'B','I','O','q','E','A','R','Y','C','Q','Z','T','z'};
    while(getline(cin,str)){
        char ch[100];
        strcpy(ch,str.c_str());
        for(int i=0;i<strlen(ch);i++){
            if(ch[i]>='0'&&ch[i]<='9'){
                ch[i]=table1[ch[i]-'0'];
                continue;
            }
            if('A'<=ch[i]&&ch[i]<='Z'){
                ch[i]=table2[ch[i]-'A'];
                continue;
            }
            if(ch[i]=='='){
                ch[i]='-';
                continue;
            }
            if(ch[i]=='-') ch[i]='0';
            if(ch[i]=='.'){
                ch[i]=',';
                continue;
            }
            if(ch[i]=='/') ch[i]='.';
            if(ch[i]==',') ch[i]='M';
            if(ch[i]==';') ch[i]='L';
            if(ch[i]=='[') ch[i]='P';
            if(ch[i]==']') ch[i]='[';
            if(ch[i]=='\'') ch[i]=';';
        }
        cout<<ch<<endl;
    }
}

发表于 2019-02-09 23:14:39 回复(1)
//用map存一下很快搞定
#include<iostream>
#include<string>
#include<map>
using namespace std;

int main()
{
        string s;
        map<char,char> m;
        m[' ']=' ';
        s="`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
        for(int i=1;i<s.length();i++)
           m[s[i]]=s[i-1];
        while(getline(cin,s))
        {
            for(int i=0;i<s.length();i++)
                s[i]=m[s[i]];
            cout<<s<<endl;
        }
}
发表于 2018-08-23 13:28:44 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    //键盘字符
    string key = "1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
    string s;
    while(getline(cin,s))
    {
        string res;
        int position;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]==' ')
                res += s[i];
            else{
                position = key.find(s[i],0);
                res += key[position-1];
            }      
        }
        cout<<res<<endl;
    }
    return 0;
}

发表于 2019-04-13 20:32:41 回复(0)
//预处理思想很棒
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char code[46]={
'Q','W','E','R','T','Y','U','I','O','P','[',']','\\',
'A','S','D','F','G','H','J','K','L',';','\'',
'Z','X','C','V','B','N','M',',','.','/',
'1','2','3','4','5','6','7','8','9','0','-','='
};
int main(){
    char c;
    int i;
    while(c=getchar()){
        if(c=='\n') break;
        if(c==' '){
            printf(" ");
            continue;
        }
        for(i=0;i<46;i++){
            if(code[i]==c){
               putchar(code[i-1]);
               break;
            }
        }
    }
    return 0;
}

编辑于 2020-02-29 15:38:28 回复(0)

Object C

#include<stdio.h>
#include<string.h>

char str[10000+10], ht[300];
const char* keyboard[] = {"1234567890-=","QWERTYUIOP[]\\", "ASDFGHJKL;'", "ZXCVBNM,./"};
void init(){
    int i, len, j;
    for(i = 0; i<300; i++)
        ht[i] = i;
    for(i = 0; i<4; i++){
        len = strlen(keyboard[i]);
        for(j = 1; j<len; j++){
            ht[keyboard[i][j]] = keyboard[i][j-1];
        }
    } 
}
void solve(){
    int i, len = strlen(str);
    for(i = 0; i<len; i++){
        printf("%c", ht[str[i]]);
    }
    printf("\n");
}
int main(){
    init();
    //freopen("in.txt", "r", stdin);
    while(gets(str)){
        solve(); 
    }

    return 0;
}
发表于 2018-07-24 16:04:18 回复(0)
#include <array>
#include <iostream>
#include <map>
#include <string>
using namespace std;

array<string, 4>arr = {
    "1234567890-=",
    "QWERTYUIOP[]\\",
    "ASDFGHJKL;'",
    "ZXCVBNM,./"
};

map<char, char>myMap;

void Initial() {
    myMap[' '] = ' ';
    for (const auto& str : arr) {
        for (int i = 0; i < str.size() - 1; i++) {
            myMap[str[i + 1]] = str[i];
        }
    }
}

int main() {
    Initial();
    string str1, str2;
    while (getline(cin, str1)) {
        for (const auto& ch : str1) {
            str2 += myMap[ch];
        }
        cout << str2 << endl;
    }
    return 0;
}

编辑于 2024-03-02 15:06:01 回复(0)
题目好***
#include <bits/stdc++.h>
using namespace std;

int main() {
    char hx[1280];
    memset(hx, 0, sizeof hx);
    hx['1']='`'; hx['7'] = '6';
    hx['2']='1'; hx['8'] = '7';
    hx['3']='2'; hx['9'] = '8';
    hx['4']='3'; hx['0'] = '9';
    hx['5']='4'; hx['-'] = '0';
    hx['6']='5'; hx['='] = '-';
    hx['W'] = 'Q'; hx['w'] = 'q';
    hx['E'] = 'W', hx['R'] = 'E'; hx['T'] = 'R';
    hx['Y'] = 'T'; hx['U'] = 'Y'; hx['I'] = 'U'; hx['O'] = 'I';
    hx['P'] = 'O'; hx['['] = 'P'; hx[']'] = '['; hx['\\'] = ']';
    hx['S'] = 'A'; hx['D'] = 'S';hx['F']='D';hx['G']='F';hx['H']='G';
    hx['J']='H';hx['K']='J';hx['L']='K';hx[';']='L';hx['\'']=';';
    hx['X']='Z';hx['V']='C';hx['C']='X';hx['B']='V';hx['N']='B';
    hx['M']='N';hx[',']='M';hx['.']=',';hx['/']='.';
    hx[' '] = ' ';
    string s;
    getline(cin, s);
    for (int i = 0; i < s.size(); i++)
        s[i] = hx[s[i]];
    cout << s;
}

发表于 2023-03-20 15:17:41 回复(0)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static String s1 = "QWERTYUIOP[]";
    static String s2 = "ASDFGHJKL;'";
    static String s3 = "ZXCVBNM,./";
    static String s4 = "1234567890-=";

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            StringBuilder sb = new StringBuilder(str);
            for (int i = 0; i < str.length(); i++) {
                char c1 = str.charAt(i);//原来的符号
                if (s1.indexOf(c1) != -1) {
                    Replace(c1, sb, i, s1);
                } else if (s2.indexOf(c1) != -1) {
                    Replace(c1, sb, i, s2);
                } else if (s3.indexOf(c1) != -1) {
                    Replace(c1, sb, i, s3);
                } else if (s4.indexOf(c1) != -1) {
                    Replace(c1, sb, i, s4);
                }
            }
            System.out.println(sb);
        }
    }

    public static void Replace(char c1, StringBuilder sb, int index, String s) {
        int c2index = s.indexOf(c1) - 1;
        char c2 = s.charAt(c2index);
        sb.setCharAt(index, c2);
    }
}



发表于 2021-03-18 19:05:03 回复(0)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

int main() {
	string str = "1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
	string s;
	getline(cin, s);
	for (int i = 0; i < s.size(); i++) {
		int pos = str.find(s[i]);
		if (pos != string::npos) {
			s[i] = str[pos - 1];
		}
	}
	cout << s << endl;
	return 0;
}


编辑于 2020-05-04 20:57:46 回复(0)
这道题我感受到了出题老师深深的恶意....
#include<bits/stdc++.h>
using namespace std;

int main(){
    map<char,char> mp;
    mp['W'] = 'Q';
    mp['E'] = 'W';
    mp['R'] = 'E';
    mp['T'] = 'R';
    mp['Y'] = 'T';
    mp['U'] = 'Y';
    mp['I'] = 'U';
    mp['O'] = 'I';
    mp['P'] = 'O';
    mp['['] = 'P';
    mp['S'] = 'A';
    mp['D'] = 'S';
    mp['F'] = 'D';
    mp['G'] = 'F';
    mp['H'] = 'G';
    mp['J'] = 'H';
    mp['K'] = 'J';
    mp['L'] = 'K';
    mp[';'] = 'L';
    mp['X'] = 'Z';
    mp['C'] = 'X';
    mp['V'] = 'C';
    mp['B'] = 'V';
    mp['N'] = 'B';
    mp['M'] = 'N';
    mp[','] = 'M';
    mp[']'] = '[';
    mp['\\'] = ']';
    mp['\''] = ';';
    mp['.'] = ',';
    mp['/'] = '.';
    mp['1'] = '`';
    mp['2'] = '1';
    mp['3'] = '2';
    mp['4'] = '3';
    mp['5'] = '4';
    mp['6'] = '5';
    mp['7'] = '6';
    mp['8'] = '7';
    mp['9'] = '8';
    mp['0'] = '9';
    mp['-'] = '0';
    mp['='] = '-';
    mp['Q']='Q';
    mp['A']='A';
    mp['Z']='Z';
    string str;
    while(getline(cin,str)){
        string s1;
        for(int i=0;i<str.length();i++){
            if(str[i]!=' '){
                s1+=mp[str[i]];
            }else{
                s1+=str[i];
            }
        }
        cout<<s1<<endl;
    }
    return 0;
}


发表于 2020-04-03 18:05:06 回复(0)
只能枚举
#include <iostream>
(720)#include <string>
#include <unordered_map>

using namespace std;

int main(){
    string line;
    getline(cin, line);
    string c1 = R"(234567890-=WERTYUIOP[]\SDFGHJKL;'XCVBNM,./ )";
    string c2 = R"(1234567890-QWERTYUIOP[]ASDFGHJKL;ZXCVBNM,. )";
    unordered_map<char, char> mp;
    int n = c1.length(), m = line.length();
    for(int i=0; i<n; i++) mp[c1[i]] = c2[i];
    for(int i=0; i<m; i++) cout<<mp[line[i]];

    return 0;
}


发表于 2020-03-30 16:36:35 回复(0)
#include <iostream>
#include <map>
using namespace std;

char key[47] = {"1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./"};
map<char, int> mp;
void init() {
    for(int i = 0; i < 46; i++) {
        mp[key[i]] = i;
    }
}
int main() {
    init();
    string s;
    while(getline(cin, s)) {
        for(int i = 0; i < s.size(); i++) {
            if(s[i] == ' ') cout <<s[i];
            else cout <<key[mp[s[i]] - 1];
        }
        cout <<endl;
    }
    return 0;
}

编辑于 2020-03-21 20:32:47 回复(0)
//一定要有空格(' ')对应的映射,' '的ascII码为32,而不映射则变为空,空的ascii码为0。
//虽然看上去空格和空都输出空格,实际不是一个东西。
#include<string>
#include<iostream>
#include<map>
using namespace std;
string a;
map<char, char> pro;
string wrong =   " 234567890-=WERTYUIOP[]SDFGHJKL;'\\XCVBNM,./";
string correct = " 1234567890-QWERTYUIOP[ASDFGHJKL;'ZXCVBNM,.";
int main() {
    for (int i = 0; i < correct.size(); i++) 
        pro[wrong[i]] = correct[i];
    while (getline(cin, a)) {
        string cor="";
        for (int i = 0; i < a.size(); i++) 
            cor.push_back(pro[a[i]]);
        cout << cor << endl;
    return 0;
}

编辑于 2020-03-11 12:26:53 回复(0)

收获:学习下高赞的用map处理的思路
#include <bits/stdc++.h>
using namespace std;
int main(){
	char right[]={'`','1','2','3','4','5','6','7','8','9','0'
	,'-','Q','W','E','R','T','Y','U','I','O','P','[',']','A',
	'S','D','F','G','H','J','K','L',';','Z','X','C','V','B','N','M',',','.'};
	char wrong[]={'1','2','3','4','5','6','7','8','9','0','-','=','W','E','R','T','Y','U','I','O','P'
	,'[',']','\\','S','D','F','G','H','J','K','L',';','\'','X','C','V','B','N','M',',','.','/'};
	char ch;
	while((ch=cin.get())!='\n'){
		if(ch==' ') cout<<' ';
		else {
			char * i=find(wrong,wrong+sizeof(wrong)/sizeof(char),ch);
			cout<<right[i-wrong];
		}
	}
	char * i=find(wrong,wrong+sizeof(wrong)/sizeof(char),'M');
//	cout<<i-wrong;
//	cout<<right[i-wrong];
	return 0;
}


发表于 2020-03-10 21:32:54 回复(0)
#include<stdio.h>
#
include<stdlib.h>
#include<string.h>
int main(){
    char TABLE[50] = { "`1234567890-=QWERTYUIOP[]ASDFGHJKL;'ZXCVBNM,./\0" },TTT[100];
    char* p;
    gets(TTT);
    for (int i = 0; TTT[i] != '\0'; i++){
        if (TTT[i] != ' ' && TTT[i] != '\\'){
            p = strchr(TABLE, TTT[i]);
            TTT[i] = *(p - 1);}
        else if (TTT[i] == '\\'){
            TTT[i] = ']';}}
    printf("%s\n", TTT);
    return 0;
}
发表于 2020-03-10 21:17:55 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
    string input = "";
    getline(cin,input);
    for(int i = 0;i<input.length();i++)
    {
        if(input[i]==' '||input[i]=='A'||input[i]=='Q'||input[i]=='Z')
        {
            continue;
        }
        else
        {
            int index = s.find_first_of(input[i])-1;
            input[i] = s[index];
        }
    }
    cout<<input<<endl;
}

发表于 2019-09-02 16:06:26 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    const string sum = "1234567890-=QWERTYUIOP[]\`ASDFGHJKL;'`ZXCVBNM,./";
    string seg;
    getline(cin,seg);//读入一行的内容
    int i = 0,j = 0;
    char m;
    for(;i<seg.length();i++)
    {
        m = seg[i];
        j = sum.find(m);
        if(j==0 || j==string::npos || sum[j-1]=='`'){
            continue;
        }
        seg[i] = sum[j-1];
    }
    cout<<seg<<endl;
    system("pause");
    return 0;
}
发表于 2019-03-12 22:54:28 回复(0)
直接建立一个二维数组,将键盘上的数存进去,然后遍历,如果相等,输出前一位字符

#include <stdio.h>
#include <string.h>

char tab[4][15]= {
    {'1','2','3','4','5','6','7','8','9','0','-','='},
    {'Q','W','E','R','T','Y','U','I','O','P','[',']'},
    {'A','S','D','F','G','H','J','K','L',';','\''},
    {'Z','X','C','V','B','N','M',',','.','/'}
};

int main() {
    char str[1010];
    while(gets(str)) {
        int len=strlen(str);
        for(int k=0; k<len; k++) {
            if(str[k]!=' ') {
                for(int i=0; i<4; i++) {
                    for(int j=0; j<12; j++) {
                        if(str[k]==tab[i][j]){
                            printf("%c",tab[i][j-1]);
                        }
                    }
                }
            }else{
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}


发表于 2019-03-08 10:42:39 回复(0)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
using namespace std;
string s[] = {"`1234567890-=","~!@#$%^&*()_+","QWERTYUIOP[]\\","ASDFGHJKL;'","{}",":\"","ZXCVBNM,./","<>?"};
int isIn(string s,char c)
{
    for(int i = 0 ;i<s.length();i++)
    if(s[i] == c)
    return i;
    
    return -1;
}
int main()
{
    string str;
    while(getline(cin,str))
    {
        for(int i = 0 ;i<str.length();i++)
        {
            for(int j = 0;j<8;j++)
            {
                int x = isIn(s[j],str[i]);
                if(x != -1)
                {
                    str[i] = s[j][x-1];
                    break;    
                }
            }
        }
        cout << str << endl;
    }
}

发表于 2019-03-04 11:07:49 回复(0)
int main() {
    string origin, ans;
    getline(cin, origin);
    ans = origin;
    map<char, char> myMap;
    myMap['W'] = 'Q';
    myMap['E'] = 'W';
    myMap['R'] = 'E';
    myMap['T'] = 'R';
    myMap['Y'] = 'T';
    myMap['U'] = 'Y';
    myMap['I'] = 'U';
    myMap['O'] = 'I';
    myMap['P'] = 'O';
    myMap['['] = 'P';
    myMap['S'] = 'A';
    myMap['D'] = 'S';
    myMap['F'] = 'D';
    myMap['G'] = 'F';
    myMap['H'] = 'G';
    myMap['J'] = 'H';
    myMap['K'] = 'J';
    myMap['L'] = 'K';
    myMap[';'] = 'L';
    myMap['X'] = 'Z';
    myMap['C'] = 'X';
    myMap['V'] = 'C';
    myMap['B'] = 'V';
    myMap['N'] = 'B';
    myMap['M'] = 'N';
    myMap[','] = 'M';
    myMap[']'] = '[';
    myMap['\\'] = ']';
    myMap['\''] = ';';
    myMap['.'] = ',';
    myMap['/'] = '.';
    myMap['1'] = '`';
    myMap['2'] = '1';
    myMap['3'] = '2';
    myMap['4'] = '3';
    myMap['5'] = '4';
    myMap['6'] = '5';
    myMap['7'] = '6';
    myMap['8'] = '7';
    myMap['9'] = '8';
    myMap['0'] = '9';
    myMap['-'] = '0';
    myMap['='] = '-';

    for (int i = 0; i < origin.length(); i++) {
        if (origin[i] == ' ')
            ans[i] = origin[i];
        else {
            ans[i] = myMap[origin[i]];
        }
    }
    cout << ans << endl;
    system("pause");
    return 0;
}
发表于 2019-03-03 22:13:16 回复(0)