首页 > 试题广场 >

电话号码

[编程题]电话号码
  • 热度指数:1955 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

上图是一个电话的九宫格,如你所见一个数字对应一些字母,因此在国外企业喜欢把电话号码设计成与自己公司名字相对应。例如公司的Help Desk号码是4357,因为4对应H、3对应E、5对应L、7对应P,因此4357就是HELP。同理,TUT-GLOP就代表888-4567、310-GINO代表310-4466。
NowCoder刚进入外企,并不习惯这样的命名方式,现在给你一串电话号码列表,请你帮他转换成数字形式的号码,并去除重复的部分。

输入描述:
输入包含多组数据。

每组数据第一行包含一个正整数n(1≤n≤1024)。

紧接着n行,每行包含一个电话号码,电话号码仅由连字符“-”、数字和大写字母组成。
没有连续出现的连字符,并且排除连字符后长度始终为7(美国电话号码只有7位)。


输出描述:
对应每一组输入,按照字典顺序输出不重复的标准数字形式电话号码,即“xxx-xxxx”形式。

每个电话号码占一行,每组数据之后输出一个空行作为间隔符。
示例1

输入

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
4
UTT-HELP
TUT-GLOP
310-GINO
000-1213

输出

310-1010
310-4466
487-3279
888-1200
888-4567
967-1111

000-1213
310-4466
888-4357
888-4567
import java.util.*;

public class Main{
    public static void main(String[] args){
        String symbol="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        String number="222333444555666777788899991234567890";
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            int n=scanner.nextInt();
            ArrayList<String> arrayList=new ArrayList<String>();
            for(int i=0;i<n;i++){
                String str=scanner.next();
                str=str.replace("-","");
                String result="";
                for(int j=0;j<7;j++){
                    result+=number.charAt(symbol.indexOf(str.charAt(j)+""));
                }
                result=result.substring(0,3)+"-"+result.substring(3,7);
                if(!arrayList.contains(result))
                    arrayList.add(result);
            }
            Collections.sort(arrayList);
            for(int j=0;j<arrayList.size();j++){
                System.out.println(arrayList.get(j));
            }
            System.out.println();
        }
    }
}

发表于 2018-09-20 23:26:32 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>

#define INF 1000000
using namespace std;

int main(int argc, char** argv)
{
	//freopen("in.txt", "r", stdin);
	unordered_map<char, char> dic;
	dic.emplace('A', '2');
	dic.emplace('B', '2');
	dic.emplace('C', '2');
	dic.emplace('D', '3');
	dic.emplace('E', '3');
	dic.emplace('F', '3');
	dic.emplace('G', '4');
	dic.emplace('H', '4');
	dic.emplace('I', '4');
	dic.emplace('J', '5');
	dic.emplace('K', '5');
	dic.emplace('L', '5');
	dic.emplace('M', '6');
	dic.emplace('N', '6');
	dic.emplace('O', '6');
	dic.emplace('P', '7');
	dic.emplace('Q', '7');
	dic.emplace('R', '7');
	dic.emplace('S', '7');
	dic.emplace('T', '8');
	dic.emplace('U', '8');
	dic.emplace('V', '8');
	dic.emplace('W', '9');
	dic.emplace('X', '9');
	dic.emplace('Y', '9');
	dic.emplace('Z', '9');
	int n;
	while (cin >> n && n > 0)
	{
		set<string> hs;
		string s;
		for (int i = 0; i < n; ++i)
		{
			cin >> s;
			string p = "";
			for (auto& c : s)
			{
				if (isdigit(c)) p += c;
				else if (isupper(c)) p += dic[c];
			}
			if (p.size() != 7) continue;
			p = p.substr(0, 3) + "-" + p.substr(3);
			hs.emplace(p);
		}

		for (auto& phone : hs) cout << phone << endl;
		cout << endl;
	}

	return 0;
}

