您的位置:首页 > 其它

poj1002 487-3279 ——水题

2013-04-19 11:22 399 查看
题目链接:http://poj.org/problem?id=1002

题目大意:

  给定一组电话号码,如果是包括字母,那么就映射到数字,统计相同的电话号码个数大于1的号码,按照从小到大的顺序输出,并且输出相应的个数。

题目思路:

  因为电话号码最多又7位数字,在整数范围内,把每个读入的电话号码转化成整数,用一个map记录是不是出现过,如果出现过,就把相应的map记录加1,如果没有出现过,就把这个数字放进一个数组arr里面,这样arr里面的数字是不重复的。最后把arr排序,对于每一个数组元素,输出相应的map记录,也就是它出现过的次数。

  输出的时候,按照整数输出,但是要注意,开始要输出3位,如果前面有0的话,按照整数输出就不行,所以要控制格式,用printf(“%03d”);这样的格式,表示前面要补齐0;卡住了,看得以前的代码才想到这种情况,还是思维不严谨 啊~

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long int LL;
const int MAXN = 0x3f3f3f3f;
const int MINN = -0x3f3f3f3f;
const double eps = 1e-9;
const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1},
{1,1},{1,-1},{-1,-1}};

int arr[100000+10];
map<char, int> ma;
char pri[10];
int main(void){
#ifndef ONLINE_JUDGE
freopen("poj1002.in", "r", stdin);
#endif
for (int i = 0; i < 5; ++i){
ma[i*3+'A'] = ma[i*3+1+'A'] = ma[i*3+2+'A'] = i + 2;
}
ma['P'] = ma['R'] = ma['S'] = 7;
ma['T'] = ma['U'] = ma['V'] = 8;
ma['W'] = ma['X'] = ma['Y'] = 9;
int n; scanf("%d", &n); char a[100]; map<int , int> cnt;
int k = 0, temp, i, j; cnt.clear();
for (i = 1; i <= n; ++i){
scanf("%s", a); int len = strlen(a), temp = 0;
for (j = 0; j < len; ++j){
if (isalpha(a[j])) {
temp = temp * 10 + ma[a[j]];
} else if (isdigit(a[j])){
temp = temp * 10 + (a[j] - '0');
}
}
if (cnt[temp] == 0){
cnt[temp]++; arr[k++] = temp;
} else cnt[temp]++;
}
sort(arr, arr+k); bool flag = false;
for (i = 0; i < k; ++i){
if (cnt[arr[i]] != 1){
flag = true;
printf("%03d-%04d %d\n", arr[i]/10000,arr[i]%10000,cnt[arr[i]]);
}
}
if (!flag){
cout << "No duplicates.\n";
}

return 0;
}


主要是为了练习一下STL,用来刷水题……看来自己做水题速度还是比较慢的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: