#UVa:755-487–3279

灆洢 2014-10-09 10:36:52

先將所有號碼轉換成相同格式,再利用C++中的map去紀錄即可。

P.S. C++的map使用的是紅黑樹結構,以key排序。

C++(1.215)

/*******************************************************/
/* UVa 755 487--3279                                   */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2014/10/09                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <cctype>
#include <map>
using namespace std;

int main(){
  const int TURN_TO_NUMBER[] = { 
    2, 2, 2, 3, 3, 3, 
    4, 4, 4, 5, 5, 5,
    6, 6, 6, 7, -1, 7, 7,
    8, 8, 8, 9, 9, 9, -1 };
  int datasets;
  while( scanf("%d", &datasets) != EOF ){

    for( int i = 0 ; i < datasets ; ++i ){
      if( i != 0 ) printf("\n");

      int N;
      scanf( "%d", &N );

      string number;
      map<string, int> numbers_check;
      for( int j = 0 ; j < N ; ++j ){
        cin >> number;

        string real_number;
        for( int k = 0 ; k < number.length() ; ++k ){
          if( isdigit(number[k]) ){
            real_number += number[k];
          }
          else if( isalpha(number[k]) && TURN_TO_NUMBER[(number[k]-'A')] != -1 ){
            real_number += (TURN_TO_NUMBER[(number[k]-'A')] + '0');
          }

          if( real_number.length() == 3 ){
            real_number += '-';
          }
        }

        ++numbers_check[real_number];
      }

      bool no_duplicate = true;
      for( map<string, int>::iterator it = numbers_check.begin() ;
           it != numbers_check.end() ; 
           ++it ){
        if( it->second > 1 ){
          printf("%s %d\n", (it->first).c_str(), it->second );
          no_duplicate = false;
        }
      }

      if( no_duplicate ) printf("No duplicates.\n");
    }

  }

  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料