发表于 2017-07-12 13:01:37 回复(0)
// 借助 hash 表完成字母和数字之间的转换即可. 注意处理大小写的情况.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>
#define INF 1000000
using namespace std;
int main(int argc, char** argv) {
    unordered_map<char, char> dic;
    dic.emplace('A', '2');
    dic.emplace('B', '2');
    dic.emplace('C', '2');
    dic.emplace('D', '3');
    dic.emplace('E', '3');
    dic.emplace('F', '3');
    dic.emplace('G', '4');
    dic.emplace('H', '4');
    dic.emplace('I', '4');
    dic.emplace('J', '5');
    dic.emplace('K', '5');
    dic.emplace('L', '5');
    dic.emplace('M', '6');
    dic.emplace('N', '6');
    dic.emplace('O', '6');
    dic.emplace('P', '7');
    dic.emplace('Q', '7');
    dic.emplace('R', '7');
    dic.emplace('S', '7');
    dic.emplace('T', '8');
    dic.emplace('U', '8');
    dic.emplace('V', '8');
    dic.emplace('W', '9');
    dic.emplace('X', '9');
    dic.emplace('Y', '9');
    dic.emplace('Z', '9');
    int n;
    while (cin >> n && n > 0) {
        set<string> hs;
        string s;
        for (int i = 0; i < n; ++i) {
            cin >> s;
            string p = "";
            for (auto& c : s) {
                if (isdigit(c)){
                    p += c;
                }
                else if (isupper(c)) {
                    p += dic[c];
                }
            }
            if (p.size() != 7){
                continue;
            }
            p = p.substr(0, 3) + "-" + p.substr(3);
            hs.emplace(p);
        }
        for (auto& phone : hs) {
            cout << phone << endl;
        }
        cout << endl;
    }
    return 0;
}

编辑于 2020-09-04 20:11:54 回复(0)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] map = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "22233344455566677778889999"};
		while (sc.hasNext()) {
			int n = sc.nextInt();
			Set<String> set = new TreeSet<>();
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < n; i ++ ) {
				char[] ch = sc.next().replace("-", "").toCharArray();
				for (int j = 0; j < ch.length; j ++ ) {
					if(j == 3) sb.append("-");
					if(Character.isLetter(ch[j])) sb.append(map[1].charAt(map[0].indexOf(ch[j])));
					else sb.append(ch[j]);
				}
				set.add(sb.toString());
				sb.delete(0, sb.length());
			}
			for (String s:set)
				System.out.println(s);
			System.out.println();
		}
	}
}

发表于 2016-10-14 01:14:55 回复(0)
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
//删除字符串中指定的字符
string removeCaracterInString(string s,char rs){
    int len = s.size();
    string res;
    for(int i=0;i<len;i++){
        if(s[i]==rs) continue;
        res += s[i];
    }
    return res;
}
int main(){
    int n;
    map<char,char> phoneMap;
    phoneMap['A'] = '2';
    phoneMap['B'] = '2';
    phoneMap['C'] = '2';
    
    phoneMap['D'] = '3';
    phoneMap['E'] = '3';
    phoneMap['F'] = '3';
    
    phoneMap['G'] = '4';
    phoneMap['H'] = '4';
    phoneMap['I'] = '4';
    
    phoneMap['J'] = '5';
    phoneMap['K'] = '5';
    phoneMap['L'] = '5';
    
    phoneMap['M'] = '6';
    phoneMap['N'] = '6';
    phoneMap['O'] = '6';
    
    phoneMap['P'] = '7';
    phoneMap['Q'] = '7';
    phoneMap['R'] = '7';
    phoneMap['S'] = '7';
    
    phoneMap['T'] = '8';
    phoneMap['U'] = '8';
    phoneMap['V'] = '8';
    
    phoneMap['W'] = '9';
    phoneMap['X'] = '9';
    phoneMap['Y'] = '9';
    phoneMap['Z'] = '9';
    while(cin>>n){
       //方式一:利用vector容器实现
        /*
       vector<string> res;
       for(int i=0;i<n;i++){
           string telephoneN;
           cin>>telephoneN;
           telephoneN = removeCaracterInString(telephoneN,'-');
           int len = telephoneN.size();
           for(int j=0;j<len;j++){
               if(telephoneN[j]>='A'){
                   telephoneN[j] = phoneMap[telephoneN[j]];
               }
           }
           telephoneN.insert(telephoneN.begin()+3,'-');
           res.push_back(telephoneN);
       }
        sort(res.begin(),res.end());
        res.erase(unique(res.begin(),res.end()),res.end());
        int res_len = res.size();
        for(int i=0;i<res_len;i++){
            cout<<res[i]<<endl;
        }
        /*
        int res_len = res.size();
        string last;
        for(int i=0;i<res_len;i++){
            if(res[i]==last)continue;
            last = res[i];
            cout<<res[i]<<endl;
        }
        
        cout<<endl;
        */
        
        //方式二:由题意可知,最终结果需要有序且不重复,这一点很符合set容器的特点
        set<string,less<string>> res;
        for(int i=0;i<n;i++){
           string telephoneN;
           cin>>telephoneN;
           telephoneN = removeCaracterInString(telephoneN,'-');
           int len = telephoneN.size();
           for(int j=0;j<len;j++){
               if(telephoneN[j]>='A'){
                   telephoneN[j] = phoneMap[telephoneN[j]];
               }
           }
           telephoneN.insert(telephoneN.begin()+3,'-');
           res.insert(telephoneN);
       }
       set<string,less<string>>::iterator it;
        for(it=res.begin();it!=res.end();it++){
            cout<<*it<<endl;
        } 
        cout<<endl;
    }
    return 0;
}

发表于 2016-06-23 16:19:03 回复(0)
用set来做自动排序
#include <iostream>
#include <string>
#include <set>
using namespace std;

int main()
{
	int n;
	string mapper[2] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
						 "22233344455566677778889999" };
	while (cin >> n && n)
	{
		string str, temp;
		set<string, less<string> > list;
		for (int i = 0; i < n; ++i)
		{
			cin >> str; temp = "";
			for (char ch : str)
			{
				if (ch != '-' && isalpha(ch))
					temp += mapper[1][toupper(ch) - 'A'];
				else if (ch != '-' && isalnum(ch))
					temp += ch;
			}
			temp.insert(temp.begin() + 3, '-');
			list.insert(temp);
		}
		for (string str : list) cout << str << endl;
		cout << endl;
	}
	return 0;
}

发表于 2015-12-19 18:54:41 回复(0)
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main()
{
	string s;
	int length;
	string NUM = "22233344455566677778889999";
	while (cin >> length)
	{
		set<string> data;
		int cnt;
		for (int i = 0; i < length; i++)
		{
			cnt = 0;
			string res;
			cin >> s;
			for (int j = 0; j < s.length(); j++)
			{
				if (s[j] >= 'A'&&s[j] <= 'Z')
				{
					res += NUM[s[j] - 'A'];
					cnt++;
					if (cnt == 3)
						res += "-";
				}
				else if (s[j] >= '0'&&s[j] <= '9')
				{
					res += s[j];
					cnt++;
					if (cnt == 3)
						res += "-";
				}
			}
			data.insert(res);
		}
		for (set<string>::iterator it = data.begin(); it != data.end(); it++)
			cout << *it << endl;
		cout << endl;
	}
	return 0;
}

发表于 2017-04-23 19:04:49 回复(0)
import java.util.*;

public class Main {
    private static boolean isLettersOrNumbers(char c) {
        if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
            return true;
        }
        return false;
    }

    private static char transform(char c) {
        if (c >= 48 && c <= 57) {
            return c;
        } else {
            if (c == 'A' || c == 'B' || c == 'C') {
                return '2';
            } else if (c == 'D' || c == 'E' || c == 'F') {
                return '3';
            } else if (c == 'G' || c == 'H' || c == 'I') {
                return '4';
            } else if (c == 'J' || c == 'K' || c == 'L') {
                return '5';
            } else if (c == 'M' || c == 'N' || c == 'O') {
                return '6';
            } else if (c == 'P' || c == 'Q' || c == 'R' || c == 'S') {
                return '7';
            } else if (c == 'T' || c == 'U' || c == 'V') {
                return '8';
            } else if (c == 'W' || c == 'X' || c == 'Y' || c == 'Z') {
                return '9';
            } else {
                return 0;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextLine()) {
            int n = Integer.parseInt(scanner.nextLine());
            String[] strings = new String[n];
            int index = 0;
            for (int i = 0; i < n; i++) {
                String str1 = scanner.nextLine();
                char[] ch1 = str1.toCharArray();
                String str2 = "";
                int count = 0;
                for (char c : ch1) {
                    if (isLettersOrNumbers(c)) {
                        if (count == 3) {
                            str2 += "-";
                            count++;
                        }
                        str2 += transform(c);
                        count++;
                    }
                }
                strings[index] = str2;
                index++;
            }
            Arrays.sort(strings);
            ArrayList<String>list = new ArrayList<>();
            for (int i = 0; i < strings.length; i++) {
                if (!list.contains(strings[i])) {
                    list.add(strings[i]);
                }
            }
            for (String str : list) {
                System.out.println(str);
            }
            System.out.println();
        }
    }
}

发表于 2023-05-15 11:52:00 回复(0)
来一个不干体力活版本的
import java.util.*;
public class Main{
    public static void main(String... args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            Set<String> set = new TreeSet<>();
            int n = sc.nextInt();
            for(int i = 0;i < n;i++){
                String str = sc.next();
                set.add(change(str));
            }
            for(String str : set){
                System.out.println(str);
            }
            System.out.println();
        }
    }
    private static String change(String str){
        StringBuilder sb = new StringBuilder();
        int ln = str.length();
        int count = 0;
        for(int i = 0;i < ln;i++){
            char ch = str.charAt(i);
            if(count == 3){
                sb.append('-');
                count++;
            }
            if(ch >= 'A' && ch <= 'Z'){
                int num = (ch - 'A') / 3 + 2;
                if(ch == 'S' || ch == 'V' || ch >= 'Y'){
                    num -= 1;
                }
                sb.append(num);
                count++;
            }else if(ch >= '0' && ch <= '9'){
                sb.append(ch);
                count++;
            }
        }
        return sb.toString();
    }
}


编辑于 2022-05-27 21:14:51 回复(0)
import java.util.*;
import java.io.*;
public class Main{
    //是否是数字
    public static boolean isDigit(char s){
        return (s >= '0' && s <= '9');
    }
    //是否是大写
    public static boolean isUpper(char s){
        return (s >= 'A' && s <= 'Z');
    }
    
    public static void main(String[] args) throws Exception{
        Map<Character,Character> map = new HashMap<>();
        String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String num =   "22233344455566677778889999";
        char[] alphach = alpha.toCharArray();
        char[] numch = num.toCharArray();
        for(int i = 0;i < alphach.length;i++){
            map.put(alphach[i],numch[i]);
        }
        Set<String> set = new TreeSet<>();
        String line;
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        while((line = bf.readLine()) != null){
            set.clear();
            int n = Integer.parseInt(line);
            for(int i = 0;i < n;i++){
                line  = bf.readLine();
                char[] linech = line.toCharArray();
                StringBuilder sb = new StringBuilder();
                for(char ch : linech){
                    if(isDigit(ch)){
                        sb.append(ch);
                    }else if(isUpper(ch)){
                        sb.append(map.get(ch));
                    }
                }
                line = sb.substring(0,3) + "-" + sb.substring(3);
                set.add(line);
            }
            for(String s : set){
                System.out.println(s);
            }
            //处理下一组数据和上一组数据之间有一个空格
             System.out.println();
        }
        
    }
}

编辑于 2022-06-07 15:13:37 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static char change(char c){
        if (c=='A'||c=='B'||c=='C'){
            return '2';
        }
        if (c=='D'||c=='E'||c=='F'){
            return '3';
        }
        if (c=='G'||c=='H'||c=='I'){
            return '4';
        }
        if (c=='J'||c=='K'||c=='L'){
            return '5';
        }
        if (c=='M'||c=='N'||c=='O'){
            return '6';
        }
        if (c=='P'||c=='Q'||c=='R'||c=='S'){
            return '7';
        }
        if (c=='T'||c=='U'||c=='V'){
            return '8';
        }
        if (c=='W'||c=='X'||c=='Y'||c=='Z'){
            return '9';
        }
        return c;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            int n=sc.nextInt();
            List<String> list=new ArrayList<>();
            for (int i = 0; i < n; i++) {
                String str=sc.next();
                String s="";
                str=str.replace("-","");
                int count=0;
                for (int j = 0; j < 7; j++) {
                    count++;
                    s += change(str.charAt(j));
                    if (count == 3){
                        s += "-";
                    }
                }
                if (!list.contains(s)) {
                    list.add(s);
                }
            }
            Collections.sort(list);
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
            System.out.println();
        }
    }
}

发表于 2022-04-19 20:53:26 回复(0)
/*有的人相爱,有的人夜里开车看海,有的人牛客题目一做一下午*/

import java.util.*;

public class Main {
    static Map<Integer, String> map1 = new HashMap<Integer, String>() {{put(2, "ABC"); put(3, "DEF"); put(4, "GHI");
        put(5, "JKL"); put(6, "MNO"); put(7, "PQRS"); put(8, "TUV"); put(9, "WXYZ");}};
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()) {
            int n = scanner.nextInt();
            String[] strs = new String[n];
            Set<String> set = new HashSet<>();
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < n; i ++) {
                strs[i] = scanner.next();
            }
            for(String str : strs) {
                find(str, sb);
                set.add(sb.toString());
                sb.delete(0, sb.length());
            }
            String[] results = set.toArray(new String[set.size()]);
            Arrays.sort(results);
            for(String result : results) {
                System.out.println(result);
            }
            System.out.println();
        }
    }

    public static void find(String str, StringBuffer sb) {
        int len = 0;
        for(int i = 0; i < str.length(); i ++) {
            char ch = str.charAt(i);
            String strCh = ch+"";
            if(ch >= '0' && ch <= '9') {
                sb.append(len == 3 ? "-"+strCh : strCh);
                len ++;
                continue;
            }

            for(int j = 2; j <= 9; j ++) {
                String word = map1.get(j);
                if(word.contains(strCh)) {
                    sb.append(len == 3 ? "-"+j : j+"");
                    len++;
                    break;
                }
            }
        }

    }
}

发表于 2021-08-02 17:53:13 回复(0)
从左向右扫描即可,这里要注意可能会出现多余的连字符,所以用一个left和right分别保证连字符左面和右面得到有效的3个和4个数字即可。然后对于去重和排序,典型的就是set容器的特性,所以非常方便
#include <iostream>
#include <set>
#include <string>
using namespace std;

char converse(char ch)
{
    if(ch=='A' || ch=='B' ||ch=='C')
        return '2';
    else if(ch=='D' || ch=='E' ||ch=='F')
        return '3';
    else if(ch=='G' || ch=='H' ||ch=='I')
        return '4';
    else if(ch=='J' || ch=='K' ||ch=='L')
        return '5';
    else if(ch=='M' || ch=='N' ||ch=='O')
        return '6';
    else if(ch=='P' || ch=='Q' ||ch=='R' || ch=='S')
        return '7';
    else if(ch=='T' || ch=='U' ||ch=='V')
        return '8';
    else
        return '9';
}

int main()
{
    int n=0;
    while(cin >> n)
    {
        set<string> ret;
        while(n--)
        {
            string receive;//一个号码
            cin>>receive;
            int left=0;//保证连字符左面是三个
            int start=0;
            string temp;//转换临时保存
            while(left<3)//扫描连字符左面,去除多余连字符
            {
                if(receive[start] >= '0' && receive[start]<='9' || receive[start]>='A' && receive[start]<='Z')
                {
                    if(receive[start] >= '0' && receive[start]<='9')
                    {
                        temp+=receive[start];
                    }
                    else
                    {
                        temp+=converse(receive[start]);
                    }
                    start++;
                    left++;
                }
                else
                {
                    start++;
                }
            }
            temp+='-';
            int right=0;
            while(right<4)//同理,右面也是一样
            {
                if(receive[start] >= '0' && receive[start]<='9' || receive[start]>='A' && receive[start]<='Z')
                {
                    if(receive[start] >= '0' && receive[start]<='9')
                    {
                        temp+=receive[start];
                    }
                    else
                    {
                        temp+=converse(receive[start]);
                    }
                    start++;
                    right++;
                }
                else
                {
                    start++;
                }
            }
            ret.insert(temp);
        }
        for(auto e: ret)//set容器技能去重,也能排序
        {
            cout<<e<<endl;
        }
        cout<<endl;
    }
    return 0;
    
}


发表于 2021-06-14 15:37:58 回复(0)
写了个很笨的解法.....,是我太菜了
import java.util.*;

public class Main {
    public static List<Character> l2= Arrays.asList('A','B','C');
    public static List<Character> l3= Arrays.asList('D','E','F');
    public static List<Character> l4= Arrays.asList('G','H','I');
    public static List<Character> l5= Arrays.asList('J','K','L');
    public static List<Character> l6= Arrays.asList('M','N','O');
    public static List<Character> l7= Arrays.asList('P','Q','R','S');
    public static List<Character> l8= Arrays.asList('T','V','U');
    public static List<Character> l9= Arrays.asList('W','X','Y','Z');

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            int n=sc.nextInt();
            String[] inp = new String[n];
            for (int i = 0; i < n; i++) {
                inp[i]=sc.next().replace("-","");
            }
            spiltStr(inp);
            System.out.println();
        }
    }

    private static void spiltStr(String[] inp) {
        char[] c=new char[8];
        String s;
        ArrayList<String > a =new ArrayList<>();
        for (int i = 0; i < inp.length; i++) {
            c=inp[i].toCharArray();
            s=getNumber(c);
            if (!a.contains(s)){
                a.add(s);
            }
        }
        Collections.sort(a);
        for (String str:a) {
            System.out.println(str);
        }
    }

    private static String getNumber(char[] c) {
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < c.length; i++) {
            if (i==3){
                sb.append('-');
            }
            if (l2.contains(c[i])){
                sb.append("2");
            }else if (l3.contains(c[i])){
                sb.append("3");
            }else if (l4.contains(c[i])){
                sb.append("4");
            }else if (l5.contains(c[i])){
                sb.append("5");
            }else if (l6.contains(c[i])){
                sb.append("6");
            }else if (l7.contains(c[i])){
                sb.append("7");
            }else if (l8.contains(c[i])){
                sb.append("8");
            }else if (l9.contains(c[i])){
                sb.append("9");
            }else {
                sb.append(c[i]);
            }
        }
        return sb.toString();
    }
}


发表于 2021-03-16 18:47:28 回复(0)

/*
1.用数组存a-z对应的数字
2.记得用set来装答案
3.每组用例输出完得换行
*/

#include<iostream>
#include<cstdio>
#include<string>
#include<set>
#include<cctype>
#include<algorithm>
using namespace std;

int main(){
	int arr[256] = { 0 };
	for (int i = 'P'; i <= 'S'; ++i){
		arr[i] = 7;
	}
	for (int i = 'W'; i <= 'Z'; ++i){
		arr[i] = 9;
	}
	int j = 'A';
	for (int i = 2; i <= 8; ++i){
		if (i == 7){
			j += 4;
		}
		else{ 
			int tmp = j + 2;
			for (j; j <= tmp; ++j){
				arr[j] = i;
			}
		}
	}
	int n = 0;
	while (cin >> n){
		set<string> res;
		string c_name;
		string tmp1;
		for (int i = 0; i < n; ++i) {
			cin >> c_name;
			int len = c_name.size();
			for (int i = 0; i < len; ++i){
				if (tmp1.size() == 3){
					tmp1 += '-';
				}
				if (c_name[i] >= '0'&&c_name[i] <= '9'){
					tmp1 += c_name[i];
				}
				else if (c_name[i] == '-'){
					continue;
				}
				else{
					//字母
                                        //本题不区分大小写 可以忽略这个if语句
					if (c_name[i] >= 'a'&&c_name[i] <= 'z'){
						c_name[i] = toupper(c_name[i]);
					}
					tmp1 += to_string(arr[c_name[i]]);
				}
			}
			res.insert(tmp1);
			tmp1.clear();
		}
		auto it = res.begin();
		while (it != res.end()){
			cout << *it << endl;
			++it;
		}
		cout << endl;
	}
	return 0;
}

发表于 2020-07-20 12:46:06 回复(0)
哪位大神帮我看看这个代码为啥越界了!IDEA能跑过..万分感谢😃😃😃😃😃😀
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            ArrayList<String> arrayList=new ArrayList<>();
            int n=scanner.nextInt();
            String [] str=new String[n];
            for (int i = 0; i <str.length ; i++) {
                str[i]=scanner.next();
                String ret=toCoverse(str[i]).toString();
                ret=ret.substring(0,3)
                        +"-"+ret.substring(3,7);
                if(!arrayList.contains(ret)){
                    arrayList.add(ret);
                }
            }
            Collections.sort(arrayList);
            for (int i = 0; i <arrayList.size() ; i++) {
                System.out.println(arrayList.get(i));
            }
            scanner.nextLine();
            
        }
    }
    public  static StringBuilder toCoverse(String s) {
        StringBuilder list=new StringBuilder();
        char []ch=s.toCharArray();
        for (int i = 0; i <ch.length ; i++) {
            if(ch[i]>='0'&&ch[i]<='9'){
                list.append(ch[i]);
            }
            if(ch[i]>='A'&&ch[i]<='B'){
                list.append('2');
            }
            if(ch[i]>='D'&&ch[i]<='F'){
                list.append('3');
            }
            if(ch[i]>='G'&&ch[i]<='I'){
                list.append('4');
            }
            if(ch[i]>='J'&&ch[i]<='L'){
                list.append('5');
            }
            if(ch[i]>='M'&&ch[i]<='O'){
                list.append('6');
            }
            if(ch[i]>='P'&&ch[i]<='S'){
                list.append('7');
            }
            if(ch[i]>='T'&&ch[i]<='V'){
                list.append('8');
            }
            if(ch[i]>='W'&&ch[i]<='Z'){
                list.append('9');
            }
        }
        return list;
    }
}


发表于 2020-04-20 12:21:28 回复(0)
#define _CRT_SECURE_NO_WARNINGS 1
(2679)#include<iostream>
#include<string>
(765)#include<vector>
#include<map>
using namespace std;
bool isTrue(string str)
{
	int count = 0;
	for (int i = 0; i < str.size(); ++i)
	{
		if ((str[i] >= '0' && str[i] <= '9') ||
			(str[i] >= 'A' && str[i] <= 'Z'))
		{
			count++;
			continue;
		}
		else if (str[i] == '-')
			continue;
		else
			return false;
	}
	if (count == 7)
		return true;
	else
		return false;
}
void NumDesk(string& tmp)
{
	for (int i = 0; i < tmp.size(); ++i)
	{
		if (tmp[i] == '-')
		{
			tmp.erase(tmp.begin()+i);
			--i;
		}
		else
		{
			if (tmp[i] >= 'A' && tmp[i] <= 'C')
				tmp[i] = '2';
			if (tmp[i] >= 'D' && tmp[i] <= 'F')
				tmp[i] = '3';
			if (tmp[i] >= 'G' && tmp[i] <= 'I')
				tmp[i] = '4';
			if (tmp[i] >= 'J' && tmp[i] <= 'L')
				tmp[i] = '5';
			if (tmp[i] >= 'M' && tmp[i] <= 'O')
				tmp[i] = '6';
			if (tmp[i] >= 'P' && tmp[i] <= 'S')
				tmp[i] = '7';
			if (tmp[i] >= 'T' && tmp[i] <= 'V')
				tmp[i] = '8';
			if (tmp[i] >= 'W' && tmp[i] <= 'Z')
				tmp[i] = '9';
		}
	}
	tmp.insert(tmp.begin() + 3, '-');
}
int main()
{
	int n = 0;
	while (cin >> n)
	{
		vector<string> str(n);
		for (int i = 0; i < str.size(); ++i)
			cin >> str[i];

		map<string, int> arr;

		for (int i = 0; i < str.size(); ++i)
		{
			if (isTrue(str[i]))
			{
				NumDesk(str[i]);
				arr.insert(make_pair(str[i],0));
			}
		}

		auto it = arr.begin();
		while (it != arr.end())
		{
			cout << it->first << endl;
			++it;
		}
        
        cout << endl;
	}

	return 0;
}

发表于 2020-03-17 10:18:48 回复(0)
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.Comparator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        Set<String> set=new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.parseInt(o1)-Integer.parseInt(o2);
            }
        });
        while (in.hasNextLine()){
            int count=in.nextInt();
            while (count>=0) {
                String str = in.nextLine();
                String result=doAction(str);
                if (result!=null) {
                    set.add(result);
                }
                count--;
            }
           
            for (String s : set) {
                  System.out.println(s.substring(0, 3) + "-" + s.substring(3));
            }
            System.out.println();
            set.clear();
        }

    }

    private static String doAction(String str) {
       if ( !check(str)){
           return null;
       }
       String result="";
       String newstr=str.replaceAll("-","");
       for (int i=0;i<7;i++){
           String c=newstr.substring(i,i+1);
           result+=CL(c);
       }

       return result;
    }

    private static String CL(String c) {
        if (c.matches("[0-9]")){
            return c;
        }else {
            if (c.matches("[A-O]")) {
                return String.valueOf((c.charAt(0)-'A')/3+2);
            }
            if (c.matches("[P-S]")){
                return "7";
            }
            if (c.matches("[T-V]")){
                return "8";
            }
            if (c.matches("[W-Z]")){
                return "9";
            }
        }
        return null;
    }

    private static boolean check(String str) {
        int num=0;
        String[] arr=str.split("-");
        for (String s:arr){
            num+=s.length();
        }
        if (num==7){
         return true;
        }
        return false;
    }
}
总之吧,在牛客上答题一定要注意细节!!!手动笑哭。。。。。无奈
注意事项:
1、输入包含多组测试用例,,,,不是白写的,一定要好好审题
2、每组测试用例输出之后要再有个回车换行,实例也要看仔细了
希望大家认真仔细,可以找个好工作啊!!!!!


发表于 2019-07-22 16:42:26 回复(0)
醉了这测试用例
求大佬解释!!*_*

发表于 2018-07-23 00:17:12 回复(2)
table = {k: v for i in [dict.fromkeys('ABC',2), dict.fromkeys('DEF',3), dict.fromkeys('GHI',4),
                       dict.fromkeys('JKL',5), dict.fromkeys('MNO',6), dict.fromkeys('PRQS',7)
                       , dict.fromkeys('TUV',8), dict.fromkeys('WXYZ',9)] for k, v in i.items()}

def transform(x):
    x = ''.join(x.split('-'))
    result = ''
    for char in x:
        if char in table:
            result += str(table[char])
        else:
            result += char
    result = result[:3] + '-' + result[3:]
    return result
    
try:
    while 1:
        contact = []
        for j in xrange(input()):
            contact.append(transform(raw_input()))
        result = sorted(set(contact))
        for w in result:
            print w
       	print ''
except:
    pass

发表于 2016-12-29 19:27:27 回复(0)

问题信息

难度:
20条回答 10873浏览

热门推荐

通过挑战的用户

查看代码